1

I am trying to save a pyplot figure in its maximized form as displayed when I call plt.show() because maximizing the graph correctly displays the data, while a smaller 'windowed' version of the plot that is currently getting saved has the data incorrectly shifted/formatted.

Current code:

mng = plt.get_current_fig_manager()
mng.window.showMaximized()
plt.savefig(path + '.png', dpi=fig.dpi)
plt.show()

I use the current_fig_manager to force plot.show() to show in its maximized state which it does correctly, but plt.savefig() still saves the fig in the smaller format.

I am looking into ways to grab the dimensions of mng.window.showMaximized() in inches and then plugging that into savefig() but was wondering if there is a better approach?

2 Answers2

1

Try to config the figure size before starting the code with plt.figure(figsize=(width, height)). See bellow a example:

plt.figure(figsize=(10,6)) #10,6 is the figure dimensions
plt.plot(...)
...
plt.savefig(...)

Doing that the savefig function will use the defined dimensions.

Janio Lima
  • 61
  • 3
  • this answer did not help directly, since it didn't save maximized even when following the code above. however it gave me the idea to mess with figsize and i found the solution here: https://stackoverflow.com/a/4306340/14650757 thanks :) – Jerry Stackhouse Apr 27 '21 at 19:25
  • can you explain where you get the dimensions (10, 6) from? – Jerry Stackhouse Apr 27 '21 at 19:30
  • 1
    I used the dimensions (10,6) just as an example. What the `savefig` function does is save your figure, not the plt.show result, then when you set a desired dimension savefig uses it to create the output file. I hope that could help! – Janio Lima Apr 27 '21 at 19:52
0

Solution is create a figure and set the size in inches there, then save the figure instead of the plot.

fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
fig.savefig('test2png.png', dpi=100)