Python编程使用matplotlib绘制动态圆锥曲线示例
作者:微小冷 时间:2021-08-30 03:38:18
作为让高中生心脏骤停的四个字,对于高考之后的人来说可谓刻骨铭心,所以定义不再赘述,直接撸图,其标准方程分别为
在Python中,绘制动图需要用到matplotlib
中的animation
包,其调用方法以及接下来要用到的参数为
ani = animation.FuncAnimation(fig, func, frames, interval)
其中fig
为绘图窗口,func
为绘图函数,其返回值为图像,frames
为迭代参数,如果为整型的话,其迭代参数则为range(frames)
。
椭圆
为了绘图方便,椭圆的参数方程为
代码为:
# 这三个包在后面的程序中不再复述
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()
双曲线
双曲线的参数方程为
设 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()
极坐标方程
圆锥曲线在极坐标系下有相同的表达式,即
在matplotlib
中,极坐标图像需要通过projection='polar'
来标识,其代码为
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绘制动态的圆锥曲线示例的详细内容,更多关于matplotlib绘制动态圆锥曲线的资料请关注脚本之家其它相关文章!
来源:https://blog.csdn.net/m0_37816922/article/details/120637882?spm=1001.2014.3001.5501
标签:matplotlib,动态,圆锥曲线


猜你喜欢
GoLang并发机制探究goroutine原理详细讲解
2023-08-30 05:41:33

异步加载Google Adsense 更新到Wordpress 2.62
2008-09-11 13:09:00
django之导入并执行自定义的函数模块图解
2023-07-27 02:45:19

详解Vue中Computed与watch的用法与区别
2023-07-02 17:05:24
Chrome V8 引擎对 sort 的优化
2010-02-04 17:27:00
Python中的字符串切片(截取字符串)的详解
2023-07-23 20:37:59

Python双版本计算器详解
2021-03-27 13:22:24
对python中的控制条件、循环和跳出详解
2022-03-08 00:41:44
原生javascript实现匀速运动动画效果
2024-06-07 15:27:55

常用于后台开发的jQuery插件
2010-09-25 12:47:00
element-ui表格合并span-method的实现方法
2024-05-28 15:59:51

基于JS判断iframe是否加载成功的方法(多种浏览器)
2023-08-24 04:14:52
使用Django框架中ORM系统实现对数据库数据增删改查
2024-01-28 03:25:50

解决python中使用PYQT时中文乱码问题
2023-07-28 10:15:51
pycharm 2018 激活码及破解补丁激活方式
2021-10-16 12:10:00

解决idea打开窗口/tab过多导致隐藏的问题
2022-12-29 10:45:42

Python深入浅出分析enum枚举类
2022-07-07 15:09:14

python正则表达式匹配IP代码实例
2022-01-03 00:25:52
vue关于eslint空格缩进等的报错问题及解决
2024-05-10 14:09:26

MySQL中InnoDB和MyISAM类型的差别
2008-11-05 13:32:00