简体   繁体   中英

Animating a circle in Matplotlib Python

I'm trying to animate a circle in such way that the circles size changes.
CODE

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
ax = plt.axes(xlim=(-3,3), ylim=(-2,2))
plt.axis('off')
line1, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)

def init():
    line1.set_data([], [])
    line2.set_data([], [])
    return line1, line2,

def animate(i):
    r = 0.0008*(i**2)-0.08*i+2
    x = np.linspace(-2, 2, 100)
    y = np.sqrt(r**2-x**2)
    line1.set_data(x,y)
    line2.set_data(x,-y)
    return line1, line2,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=40, blit=True)

plt.show()

When I run this, all I see is some flickering. How can I fix this? and when I see some kind of circle, the edges of the 2 functions do not connect.

EDIT
I've edited the code above. Now I get a circle that isn't close because of the use of 2 functions. Is there a way to fix this? Or is it because Matplotlib doesn't have enough time to fully plot the 2 functions?
非常小的零件丢失,大部分框架更不完整

I imagine that your circle just goes out of frame very quickly, note that the input to the function animate is the timestep in the animation, as an integer, so here it would go from 0 to 100.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM