Title: fig.savefig alters the dimensions of the figure · Issue #10560 · matplotlib/matplotlib · GitHub
Open Graph Title: fig.savefig alters the dimensions of the figure · Issue #10560 · matplotlib/matplotlib
X Title: fig.savefig alters the dimensions of the figure · Issue #10560 · matplotlib/matplotlib
Description: Bug report Bug summary fig.savefig(...) changes the size of the figure it is trying to save to file minor note: problem doesn't occur when size_figure_inches<=5 Code for reproduction import numpy as np import matplotlib import matplotlib...
Open Graph Description: Bug report Bug summary fig.savefig(...) changes the size of the figure it is trying to save to file minor note: problem doesn't occur when size_figure_inches<=5 Code for reproduction import numpy a...
X Description: Bug report Bug summary fig.savefig(...) changes the size of the figure it is trying to save to file minor note: problem doesn't occur when size_figure_inches<=5 Code for reproduction import ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/10560
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"fig.savefig alters the dimensions of the figure","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\n\r\n**Bug summary**\r\nfig.savefig(...) changes the size of the figure it is trying to save to file \r\nminor note: problem doesn't occur when size_figure_inches\u003c=5\r\n\r\n**Code for reproduction**\r\n\r\n```python\r\nimport numpy as np\r\nimport matplotlib\r\nimport matplotlib.pyplot as plt\r\n\r\nFORCE_RESIZE_WINDOW = False\r\n\r\nclass Figure:\r\n size_figure_inches = 10.0 #\u003c size of full figure\r\n size_world_half = 5 #\u003c size of world (centered 0)\r\n\r\n def __init__(self, fid):\r\n self.fig = plt.figure(fid)\r\n self.canvas = self.fig.canvas\r\n\r\n #--- specify figure\r\n sfi = self.size_figure_inches\r\n self.fig.set_size_inches(sfi, sfi)\r\n self.fig.set_dpi(100)\r\n\r\n # --- specify axis\r\n self.ax = self.fig.add_axes([0, 0, 1, 1]) # \u003c (full image)\r\n self.cla()\r\n\r\n def cla(self):\r\n if FORCE_RESIZE_WINDOW:\r\n sfi = self.size_figure_inches\r\n self.fig.set_size_inches(sfi, sfi)\r\n self.fig.set_dpi(100)\r\n\r\n # --- clear axis (resets limits)\r\n plt.cla()\r\n # --- set limits\r\n swh = self.size_world_half\r\n self.ax.set_ylim(-swh, +swh)\r\n self.ax.set_xlim(-swh, +swh)\r\n\r\n #--- setup grid\r\n self.ax.set_xticks(range(-swh, +swh))\r\n self.ax.set_yticks(range(-swh, +swh))\r\n self.ax.grid(color='k')\r\n self.ax.grid(True)\r\n\r\n def plot2(self, P):\r\n x = P[:, 0]\r\n y = P[:, 1]\r\n self.ax.plot(x, y)\r\n\r\n#------------------------------------------------------------------------------------------\r\n\r\nt = np.arange(0.0, 2.0, 0.01)\r\ns = 1 + np.sin(2*np.pi*t)\r\nx = np.cos(2*np.pi*t)\r\ny = np.sin(2*np.pi*t)\r\nM_restpose = np.vstack([x,y]).transpose()\r\n\r\nprint(\"backend: \", matplotlib.get_backend() )\r\n\r\nfig = Figure(1)\r\noffs = np.arange(-1, +1, .25)\r\nfor i, off in enumerate(offs):\r\n fig.cla()\r\n fig.plot2(M_restpose)\r\n fig.fig.savefig(\"./test%d.png\" % i)\r\n plt.pause(.1)\r\n print(\"Figure size for test%d \" % i, fig.fig.get_size_inches())\r\n```\r\n\r\n**Actual outcome**\r\n\r\n```\r\nbackend: TkAgg\r\nFigure size for test0 [10. 9.66]\r\nFigure size for test1 [10. 9.32]\r\nFigure size for test2 [10. 8.98]\r\nFigure size for test3 [10. 8.64]\r\nFigure size for test4 [10. 8.3]\r\nFigure size for test5 [10. 7.96]\r\nFigure size for test6 [10. 7.62]\r\nFigure size for test7 [10. 7.28]\r\n```\r\n\r\nBelow the images generated by test0 and test7\r\n\r\n\r\n\r\n**Expected outcome**\r\n\r\nImage size should **not** change.\r\nResult can be achieved by setting FORCE_RESIZE_WINDOW=True in the code above.\r\nThe terminal output is then:\r\n```\r\nbackend: TkAgg\r\nFigure size for test0 [10. 9.66]\r\nFigure size for test1 [10. 9.66]\r\nFigure size for test2 [10. 9.66]\r\nFigure size for test3 [10. 9.66]\r\nFigure size for test4 [10. 9.66]\r\nFigure size for test5 [10. 9.66]\r\nFigure size for test6 [10. 9.66]\r\nFigure size for test7 [10. 9.66]\r\n```\r\nWhile the script reports 9.66in in height, note the outputted files are correctly 1000x1000 as shown below:\r\n\r\n\r\n\r\n**Matplotlib version**\r\n * Operating system: debian\r\n * Matplotlib version: 2.1.2 (installed w/ pip)\r\n * Matplotlib backend (`print(matplotlib.get_backend())`): TkAgg\r\n * Python version: 3.5\r\n * Jupyter version (if applicable): N/A\r\n * Other libraries: \r\n","author":{"url":"https://github.com/taiya","@type":"Person","name":"taiya"},"datePublished":"2018-02-21T19:43:10.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/10560/matplotlib/issues/10560"}
| 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:7fa02823-1bae-84a9-af7b-00b31ecf8d13 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED42:24C7F8:273D6B5:35CDB25:6A543F65 |
| html-safe-nonce | 51e37d42e9318b52226aa70ea8bdc02e88f55a3f2524780a60784cc6f6032cd0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDQyOjI0QzdGODoyNzNENkI1OjM1Q0RCMjU6NkE1NDNGNjUiLCJ2aXNpdG9yX2lkIjoiMzE0NDg5NzUzOTc5MjQ1MzQ3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c5aaab9cb32504df657827e3e08253e840f008ae16085dca85e1275ef1d29ce2 |
| hovercard-subject-tag | issue:299102309 |
| 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/10560/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec8bcba93a5c3c23a940005e5a9cbb035ef086955f089e8cb3df7c1225186c43/matplotlib/matplotlib/issues/10560 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec8bcba93a5c3c23a940005e5a9cbb035ef086955f089e8cb3df7c1225186c43/matplotlib/matplotlib/issues/10560 |
| og:image:alt | Bug report Bug summary fig.savefig(...) changes the size of the figure it is trying to save to file minor note: problem doesn't occur when size_figure_inches<=5 Code for reproduction import numpy a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | taiya |
| 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