简体   繁体   English

Matplotlib.savefig忽略轴并在图像周围绘制黑色边框

[英]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). 我在带有标题和轴标签的QtAggFigureCanvas(PyQt4)中嵌入了一个matplotlib图(示例如下所示)。 在此处输入图片说明

I implemented a button to save the figure to a png file. 我实现了一个按钮来将图形保存到png文件中。 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) 但是它们没有被裁剪-在图形周围有一个边界黑框,只是空白(请参阅下文)

在此处输入图片说明

No matter what I try, changing figsize, dpi, bounding box, etc. I cannot get the figure to save with the axis labels. 无论我如何尝试,都要更改figsize,dpi,边界框等。我无法将图形与轴标签一起保存。

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)? 请查看以下链接: 仅在保存图形之后但不在Python交互式视图(具有Jupyter功能的VS Code)中,图形的标签和刻度后面的黑色背景? .

It seems plt.savefig() overwrites plot parameters. 看来plt.savefig()会覆盖绘图参数。 So you must define them again. 因此,您必须再次定义它们。 Try this: plt.savefig('yourfilenamehere.png', facecolor='w'). 试试这个:plt.savefig('yourfilenamehere.png',facecolor ='w')。 This will set the borders in white. 这会将边框设置为白色。

Best regards, 最好的祝福,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM