Jerry Stackhouse
Jerry Stackhouse

Reputation: 73

How to save a pyplot figure in its maximized state

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?

Upvotes: 1

Views: 701

Answers (2)

Jerry Stackhouse
Jerry Stackhouse

Reputation: 73

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)

Upvotes: 0

Janio Lima
Janio Lima

Reputation: 91

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.

Upvotes: 1

Related Questions