Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Closed
Description
Hi, I want to update the legend with the latest y-value in an animated plot. The graph animation is done as the following officialmatplotlib example, where the implementation of the animated figures are done by subclassing the animation.TimedAnimation class. To exemplify the issue I've modified the example:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.lines import Line2Dimport matplotlib.animation as animationclass SubplotAnimation(animation.TimedAnimation): def __init__(self): fig = plt.figure() self.ax1 = fig.add_subplot(1, 2, 1) self.ax2 = fig.add_subplot(2, 2, 2) self.ax3 = fig.add_subplot(2, 2, 4) self.t = np.linspace(0, 80, 400) self.x = np.cos(2 * np.pi * self.t / 10.) self.y = np.sin(2 * np.pi * self.t / 10.) self.z = 10 * self.t self.ax1.set_xlabel('x') self.ax1.set_ylabel('y') self.line1 = Line2D([], [], color='black', label='_nolegend_') self.line1a = Line2D([], [], color='red', linewidth=2, label='_nolegend_') self.line1e = Line2D( [], [], color='red', marker='o', markeredgecolor='r', label="val = 0") self.ax1.add_line(self.line1) self.ax1.add_line(self.line1a) self.ax1.add_line(self.line1e) self.ax1.set_xlim(-1, 1) self.ax1.set_ylim(-2, 2) self.ax1.set_aspect('equal', 'datalim') self.ax2.set_xlabel('y') self.ax2.set_ylabel('z') self.line2 = Line2D([], [], color='black') self.line2a = Line2D([], [], color='red', linewidth=2) self.line2e = Line2D( [], [], color='red', marker='o', markeredgecolor='r') self.ax2.add_line(self.line2) self.ax2.add_line(self.line2a) self.ax2.add_line(self.line2e) self.ax2.set_xlim(-1, 1) self.ax2.set_ylim(0, 800) self.ax3.set_xlabel('x') self.ax3.set_ylabel('z') self.line3 = Line2D([], [], color='black') self.line3a = Line2D([], [], color='red', linewidth=2) self.line3e = Line2D( [], [], color='red', marker='o', markeredgecolor='r') self.ax3.add_line(self.line3) self.ax3.add_line(self.line3a) self.ax3.add_line(self.line3e) self.ax3.set_xlim(-1, 1) self.ax3.set_ylim(0, 800) self.ax1.legend() animation.TimedAnimation.__init__(self, fig, interval=50, blit=True) def _draw_frame(self, framedata): i = framedata head = i - 1 head_slice = (self.t > self.t[i] - 1.0) & (self.t < self.t[i]) self.line1.set_data(self.x[:i], self.y[:i]) self.line1a.set_data(self.x[head_slice], self.y[head_slice]) self.line1e.set_data(self.x[head], self.y[head]) self.ax1.get_legend().texts[0].set_text("val = %s" % self.y[head]) self.line2.set_data(self.y[:i], self.z[:i]) self.line2a.set_data(self.y[head_slice], self.z[head_slice]) self.line2e.set_data(self.y[head], self.z[head]) self.line3.set_data(self.x[:i], self.z[:i]) self.line3a.set_data(self.x[head_slice], self.z[head_slice]) self.line3e.set_data(self.x[head], self.z[head]) self._drawn_artists = [self.line1, self.line1a, self.line1e, self.line2, self.line2a, self.line2e, self.line3, self.line3a, self.line3e] def new_frame_seq(self): return iter(range(self.t.size)) def _init_draw(self): lines = [self.line1, self.line1a, self.line1e, self.line2, self.line2a, self.line2e, self.line3, self.line3a, self.line3e] for l in lines: l.set_data([], [])ani = SubplotAnimation()# ani.save('test_sub.mp4')plt.show()The legeend will not update unless the self._drawn_artists is commented out. But I want to keep using that inherited method otherwise the window/animation is lagging seriously.