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


猜你喜欢
- 静态方法:将下面的代码复制到<body>~</body>内 程序代码 <table cellpadd
- 实验介绍增量恢复一般适用的场景:1、人为的sql语句破坏了数据库2、在进行下一次完全备份之前发生系统故障导致数据库数据丢失3、在主从架构中,
- 前言Sphinx是一款支持多种编程语言的文档生成工具,在python项目开发过程中,可以帮助开发者根据需求生成相应的说明文档,拿今天我们就基
- 正在看的ORACLE教程是:Oracle常见错误代码的分析与解决。在使用ORACLE的过程过,我们会经常遇到一些ORACLE产生的错误,对于
- 一、背景介绍今天,野鸡大学高(三)班的月考成绩出来了,这里先给大家公布一下各位同学的考试成绩。接着,在给大家公布一下各位同学的生活消费情况。
- 如下所示:#coding=utf-8#布局自定义尺寸from tkinter import *class App:def __init__(
- 前言logging模块是Python内置的标准模块,主要用于输出脚本运行日志,可以设置输出日志的等级、日志保存路径等。可以通过设置不同的日志
- MySQL服务器有几个影响其操作的参数(变量)。如果缺省的参数值不合适,可以将其修改为对服务器运行环境更合适的值。例如,如果您有大量的内存,
- <%@ Language=VBScript %><HTML><HEAD>
- Python-pip安装失败问题一、问题描述root@ubuntu:/home/chao# apt-get install python-p
- 炸金花题目很简单:就是自己写一个程序,实现诈金花游戏的发牌、判断输赢。规则:一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。牌
- 需求:主线程开启了多个线程去干活,每个线程需要完成的时间不同,但是在干完活以后都要通知给主线程下面上代码:#!/usr/bin/python
- 正在看的ORACLE教程是:Oracle 数据表分区的策略。本文描述通过统计分析出医院信息系统需分区的表,对需分区的表选择分区键,即找出包括
- 1.前言面向对象编程的三大特性:封装、继承、多态。可见继承是面向对象程序设计中一个重要的概念。Go 作为面向对象的编程语言,自然也支持继承。
- items()方法返回字典的(键,值)元组对的列表语法以下是items()方法的语法:dict.items()参数 &
- 本文实例为大家分享了readAsDataUrl方法预览图片的具体代码,供大家参考,具体内容如下<html> <head&
- 打桩测试当我们在编写单元测试的时候,有时我们非常想 mock 掉其中一个方法,但是这个方法又没有接口去定义和实现(无法用 gith
- 本文实例为大家分享了opencv+python实现图像矫正的具体代码,供大家参考,具体内容如下需求:将斜着拍摄的文本图像进行矫正python
- 如果我们在标识列中插入值,例如:insert member(id,username) values(10,'admin')
- 一、多线程间的资源竞争以下列task1(),task2()两个函数为例,分别将对全局变量num加一重复一千万次循环(数据大一些,太小的话执行