Python使用turtle和matplotlib繪制圓和愛心的示例代碼
有些小朋友很喜歡畫畫,這里就用Pyhton來畫一些簡單形狀。
首先是圓形,圓形的寓意是圓滿、完美、團(tuán)圓、優(yōu)勝和團(tuán)結(jié)。圓形在形狀上是一個平面中點到定點距離相等的圖形,象征著圓滿和完美,寓意著無所不容、和諧圓滿。圓形在古代文化中象征著宇宙萬物的狀態(tài),是宇宙意識的表現(xiàn),代表著無極、圓滿、飽滿、和諧和神圣等意義。圓形也是中華民族傳統(tǒng)上的吉祥圖案,象征著團(tuán)結(jié)、優(yōu)勝、勝利和美好。此外,圓形還代表著團(tuán)結(jié)和凝聚力,鼓勵人們?yōu)榱斯餐哪繕?biāo)而團(tuán)結(jié)一致,齊心協(xié)力克服困難。圓形的寓意非常豐富和深刻,被廣泛運用于建筑、文化、藝術(shù)、生活等多個領(lǐng)域中,代表著人們的追求和夢想。
下面我們用Python畫個圓形:
用turtle畫圓形
import turtle # 設(shè)置畫筆顏色和粗細(xì) turtle.pencolor("black") turtle.pensize(5) # 繪制一個半徑為100的圓形 turtle.circle(100) # 隱藏畫筆并保持窗口不關(guān)閉 turtle.hideturtle() turtle.done()
Turtle庫來繪制一個半徑為100的圓形。
用matplotlib畫圓形
import matplotlib.pyplot as plt import numpy as np # 創(chuàng)建一個空白的圖形對象 fig, ax = plt.subplots() # 定義圓形的中心和半徑 center = (0, 0) radius = 1 # 畫圓形 theta = np.linspace(0, 2 * np.pi, 100) x = center[0] + radius * np.cos(theta) y = center[1] + radius * np.sin(theta) ax.plot(x, y, color='r') # 顯示圖形 plt.show()
首先創(chuàng)建一個空白圖形對象,然后定義圓形的中心和半徑。接著,我們使用np.linspace
函數(shù)生成一組等間距的角度值,然后計算出對應(yīng)的橫坐標(biāo)和縱坐標(biāo),最后使用ax.plot函數(shù)將圓形畫出來。最后,我們調(diào)用plt.show
函數(shù)來顯示圖形。
經(jīng)常見小朋友們擺pose,用比心來做手勢,用手比一個心形的動作來表達(dá)對粉絲或是朋友的友好表達(dá)方式,還可以用來表達(dá)喜歡、愛你、感謝等意思。該手勢由于易懂易學(xué),容易上手,經(jīng)常出現(xiàn)在各大直播電視節(jié)目而被廣泛應(yīng)用。下面我們就用Python畫個愛心。
用turtle畫愛心
from turtle import * # 定義函數(shù)為值love,目的是繪制愛心上方的曲線 def love(): for i in range(200): # 重復(fù)200次下面的代碼 right(1) forward(1) # 畫筆向前1像素 # 為愛心設(shè)置樣式 pensize(2) # 調(diào)整畫筆粗細(xì)為2像素 speed(10) # 調(diào)節(jié)畫筆速度10 color('black', 'red') # 畫筆顏色及填充顏色 begin_fill() # 開始填充 left(140) forward(111.65) love() # 調(diào)用函數(shù) left(120) love() forward(111.65) # 調(diào)用函數(shù) end_fill() # 結(jié)束填充 hideturtle() # 隱藏畫筆 done() # 結(jié)束運行,常用于python編輯器,idea等中
另一種方式:
import turtle turtle.color('red', 'pink') turtle.pensize(2) turtle.pendown() turtle.setheading(150) turtle.begin_fill() turtle.fd(50) turtle.circle(50 * -3.745, 45) turtle.circle(50 * -1.431, 165) turtle.left(120) turtle.circle(50 * -1.431, 165) turtle.circle(50 * -3.745, 45) turtle.fd(50) turtle.end_fill()
matplotlib畫心
import numpy as np from matplotlib import pyplot as plt # Creating equally spaced 100 data in range 0 to 2*pi theta = np.linspace(0, 2 * np.pi, 100) # Generating x and y data x = 16 * ( np.sin(theta) ** 3 ) y = 13 * np.cos(theta) - 5* np.cos(2*theta) - 2 * np.cos(3*theta) - np.cos(4*theta) # Plotting plt.plot(x, y, color='red') plt.title("Heart Shape") plt.show()
這個心形用了兩個公式。
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 1000) y1 = np.sqrt(1 - (abs(x) - 1) ** 2) y2 = -3 * np.sqrt(1 - (abs(x) / 2) ** 0.5) plt.fill_between(x, y1, color='red') plt.fill_between(x, y2, color='red') # plt.text(0, -1.0, 'Heart', fontsize=24, color='black', # horizontalalignment='center') plt.show()
3D愛心
''' ================================= 3D heart shape in matplotlib ================================= Demonstrates how to plot a 3D function in cartesian coordinates. Uses the marching cubes algorithm in scikit-image to obtain a isosurface. Example contributed by CAChemE.org Adapted from: http://www.walkingrandomly.com/?p=2326 ''' from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot as plt import numpy as np from skimage import measure # Set up mesh n = 100 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) z = np.linspace(-3,3,n) X, Y, Z = np.meshgrid(x, y, z) # Create cardioid function def f_heart(x,y,z): F = 320 * ((-x**2 * z**3 -9*y**2 * z**3/80) + (x**2 + 9*y**2/4 + z**2-1)**3) return F # Obtain value to at every point in mesh vol = f_heart(X,Y,Z) # Extract a 2D surface mesh from a 3D volume (F=0) verts, faces, normals, values = measure.marching_cubes_lewiner(vol, 0, spacing=(0.1, 0.1, 0.1)) # Create a 3D figure fig = plt.figure(figsize=(12,8)) ax = fig.add_subplot(111, projection='3d') # Plot the surface ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2], cmap='Spectral', lw=1) # Change the angle of view and title ax.view_init(15, -15) # ax.set_title(u"Made with ? (and Python)", fontsize=15) # if you have Python 3 ax.set_title("Made with <3 (and Python)", fontsize=15) # Show me some love ^^ plt.show()
讓我們一起追尋童年的記憶,感悟童心的純真,擁抱童年的無邪。祝福孩子們健康成長,快樂相伴,畫出更加美好的未來!
到此這篇關(guān)于Python使用turtle和matplotlib繪制圓和愛心的示例代碼的文章就介紹到這了,更多相關(guān)Python用turtle和matplotlib繪制圓和愛心內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決python web項目意外關(guān)閉,但占用端口的問題
今天小編就為大家分享一篇解決python web項目意外關(guān)閉,但占用端口的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12pytorch __init__、forward與__call__的用法小結(jié)
這篇文章主要介紹了pytorch __init__、forward與__call__的用法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02python 按照sheet合并多個Excel的示例代碼(多個sheet)
這篇文章主要介紹了python 按照sheet合并多個Excel的示例代碼(多個sheet),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09使用Python實現(xiàn)自動化辦公的代碼示例(郵件、excel)
隨著技術(shù)的進(jìn)步,Python 的高效性和易用性使其成為辦公自動化的強(qiáng)大工具,通過 Python,我們可以自動處理日常工作中的郵件、Excel 表格等任務(wù),從而大幅提升效率,本文將詳細(xì)介紹如何使用 Python 實現(xiàn)這些自動化功能,并附上關(guān)鍵代碼示例,需要的朋友可以參考下2025-01-01Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)塊的網(wǎng)絡(luò)之VGG
雖然AlexNet證明深層神經(jīng)網(wǎng)絡(luò)卓有成效,但它沒有提供一個通用的模板來指導(dǎo)后續(xù)的研究人員設(shè)計新的網(wǎng)絡(luò)。下面,我們將介紹一些常用于設(shè)計深層神經(jīng)網(wǎng)絡(luò)的啟發(fā)式概念2021-10-10淺談Django中的數(shù)據(jù)庫模型類-models.py(一對一的關(guān)系)
今天小編就為大家分享一篇淺談Django中的數(shù)據(jù)庫模型類-models.py(一對一的關(guān)系),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05