2

Python编程使用matplotlib绘制动态圆锥曲线示例

 2 years ago
source link: https://studygolang.com/articles/35276
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python编程使用matplotlib绘制动态圆锥曲线示例

e31845690mei · 2天之前 · 98 次点击 · 预计阅读时间 5 分钟 · 大约8小时之前 开始浏览    

目录 椭圆 双曲线 抛物线 极坐标方程 作为让高中生心脏骤停的四个字,对于高考之后的人来说可谓刻骨铭心,所以定义不再赘述,直接撸图,其标准方程分别为

在这里插入图片描述

在Python中,绘制动图需要用到matplotlib中的animation包,其调用方法以及接下来要用到的参数为

1 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 = i0.04 x = anp.cos(theta) y = bnp.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+y2) lenR = np.sqrt((x-c)2+y2) theta_text.set_text(textTemplate % (180theta/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()

双曲线 双曲线的参数方程为

在这里插入图片描述

Python客栈送红包、纸质书

设 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 = anp.cosh(t) y = bnp.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+y2) lenR = np.sqrt((x-c)2+y2) 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 = y2/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() 在这里插入图片描述 https://studygolang.com/tag/%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B718313230473


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK