最近,我在阿里天池大数据竞赛中发现了一些练手的项目,例如,用Python“画月饼过云上中秋”,虽然中秋已经过去了,但还是好奇Python是怎么画出月饼的?
先上图↓
火龙果馅的月饼。主要使用了Matplotlib模块和NumPy模块,这里我只修改了字体、线宽和颜色相关代码。修改后的完整代码如下:
# 解决中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
length = 20
R = 3 ** 0.5 * length / (3 ** 0.5 * cos(pi / 12) - sin(pi / 12))
r = 2 * sin(pi / 12) * R / 3 ** 0.5
##ec为线条颜色,可以自由替换
arc1 = Arc([0, length], width=2 * r, height=2 * r,
angle=0, theta1=30, theta2=150, ec='orange', linewidth=8)
arc2 = Arc([-length / 2, length / 2 * 3 ** 0.5], width=2 * r, height=2 * r,
angle=0, theta1=60, theta2=180, ec='orange', linewidth=8)
arc3 = Arc([-length / 2 * 3 ** 0.5, length / 2], width=2 * r, height=2 * r,
angle=0, theta1=90, theta2=210, ec='orange', linewidth=8)
arc4 = Arc([-length, 0], width=2 * r, height=2 * r, angle=0, theta1=120, theta2=240, ec='orange', linewidth=8)
arc5 = Arc([-length / 2 * 3 ** 0.5, -length / 2], width=2 * r, height=2 * r,
angle=0, theta1=150, theta2=270, ec='orange', linewidth=8)
arc6 = Arc([-length / 2, -length / 2 * 3 ** 0.5], width=2 * r, height=2 * r,
angle=0, theta1=180, theta2=300, ec='orange', linewidth=8)
arc7 = Arc([0, -length], width=2 * r, height=2 * r, angle=0, theta1=210, theta2=330, ec='orange', linewidth=8)
arc8 = Arc([length / 2, -length / 2 * 3 ** 0.5], width=2 * r, height=2 * r,
angle=0, theta1=240, theta2=360, ec='orange', linewidth=8)
arc9 = Arc([length / 2 * 3 ** 0.5, -length / 2], width=2 * r, height=2 * r,
angle=0, theta1=270, theta2=390, ec='orange', linewidth=8)
arc10 = Arc([length, 0], width=2 * r, height=2 * r, angle=0, theta1=300, theta2=420, ec='orange', linewidth=8)
arc11 = Arc([length / 2 * 3 ** 0.5, length / 2], width=2 * r, height=2 * r,
angle=0, theta1=330, theta2=450, ec='orange', linewidth=8)
arc12 = Arc([length / 2, length / 2 * 3 ** 0.5], width=2 * r, height=2 * r,
angle=0, theta1=0, theta2=120, ec='orange', linewidth=8)
##ec为线条颜色,fc为填充颜色,可以自由替换
circle = Circle((0, 0), R, ec='orange', fc='DeepPink', linewidth=8)
##ec为线条颜色,fc为填充颜色,可以自由替换
wedge1 = Wedge([-2, 2], R - 5, 90, 180,
ec='orange', fc='DeepPink', linewidth=8)
wedge2 = Wedge([-5, 5], R - 12, 90, 180, ec='orange',
fc='DeepPink', linewidth=8)
wedge3 = Wedge([-2, -2], R - 5, 180, 270, ec='orange',
fc='DeepPink', linewidth=8)
wedge4 = Wedge([-5, -5], R - 12, 180, 270, ec='orange',
fc='DeepPink', linewidth=8)
wedge5 = Wedge([2, -2], R - 5, 270, 360, ec='orange',
fc='DeepPink', linewidth=8)
wedge6 = Wedge([5, -5], R - 12, 270, 360, ec='orange',
fc='DeepPink', linewidth=8)
wedge7 = Wedge([2, 2], R - 5, 0, 90, ec='orange',
fc='DeepPink', linewidth=8)
wedge8 = Wedge([5, 5], R - 12, 0, 90, ec='orange',
fc='DeepPink', linewidth=8)
art_list = [arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12]
art_list.extend([circle, wedge1, wedge2, wedge3, wedge4, wedge5, wedge6, wedge7, wedge8])
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_aspect('equal')
for a in art_list:
ax.add_patch(a)
# 关闭坐标轴
plt.axis('off')
##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-15, -2.5, '火龙果馅', bbox=dict(boxstyle='square', fc="w", ec='orange', linewidth=4), fontsize=50, color='orange')
plt.text(-28, -33, '明月几时有,把酒问青天', fontsize=30, color='#aa4a30')
plt.ylim([-35, 35])
plt.xlim([-35, 35])
plt.show()
注意:上述代码在Python IDLE或PyCharm下运行,如果参加阿里天池大赛就不要修改关于字体的代码了。
喜欢的小伙伴可以去尝试画出自己的专属月饼~
网址如下:
新人赛-天池大数据竞赛-天池大赛-阿里云天池 https://tianchi.aliyun.com/competition/gameList/coupleList
对于新手来说,学习赛里面的项目当作练手是非常不错的。当然,如果你有一定的实力也可以去挑战有奖金的项目哦!
干货来了!
关于代码中用到的matplotlib.patches模块的几个关键类的说明如下图所示。
如果想更换月饼颜色,颜色值参考如下图所示。
如果你想学习更多的关于Pandas模块、Matplotlib模块和NumPy模块的知识和案例可以参考一下《Python数据分析从入门到实践(全彩版)》这本书。
原创不易,如果觉得有点用,希望可以随手 点个赞,拜谢各位老铁。