Title: Update legends in plots subclassed from animation.TimedAnimation · Issue #13252 · matplotlib/matplotlib · GitHub
Open Graph Title: Update legends in plots subclassed from animation.TimedAnimation · Issue #13252 · matplotlib/matplotlib
X Title: Update legends in plots subclassed from animation.TimedAnimation · Issue #13252 · matplotlib/matplotlib
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 official matplotlib example, where the implementation of the animated figures are done by subclassing the animation...
Open Graph 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 official matplotlib example, where the implementation of the animated figur...
X 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 official matplotlib example, where the implementation of the animated figur...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/13252
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Update legends in plots subclassed from animation.TimedAnimation","articleBody":"Hi, I want to update the legend with the latest y-value in an animated plot. The graph animation is done as the following official [matplotlib example](https://matplotlib.org/examples/animation/subplots.html), where the implementation of the animated figures are done by subclassing the animation.TimedAnimation class. To exemplify the issue I've modified the example:\r\n\r\n```\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.lines import Line2D\r\nimport matplotlib.animation as animation\r\n\r\nclass SubplotAnimation(animation.TimedAnimation):\r\n def __init__(self):\r\n fig = plt.figure()\r\n self.ax1 = fig.add_subplot(1, 2, 1)\r\n self.ax2 = fig.add_subplot(2, 2, 2)\r\n self.ax3 = fig.add_subplot(2, 2, 4)\r\n\r\n self.t = np.linspace(0, 80, 400)\r\n self.x = np.cos(2 * np.pi * self.t / 10.)\r\n self.y = np.sin(2 * np.pi * self.t / 10.)\r\n self.z = 10 * self.t\r\n\r\n self.ax1.set_xlabel('x')\r\n self.ax1.set_ylabel('y')\r\n self.line1 = Line2D([], [], color='black', label='_nolegend_')\r\n self.line1a = Line2D([], [], color='red', linewidth=2, label='_nolegend_')\r\n self.line1e = Line2D(\r\n [], [], color='red', marker='o', markeredgecolor='r', label=\"val = 0\")\r\n self.ax1.add_line(self.line1)\r\n self.ax1.add_line(self.line1a)\r\n self.ax1.add_line(self.line1e)\r\n self.ax1.set_xlim(-1, 1)\r\n self.ax1.set_ylim(-2, 2)\r\n self.ax1.set_aspect('equal', 'datalim')\r\n\r\n self.ax2.set_xlabel('y')\r\n self.ax2.set_ylabel('z')\r\n self.line2 = Line2D([], [], color='black')\r\n self.line2a = Line2D([], [], color='red', linewidth=2)\r\n self.line2e = Line2D(\r\n [], [], color='red', marker='o', markeredgecolor='r')\r\n self.ax2.add_line(self.line2)\r\n self.ax2.add_line(self.line2a)\r\n self.ax2.add_line(self.line2e)\r\n self.ax2.set_xlim(-1, 1)\r\n self.ax2.set_ylim(0, 800)\r\n\r\n self.ax3.set_xlabel('x')\r\n self.ax3.set_ylabel('z')\r\n self.line3 = Line2D([], [], color='black')\r\n self.line3a = Line2D([], [], color='red', linewidth=2)\r\n self.line3e = Line2D(\r\n [], [], color='red', marker='o', markeredgecolor='r')\r\n self.ax3.add_line(self.line3)\r\n self.ax3.add_line(self.line3a)\r\n self.ax3.add_line(self.line3e)\r\n self.ax3.set_xlim(-1, 1)\r\n self.ax3.set_ylim(0, 800)\r\n\r\n self.ax1.legend()\r\n animation.TimedAnimation.__init__(self, fig, interval=50, blit=True)\r\n\r\n def _draw_frame(self, framedata):\r\n i = framedata\r\n head = i - 1\r\n head_slice = (self.t \u003e self.t[i] - 1.0) \u0026 (self.t \u003c self.t[i])\r\n\r\n self.line1.set_data(self.x[:i], self.y[:i])\r\n self.line1a.set_data(self.x[head_slice], self.y[head_slice])\r\n self.line1e.set_data(self.x[head], self.y[head])\r\n self.ax1.get_legend().texts[0].set_text(\"val = %s\" % self.y[head])\r\n\r\n self.line2.set_data(self.y[:i], self.z[:i])\r\n self.line2a.set_data(self.y[head_slice], self.z[head_slice])\r\n self.line2e.set_data(self.y[head], self.z[head])\r\n\r\n self.line3.set_data(self.x[:i], self.z[:i])\r\n self.line3a.set_data(self.x[head_slice], self.z[head_slice])\r\n self.line3e.set_data(self.x[head], self.z[head])\r\n\r\n self._drawn_artists = [self.line1, self.line1a, self.line1e,\r\n self.line2, self.line2a, self.line2e,\r\n self.line3, self.line3a, self.line3e]\r\n\r\n def new_frame_seq(self):\r\n return iter(range(self.t.size))\r\n\r\n def _init_draw(self):\r\n lines = [self.line1, self.line1a, self.line1e,\r\n self.line2, self.line2a, self.line2e,\r\n self.line3, self.line3a, self.line3e]\r\n for l in lines:\r\n l.set_data([], [])\r\n\r\nani = SubplotAnimation()\r\n# ani.save('test_sub.mp4')\r\nplt.show()\r\n\r\n```\r\n\r\nThe 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. ","author":{"url":"https://github.com/tkgit123","@type":"Person","name":"tkgit123"},"datePublished":"2019-01-21T13:55:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/13252/matplotlib/issues/13252"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3711332f-7827-b7b6-6fbf-54ffe2d2b43e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C4F2:1CCBDE:A378C5:DC217F:6A5314A6 |
| html-safe-nonce | a4245e9a8a6e093e083f0faac041a7e365b1b9f7312f7223f2dc1ec0d8215046 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNEYyOjFDQ0JERTpBMzc4QzU6REMyMTdGOjZBNTMxNEE2IiwidmlzaXRvcl9pZCI6IjgwODY4NDY3OTM4OTcxMzU3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | efcc2d72315a4210bb0b6606652a78f299ca536965c78f20a1c2e4292e0fc3bf |
| hovercard-subject-tag | issue:401364342 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/13252/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9f21062ac483b460874f59171a2dfd305552e207cd5c8fb73fa895bbb140885b/matplotlib/matplotlib/issues/13252 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9f21062ac483b460874f59171a2dfd305552e207cd5c8fb73fa895bbb140885b/matplotlib/matplotlib/issues/13252 |
| og:image:alt | Hi, I want to update the legend with the latest y-value in an animated plot. The graph animation is done as the following official matplotlib example, where the implementation of the animated figur... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tkgit123 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width