【问题标题】:Python Matplotlib doesn't show figure facecolor when saving figure / plot保存图形/绘图时,Python Matplotlib 不显示图形 facecolor
【发布时间】:2020-07-16 04:48:14
【问题描述】:

我目前在保存颜色和 matplotlib 时遇到问题。当我运行以下代码时,我得到了预期的结果

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')
fig.patch.set_facecolor('xkcd:mint green')

plt.show()

这是显示的屏幕截图,这是我所期待的结果。

但是,当我通过 python 或手动单击图上的保存来运行保存功能时,它会生成以下图像,没有彩色边框。

重现上面图片的代码

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')
fig.patch.set_facecolor('xkcd:mint green')

# Also doesn't work with fig.savefig
plt.savefig("test.png", dpi=200)

【问题讨论】:

  • savefig 有一个明确的参数来设置 facecolor。应该重新设置。原因可能是当将图像保存为要打印的文档的一部分时,通常首选白色背景。 docs

标签: python python-3.x matplotlib graph data-visualization


【解决方案1】:

@JohanC 在 cmets 中指出,需要进行面部颜色争论。

固定代码

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('black')
plt.title("test")
ax = plt.gca()
ax.patch.set_facecolor('black')

# Also doesn't work with fig.savefig
plt.savefig("test.png", facecolor='xkcd:mint green', dpi=200)

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2021-10-14
    • 2011-06-15
    • 2011-07-14
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多