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

 更新时间:2021年10月19日 11:56:44   作者:微小冷  
这篇文章主要介绍了Python使用matplotlib绘制动态的圆锥曲线示例实现代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步

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

在这里插入图片描述

在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绘制动态圆锥曲线的资料请关注脚本之家其它相关文章!

相关文章

  • python字典按照value排序方法

    python字典按照value排序方法

    在本篇文章里小编给各位分享一篇关于python字典按照value排序方法的相关文章,有兴趣的朋友们可以学习下。
    2020-12-12
  • pycharm如何使用anaconda中的各种包(操作步骤)

    pycharm如何使用anaconda中的各种包(操作步骤)

    这篇文章主要介绍了pycharm如何使用anaconda中的各种包,本文通过操作步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • Django记录操作日志与LogEntry的使用详解

    Django记录操作日志与LogEntry的使用详解

    我们既知道如何记录变更日志,也知道如何获取变更日志,那么如何才能够在admin后台方便地查看操作日志呢?这篇文章主要给大家介绍了关于Django记录操作日志与LogEntry使用的相关资料,需要的朋友可以参考下
    2022-01-01
  • Python实现贪心算法的示例

    Python实现贪心算法的示例

    这篇文章主要介绍了Python实现贪心算法的示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04
  • python的函数和方法(上)

    python的函数和方法(上)

    这篇文章主要为大家详细介绍了python的函数和方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • 详解利用装饰器扩展Python计时器

    详解利用装饰器扩展Python计时器

    在本文中,云朵君将和大家一起了解装饰器的工作原理,如何将我们之前定义的定时器类 Timer 扩展为装饰器,以及如何简化计时功能,感兴趣的可以了解一下
    2022-06-06
  • python中序列的逆序方式

    python中序列的逆序方式

    这篇文章主要介绍了python中序列的逆序方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Python+Kepler.gl轻松制作酷炫路径动画的实现示例

    Python+Kepler.gl轻松制作酷炫路径动画的实现示例

    这篇文章主要介绍了Python+Kepler.gl轻松制作酷炫路径动画的实,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧现示例
    2020-06-06
  • python判断所输入的任意一个正整数是否为素数的两种方法

    python判断所输入的任意一个正整数是否为素数的两种方法

    今天小编就为大家分享一篇python判断所输入的任意一个正整数是否为素数的两种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Django ForeignKey与数据库的FOREIGN KEY约束详解

    Django ForeignKey与数据库的FOREIGN KEY约束详解

    这篇文章主要介绍了Django ForeignKey与数据库的FOREIGN KEY约束详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05

最新评论