【发布时间】: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