Python编程使用matplotlib绘制动态圆锥曲线示例
目录
- 椭圆
- 双曲线
- 抛物线
- 极坐标方程
文章图片
在Python中,绘制动图需要用到
matplotlib
中的animation
包,其调用方法以及接下来要用到的参数为ani = animation.FuncAnimation(fig, func, frames, interval)
其中
fig
为绘图窗口,func
为绘图函数,其返回值为图像,frames
为迭代参数,如果为整型的话,其迭代参数则为range(frames)
。椭圆 为了绘图方便,椭圆的参数方程为
文章图片
文章图片
代码为:
# 这三个包在后面的程序中不再复述import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationa,b,c = 5,3,4fig = 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°\nlenL = %.1f, lenR = %.1f\nlenL+lenR = %.1f'''xs,ys = [], []def animate(i):if(i==0):xs.clear()ys.clear()theta = i*0.04x = 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_textani = 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,5fig = 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\nlenL = %.1f, lenR = %.1f\nlenL-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_textframes = 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 npimport matplotlib.pyplot as pltimport matplotlib.animation as animationa,b,c = 4,3,5p = 1fig = 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\nlenL = %.1f, lenF = %.1f\nlenL-lenF = %.1f'''xs,ys = [],[]def animate(y):if(y==-3):xs.clear()ys.clear()x = y**2/p/2xs.append(x)ys.append(y)line.set_data([-p,x,p/2], [y,y,0])trace.set_data(xs,ys)lenL = x+p/2lenF = np.sqrt((x-p/2)**2+y**2)theta_text.set_text(textTemplate % (y, lenL, lenF, lenL-lenF))return line, trace, theta_textframes = 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 = 2fig = 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_textframes = np.arange(-2,2,0.1)ani = animation.FuncAnimation(fig, animate, frames, interval=100, blit=True)ani.save("polar.gif")plt.show()
文章图片
【Python编程使用matplotlib绘制动态圆锥曲线示例】 以上就是Python使用matplotlib绘制动态的圆锥曲线示例的详细内容,更多关于matplotlib绘制动态圆锥曲线的资料请关注脚本之家其它相关文章!
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- iOS中的Block