Python编程使用matplotlib绘制动态圆锥曲线示例
作者:微小冷 发布时间:2021-08-30 03:38:18
标签: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绘制动态圆锥曲线的资料请关注脚本之家其它相关文章!
来源:https://blog.csdn.net/m0_37816922/article/details/120637882?spm=1001.2014.3001.5501
0
投稿
猜你喜欢
- 最近对 Range 和 Selection 比较感兴趣。基本非 IE 的浏览器都支持 DOM Level2 中的 Range,而 IE 中仅
- 从2003年到现在,从ACCESS到SQL SERVER的使用。在ACCESS中没有存储过程的概念。在使用过程中,发现ACCESS与SQL
- 这是一个很和谐很实用的网站管理程序,和我以前介绍的服务器管理程序不同的是,这个程序只有一个功能,就是实现远程Web方式删除文件(实际上是重命
- QQ登录Banner增加了剧情的概念之后,已经推出了春节和情人节两期。这之后设想能围绕Banner做的更加丰富,对传统文化的体现也能更为深入
- 1、灵活运用样式熟悉网页设计的网友就知道,调用Style的方法很多,我们可以单击鼠标右键选择Custo
- 在sql语句中,如果查找某个文本字段值为空的可以用select * from 表 where 字段=''但是如果
- 使用 Python 内建的defaultdict 方法可以轻松定义一个树的数据结构。简单的说树也可以是一个字典数据结构 def t
- 【原文地址】 Fixes for Common VS 2008 and .NET 3.5 Beta2 Issu
- 概述很多人接触Python,都是从爬虫开始,其实很多语言都可以做爬虫,只是Python相对其他语言来说,更加简单而已。但是Python并不止
- 前言最近微信小游戏跳一跳大热,自己也是中毒颇久,无奈手残最高分只拿到200分。无意间看到教你用Python来玩微信跳一跳一文,在电脑上利用a
- PHP addAttribute() 函数实例给根元素和 body 元素添加一个属性:<?php $note=<<<
- # -*- coding: utf-8 -*-# @Time : 2019-11-18 09:31# @Author : cxa# @Fil
- 本文实例讲述了PHP共享内存使用与信号控制。分享给大家供大家参考,具体如下:共享内存共享内存的使用主要是为了能够在同一台机器不同的进程中共享
- 异常描述有时我们的Excel有一个调整过自定义格式的日期字段:当我们用pandas读取时却是这样的效果:不管如何指定参数都无效。出现原因没有
- 本文实例讲述了Python实现将HTML转换成doc格式文件的方法。分享给大家供大家参考,具体如下:网页上的一些文章,因为有格式的原因,它们
- 本文实例为大家分享了python实现矩阵打印的具体代码,供大家参考,具体内容如下之前面试嵌入式软件的一道题,用c实现矩阵打印,考场上并没有写
- 连续看到几个和 Oracle 优化器隐含参数 _sort_elimination_cost_ratio 相关的优化案例(Refer Refe
- 这篇文章主要介绍了Python文本处理简单易懂方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 异常详细信息: System.Web.HttpException: 无法向会话状态服务器发出会话状态请求。请确保已启动 ASP.NET St
- 整个重装步骤大致分四个步骤进行,第一步,备份原mysql中的所有数据库。第二步,完全卸载mysql第三步,下载安装新版mysql第四步,导入