Python編程使用matplotlib繪制動(dòng)態(tài)圓錐曲線示例
作為讓高中生心臟驟停的四個(gè)字,對(duì)于高考之后的人來(lái)說(shuō)可謂刻骨銘心,所以定義不再贅述,直接擼圖,其標(biāo)準(zhǔn)方程分別為

在Python中,繪制動(dòng)圖需要用到matplotlib中的animation包,其調(diào)用方法以及接下來(lái)要用到的參數(shù)為
ani = animation.FuncAnimation(fig, func, frames, interval)
其中fig為繪圖窗口,func為繪圖函數(shù),其返回值為圖像,frames為迭代參數(shù),如果為整型的話,其迭代參數(shù)則為range(frames)。
橢圓
為了繪圖方便,橢圓的參數(shù)方程為


代碼為:
# 這三個(gè)包在后面的程序中不再?gòu)?fù)述
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
a,b,c = 5,3,4
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(autoscale_on=False,
xlim=(-a,a),ylim=(-b,b))
ax.grid()
line, = ax.plot([],[],'o-',lw=2)
trace, = ax.plot([],[],'-', lw=1)
theta_text = ax.text(0.02,0.85,'',transform=ax.transAxes)
textTemplate = '''theta = %.1f°\n
lenL = %.1f, lenR = %.1f\n
lenL+lenR = %.1f'''
xs,ys = [], []
def animate(i):
if(i==0):
xs.clear()
ys.clear()
theta = i*0.04
x = a*np.cos(theta)
y = b*np.sin(theta)
xs.append(x)
ys.append(y)
line.set_data([-c,x,c], [0,y,0])
trace.set_data(xs,ys)
lenL = np.sqrt((x+c)**2+y**2)
lenR = np.sqrt((x-c)**2+y**2)
theta_text.set_text(textTemplate %
(180*theta/np.pi, lenL, lenR, lenL+lenR))
return line, trace, theta_text
ani = animation.FuncAnimation(fig, animate, 157,
interval=5, blit=True)
ani.save("ellipse.gif")
plt.show()
雙曲線
雙曲線的參數(shù)方程為

設(shè) a = 4 , b = 3 , c = 5 則代碼如下
a,b,c = 4,3,5
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(autoscale_on=False,
xlim=(-c,16),ylim=(-12,12))
ax.grid()
line, = ax.plot([],[],'o-',lw=2)
trace, = ax.plot([],[],'-', lw=1)
theta_text = ax.text(0.01,0.85,'',
transform=ax.transAxes)
textTemplate = '''t = %.1f\n
lenL = %.1f, lenR = %.1f\n
lenL-lenR = %.1f'''
xs,ys = [],[]
def animate(t):
if(t==-3):
xs.clear()
ys.clear()
x = a*np.cosh(t)
y = b*np.sinh(t)
xs.append(x)
ys.append(y)
line.set_data([-c,x,c], [0,y,0])
trace.set_data(xs,ys)
lenL = np.sqrt((x+c)**2+y**2)
lenR = np.sqrt((x-c)**2+y**2)
theta_text.set_text(textTemplate %
(t, lenL, lenL, lenL-lenR))
return line, trace, theta_text
frames = np.arange(-3,3,0.05)
ani = animation.FuncAnimation(fig, animate,
frames, interval=5, blit=True)
ani.save("hyperbola.gif")
plt.show()

拋物線

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
a,b,c = 4,3,5
p = 1
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(autoscale_on=False,
xlim=(-0.6,4.5),ylim=(-3,3))
ax.grid()
ax.plot([-p/2,-p/2],[-5,5],'-',lw=2)
line, = ax.plot([],[],'o-',lw=2)
trace, = ax.plot([],[],'-', lw=1)
theta_text = ax.text(0.05,0.85,'',
transform=ax.transAxes)
textTemplate = '''y = %.1f\n
lenL = %.1f, lenF = %.1f\n
lenL-lenF = %.1f'''
xs,ys = [],[]
def animate(y):
if(y==-3):
xs.clear()
ys.clear()
x = y**2/p/2
xs.append(x)
ys.append(y)
line.set_data([-p,x,p/2], [y,y,0])
trace.set_data(xs,ys)
lenL = x+p/2
lenF = np.sqrt((x-p/2)**2+y**2)
theta_text.set_text(textTemplate %
(y, lenL, lenF, lenL-lenF))
return line, trace, theta_text
frames = np.arange(-3,3,0.1)
ani = animation.FuncAnimation(fig, animate,
frames, interval=5, blit=True)
ani.save("parabola.gif")
plt.show()

極坐標(biāo)方程
圓錐曲線在極坐標(biāo)系下有相同的表達(dá)式,即

在matplotlib中,極坐標(biāo)圖像需要通過(guò)projection='polar'來(lái)標(biāo)識(shí),其代碼為
p = 2
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(autoscale_on=False, projection='polar')
ax.set_rlim(0,8)
trace, = ax.plot([],[],'-', lw=1)
theta_text = ax.text(0.05,0.95,'',transform=ax.transAxes)
textTemplate = 'e = %.1f\n'
theta = np.arange(-3.1,3.2,0.1)
def animate(e):
rho = p/(1-e*np.cos(theta))
trace.set_data(theta,rho)
theta_text.set_text(textTemplate % e)
return trace, theta_text
frames = np.arange(-2,2,0.1)
ani = animation.FuncAnimation(fig, animate,
frames, interval=100, blit=True)
ani.save("polar.gif")
plt.show()

以上就是Python使用matplotlib繪制動(dòng)態(tài)的圓錐曲線示例的詳細(xì)內(nèi)容,更多關(guān)于matplotlib繪制動(dòng)態(tài)圓錐曲線的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python os.listdir與os.walk實(shí)現(xiàn)獲取路徑詳解
這篇文章主要介紹了Python使用os.listdir和os.walk獲取文件路徑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-10-10
對(duì)python字典過(guò)濾條件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python字典過(guò)濾條件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python中實(shí)現(xiàn)定制類的特殊方法總結(jié)
這篇文章主要介紹了python中實(shí)現(xiàn)定制類的特殊方法總結(jié),本文講解了__str__、__iter__、__getitem__、__getattr__、__call__等特殊方法,需要的朋友可以參考下2014-09-09
python爬蟲(chóng)之生活常識(shí)解答機(jī)器人
這篇文章主要介紹了python爬蟲(chóng)之生活常識(shí)解答機(jī)器人,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
python實(shí)現(xiàn)連連看輔助(圖像識(shí)別)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)連連看輔助程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Python中用函數(shù)作為返回值和實(shí)現(xiàn)閉包的教程
這篇文章主要介紹了Python中用函數(shù)作為返回值和實(shí)現(xiàn)閉包的教程,代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
詳解python中Numpy的屬性與創(chuàng)建矩陣
這篇文章給大家分享了關(guān)于python中Numpy的屬性與創(chuàng)建矩陣的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2018-09-09
pygame庫(kù)實(shí)現(xiàn)移動(dòng)底座彈球小游戲
這篇文章主要為大家詳細(xì)介紹了pygame庫(kù)實(shí)現(xiàn)移動(dòng)底座彈球小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

