Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib.savefig ignoring axes and plotting black border around image

I have a matplotlib Figure embedded in a QtAggFigureCanvas (PyQt4) with titles and axis labels (example shown below). enter image description here

I implemented a button to save the figure to a png file. The figure was created with:

self.plkDpi = 100
self.plkFigure = Figure(dpi=self.plkDpi)
...
self.plkAxes = self.plkFig.add_subplot(111)
...
self.plkAxes.set_xlabel(...)
self.plkAxes.set_ylabel(...)
self.plkAxes.set_title(...)

When I hit my save button, the following code is executed:

self.plkFig.savefig('tmp.png', bbox_inches='tight', dpi=self.plkDpi)

For some reason, the axes and plot title are omitted from the final plot. But they are not cropped - there is a bounding black box around the figure that is just blank (see below)

enter image description here

No matter what I try, changing figsize, dpi, bounding box, etc. I cannot get the figure to save with the axis labels.

like image 867
jcolen19 Avatar asked Jan 28 '23 13:01

jcolen19


1 Answers

Please, take a look at this link: Black background behind a figure's labels and ticks, only after saving figure but not in Python Interactive view (VS Code with Jupyter functionality)?.

It seems plt.savefig() overwrites plot parameters. So you must define them again. Try this: plt.savefig('yourfilenamehere.png', facecolor='w'). This will set the borders in white.

Best regards,

like image 118
Gustavo Mirapalheta Avatar answered Jan 31 '23 21:01

Gustavo Mirapalheta