[英]Draggable Matplotlib scatter with an animated plot using blitting
[英]Python, Matplotlib : Plot disappear when blitting is on
如標題所描述的那樣,我的plot在選項打開時消失了。 我進一步解釋一下:我正在制作動畫來顯示一些微分方程的解,代碼會變得越來越重。 我需要 blitting 選項來獲得平滑的 animation,但我還需要一個按鈕來啟動/停止 animation。 我正在使用 FuncAnimation。 問題是當我用“myAnimation.event_source.stop()”命令停止 animation 時,只要 Z6F1C25ED1523962F1BBF9DEE_9BE5092BZ 處於暫停狀態,plot 就會消失。 我試圖在 matplotlib 文檔中找到問題: https://matplotlib.org/3.2.1/_modules/matplotlib/animation.html#FuncAnimation但這對我來說太多了,無法修改可以修改的內容。 你知道如何解決我的問題嗎? 代碼:(funcanimation部分和停止按鈕部分,A是我的具體代碼的矩陣)
def update(self,i):
self.myAnimation.event_source.interval = self.Constants['interv']
self.k = i%10
self.n[:,self.k] = self.A*self.n[:,self.k-1]
self.p.set_ydata(self.n[:,self.k])
return self.p,
def _stopp(self,event):
if self.Launch:
self.myAnimation = aniamtion.FuncAnimation(self.fig, self.update, frames=range(1,self.Constants['N']), interval=self.Constants['interv'],repeat=False)
self.Launch=False
else:
if self.anim_running:
self.myAnimation.event_source.stop()
self.anim_running = False
else:
self.myAnimation.event_source.start()
self.anim_running = True
def add_button(self,left,name):
axbutton=plt.axes([left,0.88, 0.12, 0.05])
bstop = Button(axbutton, name)
self.Launch=True
self.Button.append(bstop)
Matplotlib 的好心開發者回答了我。 " https://matplotlib.org/3.3.0/tutorials/advanced/blitting.html#sphx-glr-tutorials-advanced-blitting-py可能在這里有用。
問題是,當使用 bliting 時,我們通過 obj.set_animated(True) 將藝術家標記為“動畫”,這意味着它們被排除在正常的繪制過程之外(這樣您可以獲得“干凈”的背景)。 在 FuncAnimation 中,我們啟用此功能(只是為了安全起見)以防止先前數據“卡”在 animation 中的偽影。 當您暫停 animation 時,您不會取消設置此 state,因此當圖形重繪時,它會跳過渲染圖(因為這是管理 Z6F1C25ED1523962F1BBF9DEE9BE59 進行渲染的問題)。
初始代碼:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class plotanimation:
def __init__(self):
self.fig,self.ax=plt.subplots()
self.x=np.linspace(-10,10,1000)
self.N=200
self.interv=50
self.n0=1./(4*np.pi*2e-4*0.1)**0.5 * np.exp(-self.x**2/(4*2e-4*0.1))
self.p,=self.ax.plot(self.x,self.n0)
self.anim_running = True
self.Myanimation=animation.FuncAnimation(self.fig, self.update,frames=self.N,interval=self.interv,blit=True)
def update(self,i):
self.n0+=i/100
self.p.set_ydata(self.n0)
return self.p,
def animate(self):
pause_ax = self.fig.add_axes((0.7, 0.025, 0.1, 0.04))
pause_button = Button(pause_ax, 'pause', hovercolor='0.975')
pause_button.on_clicked(self._pause)
plt.show()
def _pause(self, event):
if self.anim_running:
self.Myanimation.event_source.stop()
self.anim_running = False
else:
self.Myanimation.event_source.start()
self.anim_running = True
animated_plot = plotanimation()
animated_plot.animate()
解決方案(注意 self.p.set_animated(False/True)):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Button
class PlotAnimation:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.x = np.linspace(-10, 10, 1000)
self.N = 200
self.interv = 50
self.n0 = (
1.0
/ (4 * np.pi * 2e-4 * 0.1) ** 0.5
* np.exp(-self.x ** 2 / (4 * 2e-4 * 0.1))
)
(self.p,) = self.ax.plot(self.x, self.n0)
self.anim_running = True
self.Myanimation = animation.FuncAnimation(
self.fig, self.update, frames=self.N, interval=self.interv, blit=True
)
def update(self, i):
self.n0 += i / 100 % 5
self.p.set_ydata(self.n0 % 20)
return (self.p,)
def animate(self):
pause_ax = self.fig.add_axes((0.7, 0.025, 0.1, 0.04))
pause_button = Button(pause_ax, "pause", hovercolor="0.975")
pause_button.on_clicked(self._pause)
plt.show()
def _pause(self, event):
if self.anim_running:
self.Myanimation.event_source.stop()
self.p.set_animated(False)
self.anim_running = False
self.fig.canvas.draw_idle()
else:
self.p.set_animated(True)
self.Myanimation.event_source.start()
self.anim_running = True
animated_plot = PlotAnimation()
animated_plot.animate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.