In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Python programming how to use matplotlib to draw a dynamic conic curve, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
As the four words that cause cardiac arrest in senior high school students, it is unforgettable for people after the college entrance examination, so the definition is no longer repeated, and the standard equations are respectively.
In Python, the animation package in matplotlib is used to draw a dynamic diagram, and the calling method and the parameters to be used next are
Ani = animation.FuncAnimation (fig, func, frames, interval)
Fig is the drawing window, func is the drawing function, its return value is the image, frames is the iterative parameter, if it is an integer, its iterative parameter is range (frames).
Ellipse
For the convenience of drawing, the parameter equation of the ellipse is
The code is:
# these three packages will not be repeated in the following program: import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationa,b,c = 5pje 3pje 4fig = plt.figure (figsize= (12pje 9)) ax = fig.add_subplot (autoscale_on=False, xlim= (- a), ylim= (- b)) ax.grid () line, = ax.plot ([], [], 'Omurf (]) trace, = ax.plot ([], [],' -') Lw=1) theta_text = ax.text (0.02, 0.85) textTemplate =''theta =% .1f °\ nlenL =% .1f, lenR =% .1f\ nlenL+lenR =% .1f' xs,ys = [] [] def animate (I): if (iMagnum 0): xs.clear () ys.clear () theta = iFlex 0.04 x = a*np.cos (theta) y = b*np.sin (theta) xs.append (x) ys.append (y) line.set_data ([- c Ling XMagi c], [0je Yjue 0]) trace.set_data (xs) Ys) lenL = np.sqrt ((Xmurc) * * 2+y**2) lenR = np.sqrt ((XMEC) * * 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 () hyperbola
The parameter equation of the hyperbola is
If a = 4, b = 3, c = 5, the code is as follows
(figsize= (12) ax = fig.add_subplot (autoscale_on=False, xlim= (- c), ylim= (- 12)) ax.grid () line, = ax.plot ([], [], '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 (tantalizum3): xs.clear () ys.clear () x = a*np.cosh (t) y = b*np.sinh (t) xs.append (x) ys.append (y) line.set_data 0]) trace.set_data (xs,ys) lenL = np.sqrt ((xmerc) * * 2+y**2) lenR = np.sqrt ((xmerc) * * 2+y**2) theta_text.set_text (textTemplate% (t, lenL, lenL, lenL-lenR) return line, trace, theta_textframes = np.arange ani = animation.FuncAnimation (fig, animate, frames, interval=5) Blit=True) ani.save ("hyperbola.gif") plt.show ()
Parabola
Import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationa,b,c = 4Jing 3jing5p = 1fig = plt.figure (figsize= (12p9)) ax = fig.add_subplot (autoscale_on=False, xlim= (- 0.6p4.5), ylim= (- 3je 3)) ax.grid () ax.plot ([- pYuppag2], [- 5jue 5],'-', lw=2) line, = ax.plot ([], [], 'Olyphant LWP 2) trace, = ax.plot ([], [] '-', lw=1) theta_text = ax.text (0.05 transform=ax.transAxes) textTemplate =''y =% .1f\ nlenL =% .1f, lenF =% .1f\ nlenL-lenF =% .1f''xs,ys = [] [] def animate (y): if (y): xs.clear () ys.clear () x = y**2/p/2 xs.append (x) ys.append (y) line.set_data 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 ani = animation.FuncAnimation (fig, animate, frames, interval=5, blit=True) ani.save ("parabola.gif") plt.show ()
Polar coordinate equation
The conic curve has the same expression in the polar coordinate system, that is
In matplotlib, polar images need to be identified by projection='polar', and the code is
P = 2fig = plt.figure (figsize= (12Power9)) ax = fig.add_subplot (autoscale_on=False, projection='polar') ax.set_rlim (0score8) 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 theta 3.2 theta_text.set_text 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 ani = animation.FuncAnimation (fig, animate, frames, interval=100 Blit=True) ani.save ("polar.gif") plt.show ()
Thank you for reading this article carefully. I hope the article "how to draw a dynamic conic curve with matplotlib in Python programming" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 213
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.