本文介绍了绘制图形,清除其坐标轴,然后绘制新图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行以下操作:创建图形,在其上绘制图形,然后在3秒钟内清除其轴.发生这种情况时,应在同一图形上绘制新图形,并在屏幕上更新.
I'm trying to do the following: create a figure, plot a graph on it, then in 3 seconds clear its axes. When that happen a new graph should be plotted on the same figure and it should be updated on the screen.
类似的东西:
import matplotlib.pyplot as plt
import time
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3],[1,2,3])
plt.show()
time.sleep(3)
plt.ion()
plt.cla()
ax.plot([10,20,30],[10,20,30])
fig.canvas.draw()
但它不起作用.这个逻辑有什么问题?
But it isn't working. What's wrong with this logic?
推荐答案
如果要对图形进行动画处理,可以使用 matplotlib.animation 库.您的代码如下所示:
If you want to animate your figures, you can use matplotlib.animation library.Here is what your code would look like:
import matplotlib.pyplot as plt
import time
import matplotlib.animation as animation
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([],[])
ax.set_xlim(3)
ax.set_ylim(3)
line.set_data([1,2,3],[1,2,3])
def init():
""" Initializes the plots to have zero values."""
line.set_data([],[])
return line,
def animate(n, *args, **kwargs):
if(n%2==0):
line.set_data([],[])
else:
line.set_data([1,2,3],[1,2,3])
return line,
ani = animation.FuncAnimation(fig, animate, init_func=init,frames =100, interval=10, blit=False, repeat =False)
fig.show()
查看matplotlib.animation以获得更多详细信息.此链接可以帮助您入门.
Look into matplotlib.animation for more details. This link can get you started.
这篇关于绘制图形,清除其坐标轴,然后绘制新图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!