在 Python 中,我们将使用以下方法和模块完成绘制任务:
matplotlib.patches 模块:
matplotlib.pyplot 模块:
首先,你需要安装Matplotlib库。如果还没有安装,可以通过以下命令进行安装:
1 |
pip install matplotlib |
我们还需要用到NumPy库来生成平滑的蛇形曲线。它可以帮助我们更好地控制蛇的身体形态。
1 |
pip install numpy |
我们首先创建绘图画布,并设置招财猫的坐标系范围。注意:需要关闭坐标轴,以便突出卡通形象。
1 2 3 4 5 6 7 8 |
import matplotlib.pyplot as plt from matplotlib.patches import Circle, Ellipse, Polygon, FancyBboxPatch, Arc
# 创建画布 fig, ax = plt.subplots(figsize=(6, 8)) ax.set_xlim(-10, 10) ax.set_ylim(-12, 12) ax.axis('off') # 隐藏坐标轴 |
这幅艺术图的中心元素是数字“2025”,代表着未来的年份。我们使用FancyBboxPatch来绘制这些数字,利用边框和圆角使其看起来更有趣。每个数字被放置在不同的位置,确保它们之间有足够的间隔。代码如下:
1 2 3 4 5 6 7 8 9 10 11 |
# 绘制数字块 def draw_number_block(x, y, number, color="red", edge_color="black"): block = FancyBboxPatch((x, y), 3, 5, boxstyle="round,pad=0.2", color=color, edgecolor=edge_color, linewidth=2) ax.add_patch(block) ax.text(x + 1.5, y + 2.5, number, color="black", fontsize=30, ha="center", va="center", fontweight="bold")
# 绘制数字“2025” draw_number_block(-9, -6, "2") draw_number_block(-5, -6, "0") draw_number_block(-1, -6, "2") draw_number_block(3, -6, "5") |
通过FancyBboxPatch,我们为数字设置了圆角矩形的背景,并用ax.text方法在每个矩形中心绘制数字,使用黑色字体来增加对比度。
接下来,我们绘制蛇的身体,利用平滑的样条曲线模拟蛇的形状。使用np.sin和np.cos函数生成蛇的轨迹,并通过指数衰减让蛇身逐渐变小,增加层次感。
1 2 3 4 5 |
# 绘制小蛇身体(用平滑的样条曲线模拟) t = np.linspace(0, 2 * np.pi, 500) x_snake = 5 * np.sin(t) * np.exp(-0.05 * t) # 小蛇身体的横向分布 y_snake = 3 * np.cos(t) * np.exp(-0.05 * t) + 3 # 小蛇身体的纵向分布 ax.plot(x_snake, y_snake, color="yellow", linewidth=12, zorder=2) # 黄色曲线表示蛇的身体 |
蛇的身体用黄色线条表示,通过ax.plot绘制曲线。蛇的头部在蛇形曲线的起点上,我们通过一个圆形表示蛇头,并为蛇头添加一些装饰。
为了让蛇看起来更加生动,我们为蛇头添加了眼睛、腮红、花朵、嘴巴和舌头等细节。眼睛是两个小黑圆形,而腮红则是两个淡粉色的圆形,增加了可爱的感觉。嘴巴用一个“v”字形表示,而舌头则是通过多边形来完成的。
1 2 3 4 5 6 7 8 |
# 蛇头装饰 head_x, head_y = x_snake[0], y_snake[0] head = Circle((head_x, head_y), radius=1.5, color="yellow", ec="black", lw=2, zorder=3) ax.add_patch(head)
# 眼睛 ax.add_patch(Circle((head_x - 0.5, head_y + 0.5), radius=0.2, color="black", zorder=4)) # 左眼 ax.add_patch(Circle((head_x + 0.5, head_y + 0.5), radius=0.2, color="black", zorder=4)) # 右眼 |
除此之外,我们还为蛇的头部添加了几朵粉色的小花,通过绘制多个小圆形来实现。
为了增强艺术效果,我们在图的背景加入了星星和花瓣装饰。我们使用draw_star函数绘制了几颗星星,分布在画布的不同位置。
1 2 3 4 5 6 7 8 9 10 |
# 背景装饰(星星和花瓣) def draw_star(x, y, size=0.5, color="gold"): for angle in range(0, 360, 144): star_x = x + size * np.cos(np.radians(angle)) star_y = y + size * np.sin(np.radians(angle)) ax.plot([x, star_x], [y, star_y], color=color, lw=2)
draw_star(2, 5, size=1) draw_star(-6, 3, size=1) draw_star(6, 0, size=1) |
此外,我们还在左右两侧添加了“蛇年有福”和“平安喜乐”两段文字,利用ax.text函数为文字加上背景框,增强可读性。
1 2 3 4 5 6 |
# 绘制左边的文字 "蛇年有福" x_text_left, y_text_left = -7, 6 # 左侧文字位置 for i, char in enumerate("蛇年有福"): ax.text(x_text_left, y_text_left - i * 2, char, fontsize=25, color="yellow", ha="center", va="center", fontweight="bold", bbox=dict(boxstyle="round", facecolor="red", edgecolor="black", pad=0.5), fontname="SimHei") # 指定字体为SimHei(黑体) |
文字的颜色为黄色,背景色是红色,字体使用了“SimHei”(黑体),并为文字添加了圆角矩形背景框,增强视觉效果。
通过plt.show()方法,我们展示了最终绘制的艺术图。这幅图不仅仅是一个简单的图形,它结合了数字、蛇形、花朵和装饰文字,传达出祝福与喜庆的氛围。
1 2 |
# 显示图像 plt.show() |
将所有部分整合在一起:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import matplotlib.pyplot as plt from matplotlib.patches import Ellipse, Circle, Polygon, FancyBboxPatch import numpy as np
def draw_snake_art_optimized_v4(): # 设置画布 fig, ax = plt.subplots(figsize=(12, 10)) ax.set_xlim(-10, 10) ax.set_ylim(-10, 10) ax.axis('off') ax.set_facecolor('white') # 背景色
# 绘制“2025”数字块 def draw_number_block(x, y, number, color="red", edge_color="black"): block = FancyBboxPatch((x, y), 3, 5, boxstyle="round,pad=0.2", color=color, edgecolor=edge_color, linewidth=2) ax.add_patch(block) ax.text(x + 1.5, y + 2.5, number, color="black", fontsize=30, ha="center", va="center", fontweight="bold")
# 绘制数字 draw_number_block(-9, -6, "2") draw_number_block(-5, -6, "0") draw_number_block(-1, -6, "2") draw_number_block(3, -6, "5")
# 绘制小蛇身体(用平滑的样条曲线模拟) t = np.linspace(0, 2 * np.pi, 500) x_snake = 5 * np.sin(t) * np.exp(-0.05 * t) # 小蛇身体的横向分布 y_snake = 3 * np.cos(t) * np.exp(-0.05 * t) + 3 # 小蛇身体的纵向分布 ax.plot(x_snake, y_snake, color="yellow", linewidth=12, zorder=2) # 黄色曲线表示蛇的身体
# 蛇头 head_x, head_y = x_snake[0], y_snake[0] head = Circle((head_x, head_y), radius=1.5, color="yellow", ec="black", lw=2, zorder=3) ax.add_patch(head)
# 蛇头装饰(花朵) def draw_flower(x, y, size=0.3, color="pink"): for angle in range(0, 360, 72): petal_x = x + size * np.cos(np.radians(angle)) petal_y = y + size * np.sin(np.radians(angle)) petal = Circle((petal_x, petal_y), radius=size * 0.6, color=color, ec="black", lw=1) ax.add_patch(petal) center = Circle((x, y), radius=size * 0.5, color="red", zorder=4) ax.add_patch(center)
draw_flower(head_x + 0.5, head_y + 1) draw_flower(head_x - 0.8, head_y + 0.8) draw_flower(head_x, head_y - 1.5)
# 蛇的表情细节 # 眼睛 ax.add_patch(Circle((head_x - 0.5, head_y + 0.5), radius=0.2, color="black", zorder=4)) # 左眼 ax.add_patch(Circle((head_x + 0.5, head_y + 0.5), radius=0.2, color="black", zorder=4)) # 右眼 ax.add_patch(Circle((head_x - 0.5, head_y + 0.5), radius=0.1, color="white", zorder=5)) # 左眼高光 ax.add_patch(Circle((head_x + 0.5, head_y + 0.5), radius=0.1, color="white", zorder=5)) # 右眼高光
# 腮红 ax.add_patch(Circle((head_x - 1, head_y - 0.5), radius=0.3, color="pink", alpha=0.7, zorder=3)) # 左腮红 ax.add_patch(Circle((head_x + 1, head_y - 0.5), radius=0.3, color="pink", alpha=0.7, zorder=3)) # 右腮红
# 嘴巴和舌头 ax.text(head_x, head_y - 0.7, "v", fontsize=30, color="black", ha="center", zorder=4) # 嘴巴 tongue = Polygon([[head_x, head_y - 1.3], [head_x + 0.3, head_y - 1.8], [head_x - 0.3, head_y - 1.8]], closed=True, color="red", zorder=4) ax.add_patch(tongue)
# 背景装饰(星星和花瓣) def draw_star(x, y, size=0.5, color="gold"): for angle in range(0, 360, 144): star_x = x + size * np.cos(np.radians(angle)) star_y = y + size * np.sin(np.radians(angle)) ax.plot([x, star_x], [y, star_y], color=color, lw=2)
draw_star(2, 5, size=1) draw_star(-6, 3, size=1) draw_star(6, 0, size=1)
# 绘制左边的文字 "蛇年有福" x_text_left, y_text_left = -7, 6 # 左侧文字位置 for i, char in enumerate("蛇年有福"): ax.text(x_text_left, y_text_left - i * 2, char, fontsize=25, color="yellow", ha="center", va="center", fontweight="bold", bbox=dict(boxstyle="round", facecolor="red", edgecolor="black", pad=0.5), fontname="SimHei") # 指定字体为SimHei(黑体)
# 绘制右边的文字 "平安喜乐" x_text_right, y_text_right = 7, 6 # 右侧文字位置 for i, char in enumerate("平安喜乐"): ax.text(x_text_right, y_text_right - i * 2, char, fontsize=25, color="yellow", ha="center", va="center", fontweight="bold", bbox=dict(boxstyle="round", facecolor="red", edgecolor="black", pad=0.5), fontname="SimHei") # 指定字体为SimHei(黑体)
# 显示图像 plt.show()
# 调用绘制函数 draw_snake_art_optimized_v4() |
最终效果
运行本文的代码后,我们将获得如下所示的可爱蛇年祝福图片:
通过这篇博客文章,我们利用Matplotlib和NumPy绘制了一幅结合数字和蛇形图案的艺术图,图中融入了丰富的细节,如花朵、眼睛、腮红、舌头等,营造出了一种既有趣又富有寓意的效果。无论是用于贺卡、海报,还是仅仅作为创意表达,这幅图都非常合适。
Matplotlib不仅仅是一个绘图库,它也能帮助我们实现各种富有创意的设计。在Python中,通过编程创造艺术作品,既是对技术的挑战,也是对创意的发扬。