Title: pgf backend no longer supports fig.draw · Issue #18407 · matplotlib/matplotlib · GitHub
Open Graph Title: pgf backend no longer supports fig.draw · Issue #18407 · matplotlib/matplotlib
X Title: pgf backend no longer supports fig.draw · Issue #18407 · matplotlib/matplotlib
Description: Bug report Because of the cleanups in #15977, the pgf backend no longer supports fig.draw(fig.canvas.get_renderer()) to trigger a draw. If this is not supposed to be a bug and is the correct expected behavior, is there a cleaner way to t...
Open Graph Description: Bug report Because of the cleanups in #15977, the pgf backend no longer supports fig.draw(fig.canvas.get_renderer()) to trigger a draw. If this is not supposed to be a bug and is the correct expect...
X Description: Bug report Because of the cleanups in #15977, the pgf backend no longer supports fig.draw(fig.canvas.get_renderer()) to trigger a draw. If this is not supposed to be a bug and is the correct expect...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/18407
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"pgf backend no longer supports fig.draw","articleBody":"\u003c!--To help us understand and resolve your issue, please fill out the form to the best of your ability.--\u003e\r\n\u003c!--You can feel free to delete the sections that do not apply.--\u003e\r\n\r\n### Bug report\r\nBecause of the cleanups in #15977, the pgf backend no longer supports `fig.draw(fig.canvas.get_renderer())` to trigger a draw.\r\n\r\nIf this is not supposed to be a bug and is the correct expected behavior, is there a cleaner way to trigger a draw without using `fig.savefig`?\r\n\r\n**Bug summary**\r\n\r\n\u003c!--A short 1-2 sentences that succinctly describes the bug--\u003e\r\n\r\n**Code for reproduction**\r\n\r\n\u003c!--A minimum code snippet required to reproduce the bug.\r\nPlease make sure to minimize the number of dependencies required, and provide\r\nany necessary plotted data.\r\nAvoid using threads, as Matplotlib is (explicitly) not thread-safe.--\u003e\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\nplt.switch_backend('pgf') # Switching to the PGF backend\r\nfig, ax = plt.subplots(figsize=(6, 3.0), ncols=2, nrows=1,\r\n constrained_layout=True, sharey=True)\r\nfig.set_constrained_layout_pads(wspace=0.03)\r\n\r\nax[0].plot(np.arange(4), 'r', label=r'Really longgggg label 1')\r\nax[1].plot(np.arange(5), 'b', label=r'Really longgggg label 2')\r\n(leg_h0, leg_lab0) = ax[0].get_legend_handles_labels()\r\n(leg_h1, leg_lab1) = ax[1].get_legend_handles_labels()\r\n\r\n# add a legend to one of the axes to make space at the top of the figure\r\nleg0 = ax[0].legend(leg_h0, leg_lab0, loc='lower center',\r\n bbox_to_anchor=(0.5, 1.07))\r\n\r\nleg0.set_in_layout(True) # force to steal space from top of the figure\r\n# ------------------------------------------------------\r\n# Trigger a draw (required for constrained_layout to work)\r\n# fig.canvas.draw() # Doesn't work since pgf is a non-interactive backend\r\nfig.draw(fig.canvas.get_renderer()) # used to work before #15977\r\n# ------------------------------------------------------\r\n# Thanks constrained_layout, your work is done! turning you off now\r\nfig.set_constrained_layout(False)\r\n\r\n# Remove legend from axes and add a figure legend\r\n# which includes entries from both axes\r\nleg0.remove() \r\nfig_leg = fig.legend(*zip(leg_h0, leg_h1), *zip(leg_lab0, leg_lab1),\r\n loc='lower center', ncol=2, columnspacing=5,\r\n bbox_to_anchor=(0.5, 0.85))\r\nfig.savefig('test.png', dpi=200)\r\n```\r\n\r\n**Actual outcome**\r\n\r\n\u003c!--The output produced by the above code, which may be a screenshot, console output, etc.--\u003e\r\n\r\n``` \r\nTraceback (most recent call last):\r\n File \"test.py\", line 22, in \u003cmodule\u003e\r\n fig.draw(fig.canvas.get_renderer()) # works!\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\artist.py\", line 41, in draw_wrapper\r\n return draw(artist, renderer, *args, **kwargs)\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\figure.py\", line 1862, in draw\r\n self.patch.draw(renderer)\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\artist.py\", line 41, in draw_wrapper\r\n return draw(artist, renderer, *args, **kwargs)\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\patches.py\", line 587, in draw\r\n draw_path(tpath, affine,\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\backends\\backend_pgf.py\", line 475, in draw_path\r\n writeln(self.fh, r\"\\begin{pgfscope}\")\r\n File \"---\\Miniconda3\\lib\\site-packages\\matplotlib\\backends\\backend_pgf.py\", line 129, in writeln\r\n fh.write(line)\r\nAttributeError: 'NoneType' object has no attribute 'write'\r\n```\r\n\r\n**Expected outcome**\r\n\r\n\r\n\u003c!--A description of the expected outcome from the code snippet--\u003e\r\n\u003c!--If this used to work in an earlier version of Matplotlib, please note the version it used to work on--\u003e\r\n\r\n**Matplotlib version**\r\n\u003c!--Please specify your platform and versions of the relevant libraries you are using:--\u003e\r\n * Operating system: Windows 10\r\n * Matplotlib version: 3.3.1\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): pgf\r\n * Python version: 3.8.5\r\n * Jupyter version (if applicable):\r\n * Other libraries: \r\n\r\n\u003c!--Please tell us how you installed matplotlib and python e.g., from source, pip, conda--\u003e\r\n\u003c!--If you installed from conda, please specify which channel you used if not the default--\u003e\r\n\r\n","author":{"url":"https://github.com/pharshalp","@type":"Person","name":"pharshalp"},"datePublished":"2020-09-04T15:02:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/18407/matplotlib/issues/18407"}
| 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:70592c65-df1f-b52d-d79a-c879ea5846c4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB32:12305D:95E7AE:C99388:6A52914F |
| html-safe-nonce | 9c774d4d8f7f9fe119d902a3285ddc069334ef2114caff85467da105969e6c8f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjMyOjEyMzA1RDo5NUU3QUU6Qzk5Mzg4OjZBNTI5MTRGIiwidmlzaXRvcl9pZCI6IjE1NjY5MDY2Njk1NDMzNjI4OTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a3283cbd5af7e86d5ecd595ce371d2cb364ceed44238b8be56e6b81da3b91175 |
| hovercard-subject-tag | issue:693267017 |
| 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/18407/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5cab2c3d61bb0a1350e636dc39fbf3f038e8a3d3a8bb9db14bd893bfd0037d3a/matplotlib/matplotlib/issues/18407 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5cab2c3d61bb0a1350e636dc39fbf3f038e8a3d3a8bb9db14bd893bfd0037d3a/matplotlib/matplotlib/issues/18407 |
| og:image:alt | Bug report Because of the cleanups in #15977, the pgf backend no longer supports fig.draw(fig.canvas.get_renderer()) to trigger a draw. If this is not supposed to be a bug and is the correct expect... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pharshalp |
| 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