0
如何重新繪製更新的數據,而不是從頭開始清除整個圖形和繪圖?如何僅使用Matplotlib重繪更新的數據
(我不希望這樣的事情)
到目前爲止我的代碼是類似的東西:(儘管,在這個例子中,我用我在互聯網上找到,而不是使用實際數據的一些隨機函數我使用)
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
#from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
f = plt.Figure()
class SubplotAnimation(animation.TimedAnimation):
def __init__(self):
fig= plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
par = ax1.twinx()
ax2 = fig.add_subplot(2, 1, 2)
par2 = ax2.twinx()
#********************** FUNCTIONS **************************************#
dt = 1
self.t = np.arange(0, 40, dt)
self.nse1 = np.random.randn(len(self.t)) # white noise 1
self.nse2 = np.random.randn(len(self.t)) # white noise 2
self.nse3 = np.random.randn(len(self.t)) # white noise 2
self.r = np.exp(-self.t/0.05)
cnse1 = np.convolve(self.nse1, self.r, mode='same')*dt # colored noise 1
cnse2 = np.convolve(self.nse2, self.r, mode='same')*dt # colored noise 2
cnse3 = np.convolve(self.nse3, self.r, mode='same')*dt # colored noise 2
# two signals with a coherent part and a random part
self.x = 0.01*np.sin(2*np.pi*10*self.t) + cnse1
self.y = 0.01*np.sin(2*np.pi*10*self.t) + cnse2
self.k = 0.01*np.sin(2*np.pi*10*self.t) + cnse3
self.z = 10 * self.t
ax1.set_xlabel('x')
ax1.set_ylabel('y')
par.set_ylabel("y2")
###################### LINES ############################
self.line1 = Line2D([], [], color='red', linewidth=2)
self.line1a = Line2D([], [], color='black', linewidth=1)
ax1.add_line(self.line1)
par.add_line(self.line1a)
ax1.set_xlim(0, 35)
ax1.set_ylim(-10, 10)
par.set_ylim(-10, 10)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
par2.set_ylabel("y2")
###################### LINES ############################
self.line2 = Line2D([], [], color='black')
self.line2a = Line2D([], [], color='red')
self.line2b = Line2D([], [], color='grey')
ax2.add_line(self.line2)
par2.add_line(self.line2a)
par2.add_line(self.line2b)
ax2.set_xlim(0, 35)
ax2.set_ylim(-0.5, 0.5)
par2.set_ylim(-0.3, 0.3)
animation.TimedAnimation.__init__(self, fig, interval=30, blit=True)
def _draw_frame(self, framedata):
i = framedata
self.line1.set_data(self.t[:i], self.x[:i])
self.line1a.set_data(self.t[:i], self.y[:i])
self.line2.set_data(self.t[:i], self.x[:i])
self.line2a.set_data(self.t[:i], self.y[:i])
self.line2b.set_data(self.t[:i], self.k[:i])
self._drawn_artists = [self.line1, self.line1a,
self.line2, self.line2a, self.line2b]
def new_frame_seq(self):
return iter(range(self.t.size))
def _init_draw(self):
lines = [self.line1, self.line1a,
self.line2, self.line2a, self.line2b]
for l in lines:
l.set_data([], [])
ani = SubplotAnimation()
plt.show()