简体   繁体   English

更改背景颜色后如何保存 Matplotlib 图形?

[英]How can I save a Matplotlib figure after changing the background color?

Using the Spyder IDE, I have created a matplotlib plot and changed the face (background) color of both the figure object and the axes object to black.使用 Spyder IDE,我创建了一个 matplotlib 图并将图形对象和轴对象的面(背景)颜色更改为黑色。 When I try to save the figure using plt.savefig(...) the axes, title, and axes label are not included.当我尝试使用plt.savefig(...)保存图形时,不包括轴、标题和轴标签。

I have tried implementing the standard advice of adding bbox_inches='tight' to the plt.savefig() function for when the axes are cut off:我已经尝试在轴被切断时实现将bbox_inches='tight'添加到plt.savefig()函数的标准建议

plt.savefig("my_fig_name.png", bbox_inches='tight')

To no avail.无济于事。 Others suggested that I change the plotting method to "inline" from "automatic" within either Jupyter Notebook or Spyder. 其他人建议我在 Jupyter Notebook 或 Spyder 中将绘图方法从“自动”更改为“内联”。 This had no effect.这没有效果。 I also tried to make sure there was enough room in the figure for my axes using:我还尝试使用以下方法确保图中有足够的空间容纳我的轴:

fig.add_axes([0.1,0.1,0.75,0.75])

This does not work either.这也不起作用。 Below is enough to reproduce my experience.以下足以重现我的经验。

import matplotlib.pyplot as plt

xs, ys = [0,1], [0,1]

fig = plt.figure(figsize=(6, 6)) # Adding tight_layout=True has no effect
ax = fig.add_subplot(1, 1, 1)

# When the following block is commented out, the color of the 
# plot is unchanged and the plt.savefig function works perfectly

fig.patch.set_facecolor("#121111")
ax.set_facecolor("#121111")
ax.spines['top'].set_color("#121111")
ax.spines['right'].set_color("#121111")
ax.spines['bottom'].set_color('white')
ax.spines['left'].set_color('white')
ax.xaxis.label.set_color('white')
ax.tick_params(axis='x', colors='white')
ax.yaxis.label.set_color('white')
ax.tick_params(axis='y', colors='white')

ax.set_title("My Graph's Title", color="white")

plt.plot(xs, ys)
plt.xlabel("x-label")
plt.ylabel("y-label")

plt.savefig("my_fig_name.png", bbox_inches="tight")

I am expecting to get an image like this:我期待得到这样的图像:

What I Expect to Get我期望得到什么

However, plt.savefig(...) gives me the following result:但是, plt.savefig(...)给了我以下结果:

What I Actually Get我实际得到的

Curiously, there seems to be white space around the plot which does not disappear even when I add the tight_layout=True parameter to the matplotlib figure constructor.奇怪的是,即使我向 matplotlib 图形构造函数添加了tight_layout=True参数,绘图周围似乎还有空白区域也不会消失。

fig = plt.figure(figsize=(6, 6), tight_layout=True)

And, when I comment out the code which changes the face color of the plot, the figure is saved correctly with all the axes and labels displayed correctly.而且,当我注释掉改变绘图面颜色的代码时,图形会正确保存,所有轴和标签都正确显示。

In order to solve your problem, you just have to specify the facecolor keyword argument to your plt.savefig call, in this case :为了解决您的问题,您只需在plt.savefig调用中指定facecolor关键字参数,在这种情况下:

plt.savefig("my_fig_name.png", bbox_inches="tight", facecolor="#121111")

which gives the intended .png output :这给出了预期的.png输出:

正确输出

For more information, see plt.savefig documentation .有关更多信息,请参阅plt.savefig 文档

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

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