Home  >  Article  >  Backend Development  >  Use Python to draw a cute ice cube

Use Python to draw a cute ice cube

王林
王林Original
2024-01-13 14:19:051036browse

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:

pip install matplotlib
pip install numpy

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:

import matplotlib.pyplot as plt
import numpy as np

# 设置图形的大小和分辨率
plt.figure(figsize=(6, 6), dpi=80)

# 设置图形的标题
plt.title("可爱的冰墩墩")

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:

# 绘制身体
body = plt.Circle((0.5, 0.5), 0.4, color='white')

# 添加身体到图形中
plt.gca().add_patch(body)

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:

# 绘制左眼
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)

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:

# 绘制嘴巴
mouth = plt.Arc((0.5, 0.4), 0.3, 0.2, 0, 0, 180, color='black')

# 添加嘴巴到图形中
plt.gca().add_patch(mouth)

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:

# 绘制左胳膊
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)

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:

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()

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:
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