Home Backend Development Python Tutorial Use Python to draw a cute ice cube

Use Python to draw a cute ice cube

Jan 13, 2024 pm 02:19 PM
python drawing Cute image design Bingdundun painting

Use Python to draw a cute ice cube

Using Python to draw the cute Bingdundun

Bingdundun, as the mascot of the Beijing Winter Olympics, is deeply loved by people for its cute image. In this article, we will use Python language to draw a cute icy image. First, we need to understand Python’s drawing libraries matplotlib and numpy.

Step 1: Install matplotlib and numpy libraries
Before using these two libraries, we need to install them first. Open a command line terminal and enter the following commands to install these two libraries:

1

2

pip install matplotlib

pip install numpy

Copy after login

Step 2: Import the necessary libraries
Before we start writing code, we first need to import the matplotlib and numpy libraries and provide The drawing sets up some basic configuration. The following is a code example for importing libraries and configurations:

1

2

3

4

5

6

7

8

import matplotlib.pyplot as plt

import numpy as np

 

# 设置图形的大小和分辨率

plt.figure(figsize=(6, 6), dpi=80)

 

# 设置图形的标题

plt.title("可爱的冰墩墩")

Copy after login

Step 3: Draw Bingdundun’s body
Bingdundun’s body is composed of a circle, we can use matplotlib Circle function to draw this circle. The following is a code example for drawing the body:

1

2

3

4

5

# 绘制身体

body = plt.Circle((0.5, 0.5), 0.4, color='white')

 

# 添加身体到图形中

plt.gca().add_patch(body)

Copy after login

Step 4: Draw Bingdundun’s eyes
Bingdundun’s eyes are two black circles, we can use matplotlib’s Circle function to draw these two circles. The following is a code example for drawing eyes:

1

2

3

4

5

6

7

8

9

10

11

# 绘制左眼

left_eye = plt.Circle((0.4, 0.6), 0.1, color='black')

 

# 添加左眼到图形中

plt.gca().add_patch(left_eye)

 

# 绘制右眼

right_eye = plt.Circle((0.6, 0.6), 0.1, color='black')

 

# 添加右眼到图形中

plt.gca().add_patch(right_eye)

Copy after login

Step 5: Draw Bingdundun’s mouth
Bingdundun’s mouth is an arc, we can use matplotlib’s Arc function to draw this arc. The following is a code example for drawing a mouth:

1

2

3

4

5

# 绘制嘴巴

mouth = plt.Arc((0.5, 0.4), 0.3, 0.2, 0, 0, 180, color='black')

 

# 添加嘴巴到图形中

plt.gca().add_patch(mouth)

Copy after login

Step 6: Draw Bingdundun’s arms and hands
Bingdundun’s arms are two curved line segments, and the hands are two circles. We can Use matplotlib's Plot function and Circle function to draw these graphics. The following is a code example for drawing arms and hands:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

# 绘制左胳膊

left_arm = plt.Polygon([[0.3, 0.5], [0.2, 0.4], [0.1, 0.5]], color='black')

 

# 添加左胳膊到图形中

plt.gca().add_patch(left_arm)

 

# 绘制右胳膊

right_arm = plt.Polygon([[0.7, 0.5], [0.8, 0.4], [0.9, 0.5]], color='black')

 

# 添加右胳膊到图形中

plt.gca().add_patch(right_arm)

 

# 绘制左手

left_hand = plt.Circle((0.1, 0.5), 0.05, color='black')

 

# 添加左手到图形中

plt.gca().add_patch(left_hand)

 

# 绘制右手

right_hand = plt.Circle((0.9, 0.5), 0.05, color='black')

 

# 添加右手到图形中

plt.gca().add_patch(right_hand)

Copy after login

Step 7: Display the graphics
Finally, we need to use the plt.show() function to display the graphics we drew. The following is a complete code example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

import matplotlib.pyplot as plt

import numpy as np

 

plt.figure(figsize=(6, 6), dpi=80)

plt.title("可爱的冰墩墩")

 

body = plt.Circle((0.5, 0.5), 0.4, color='white')

plt.gca().add_patch(body)

 

left_eye = plt.Circle((0.4, 0.6), 0.1, color='black')

plt.gca().add_patch(left_eye)

 

right_eye = plt.Circle((0.6, 0.6), 0.1, color='black')

plt.gca().add_patch(right_eye)

 

mouth = plt.Arc((0.5, 0.4), 0.3, 0.2, 0, 0, 180, color='black')

plt.gca().add_patch(mouth)

 

left_arm = plt.Polygon([[0.3, 0.5], [0.2, 0.4], [0.1, 0.5]], color='black')

plt.gca().add_patch(left_arm)

 

right_arm = plt.Polygon([[0.7, 0.5], [0.8, 0.4], [0.9, 0.5]], color='black')

plt.gca().add_patch(right_arm)

 

left_hand = plt.Circle((0.1, 0.5), 0.05, color='black')

plt.gca().add_patch(left_hand)

 

right_hand = plt.Circle((0.9, 0.5), 0.05, color='black')

plt.gca().add_patch(right_hand)

 

plt.axis('scaled')

plt.axis('off')

plt.show()

Copy after login

Through the above steps, we successfully drew a lovely ice-dump image. You can modify and expand the drawn content according to your own interests and creativity, and add more colors and details to make the image of Bing Dundun more lifelike. Have a great time!

The above is the detailed content of Use Python to draw a cute ice cube. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles