python练手_Python数据分析练手项目
最近,我在阿里天池大數(shù)據(jù)競(jìng)賽中發(fā)現(xiàn)了一些練手的項(xiàng)目,例如,用Python“畫(huà)月餅過(guò)云上中秋”,雖然中秋已經(jīng)過(guò)去了,但還是好奇Python是怎么畫(huà)出月餅的?
先上圖↓
火龍果餡的月餅。主要使用了Matplotlib模塊和NumPy模塊,這里我只修改了字體、線寬和顏色相關(guān)代碼。修改后的完整代碼如下:
# 解決中文亂碼 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) # 關(guān)閉坐標(biāo)軸 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, '明月幾時(shí)有,把酒問(wèn)青天', fontsize=30, color='#aa4a30') plt.ylim([-35, 35]) plt.xlim([-35, 35]) plt.show()注意:上述代碼在Python IDLE或PyCharm下運(yùn)行,如果參加阿里天池大賽就不要修改關(guān)于字體的代碼了。
喜歡的小伙伴可以去嘗試畫(huà)出自己的專屬月餅~
網(wǎng)址如下:
新人賽-天池大數(shù)據(jù)競(jìng)賽-天池大賽-阿里云天池 https://tianchi.aliyun.com/competition/gameList/coupleList
對(duì)于新手來(lái)說(shuō),學(xué)習(xí)賽里面的項(xiàng)目當(dāng)作練手是非常不錯(cuò)的。當(dāng)然,如果你有一定的實(shí)力也可以去挑戰(zhàn)有獎(jiǎng)金的項(xiàng)目哦!
干貨來(lái)了!
關(guān)于代碼中用到的matplotlib.patches模塊的幾個(gè)關(guān)鍵類的說(shuō)明如下圖所示。
如果想更換月餅顏色,顏色值參考如下圖所示。
如果你想學(xué)習(xí)更多的關(guān)于Pandas模塊、Matplotlib模塊和NumPy模塊的知識(shí)和案例可以參考一下《Python數(shù)據(jù)分析從入門(mén)到實(shí)踐(全彩版)》這本書(shū)。
原創(chuàng)不易,如果覺(jué)得有點(diǎn)用,希望可以隨手點(diǎn)個(gè)贊,拜謝各位老鐵。總結(jié)
以上是生活随笔為你收集整理的python练手_Python数据分析练手项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苹果无线耳机后面的按钮什么作用
- 下一篇: 升级bigsur_升级 macOS Bi