Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
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
importnumpyasnpimportmatplotlibimportmatplotlib.pyplotaspltFORCE_RESIZE_WINDOW=FalseclassFigure:size_figure_inches=10.0#< size of full figuresize_world_half=5#< size of world (centered 0)def__init__(self,fid):self.fig=plt.figure(fid)self.canvas=self.fig.canvas#--- specify figuresfi=self.size_figure_inchesself.fig.set_size_inches(sfi,sfi)self.fig.set_dpi(100)# --- specify axisself.ax=self.fig.add_axes([0,0,1,1])# < (full image)self.cla()defcla(self):ifFORCE_RESIZE_WINDOW:sfi=self.size_figure_inchesself.fig.set_size_inches(sfi,sfi)self.fig.set_dpi(100)# --- clear axis (resets limits)plt.cla()# --- set limitsswh=self.size_world_halfself.ax.set_ylim(-swh,+swh)self.ax.set_xlim(-swh,+swh)#--- setup gridself.ax.set_xticks(range(-swh,+swh))self.ax.set_yticks(range(-swh,+swh))self.ax.grid(color='k')self.ax.grid(True)defplot2(self,P):x=P[:,0]y=P[:,1]self.ax.plot(x,y)#------------------------------------------------------------------------------------------t=np.arange(0.0,2.0,0.01)s=1+np.sin(2*np.pi*t)x=np.cos(2*np.pi*t)y=np.sin(2*np.pi*t)M_restpose=np.vstack([x,y]).transpose()print("backend: ",matplotlib.get_backend() )fig=Figure(1)offs=np.arange(-1,+1,.25)fori,offinenumerate(offs):fig.cla()fig.plot2(M_restpose)fig.fig.savefig("./test%d.png"%i)plt.pause(.1)print("Figure size for test%d "%i,fig.fig.get_size_inches())
Actual outcome
backend: TkAggFigure size for test0 [10. 9.66]Figure size for test1 [10. 9.32]Figure size for test2 [10. 8.98]Figure size for test3 [10. 8.64]Figure size for test4 [10. 8.3]Figure size for test5 [10. 7.96]Figure size for test6 [10. 7.62]Figure size for test7 [10. 7.28]
Below the images generated by test0 and test7
Expected outcome
Image size shouldnot change.
Result can be achieved by setting FORCE_RESIZE_WINDOW=True in the code above.
The terminal output is then:
backend: TkAggFigure size for test0 [10. 9.66]Figure size for test1 [10. 9.66]Figure size for test2 [10. 9.66]Figure size for test3 [10. 9.66]Figure size for test4 [10. 9.66]Figure size for test5 [10. 9.66]Figure size for test6 [10. 9.66]Figure size for test7 [10. 9.66]
While the script reports 9.66in in height, note the outputted files are correctly 1000x1000 as shown below:
Matplotlib version
- Operating system: debian
- Matplotlib version: 2.1.2 (installed w/ pip)
- Matplotlib backend (
print(matplotlib.get_backend())
): TkAgg - Python version: 3.5
- Jupyter version (if applicable): N/A
- Other libraries:
Metadata
Metadata
Assignees
Labels
No labels