千家信息网

Python编程如何使用matplotlib绘制动态圆锥曲线

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章主要介绍了Python编程如何使用matplotlib绘制动态圆锥曲线,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。作为让高中
千家信息网最后更新 2024年09月21日Python编程如何使用matplotlib绘制动态圆锥曲线

这篇文章主要介绍了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.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_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/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_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绘制动态圆锥曲线"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

参数 方程 篇文章 圆锥 圆锥曲线 曲线 代码 极坐标 绘图 动态 编程 双曲线 图像 椭圆 迭代 相同 刻骨铭心 接下来 三个 下有 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 青岛洪杰网络技术 大众关闭服务器还能换液晶仪表吗 获取数据库时间语句java 世界地球都市服务器 下载游戏服务器 省网络安全和信息化工作规则 用什么软件开发arm7好 简幻欢服务器怎么开实验模式 北京聚搏时代网络技术 神佑为什么没办法切换服务器 东莞有招聘网络安全员的吗 平时怎样做到网络安全 山东特亿宝互联网科技公司官网 生物科技和互联网医疗 iis服务器安全狗 如何利用闲置的服务器赚钱 普及推广网络安全 在云服务器上运行有什么不一样 工行软件开发中心珠海地址 昆明软件开发工程师培训排行 安享家网络技术 中国卫通网络安全培训 航空公司软件开发岗笔试 iis服务器安全狗 网络安全是干什么的累吗 网络安全第二版课后答案 李沧区平台软件开发外包公司 办公软件开发有什么好处 奥的斯服务器怎么看历史故障 sony软件开发
0