Matplotlib图形facecolor(背景颜色)
有人可以解释一下为什么下面的代码在设置图形的面部颜色时不起作用吗?
import matplotlib.pyplot as plt
# create figure instance
fig1 = plt.figure(1)
fig1.set_figheight(11)
fig1.set_figwidth(8.5)
rect = fig1.patch
rect.set_facecolor('red') # works with plt.show().
# Does not work with plt.savefig("trial_fig.png")
ax = fig1.add_subplot(1,1,1)
x = 1, 2, 3
y = 1, 4, 9
ax.plot(x, y)
# plt.show() # Will show red face color set above using rect.set_facecolor('red')
plt.savefig("trial_fig.png") # The saved trial_fig.png DOES NOT have the red facecolor.
# plt.savefig("trial_fig.png", facecolor='red') # Here the facecolor is red.
当我使用 fig1.set_figheight(11)
fig1.set_figwidth(8.5)
指定图形的高度和宽度时,这些由命令 plt.savefig 拾取(“Trial_fig.png”)
。但是,不会选择面部颜色设置。为什么?
感谢您的帮助。
Can someone please explain why the code below does not work when setting the facecolor of the figure?
import matplotlib.pyplot as plt
# create figure instance
fig1 = plt.figure(1)
fig1.set_figheight(11)
fig1.set_figwidth(8.5)
rect = fig1.patch
rect.set_facecolor('red') # works with plt.show().
# Does not work with plt.savefig("trial_fig.png")
ax = fig1.add_subplot(1,1,1)
x = 1, 2, 3
y = 1, 4, 9
ax.plot(x, y)
# plt.show() # Will show red face color set above using rect.set_facecolor('red')
plt.savefig("trial_fig.png") # The saved trial_fig.png DOES NOT have the red facecolor.
# plt.savefig("trial_fig.png", facecolor='red') # Here the facecolor is red.
When I specify the height and width of the figure using fig1.set_figheight(11)
fig1.set_figwidth(8.5)
these are picked up by the command plt.savefig("trial_fig.png")
. However, the facecolor setting is not picked up. Why?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我必须使用透明关键字来获取我用首字母选择的颜色,
如下所示:
I had to use the transparent keyword to get the color I chose with my initial
like this:
savefig
有自己的facecolor
参数。我认为比接受的答案更简单的方法是全局设置它们仅一次,而不是每次都放置
facecolor=fig.get_facecolor()
:savefig
has its own parameter forfacecolor
.I think an even easier way than the accepted answer is to set them globally just once, instead of putting
facecolor=fig.get_facecolor()
every time:这是因为
savefig
覆盖了图形背景的面色。(实际上,这是故意的......假设您可能希望使用
facecolor
kwarg 到savefig
来控制保存的图形的背景颜色。这是一个不过,令人困惑且不一致的默认值!)最简单的解决方法就是执行
fig.savefig('whatever.png', facecolor=fig.get_facecolor(), edgecolor='none')
(我是此处指定边缘颜色,因为实际图形的默认边缘颜色是白色,这将为您在保存的图形周围提供白色边框)It's because
savefig
overrides the facecolor for the background of the figure.(This is deliberate, actually... The assumption is that you'd probably want to control the background color of the saved figure with the
facecolor
kwarg tosavefig
. It's a confusing and inconsistent default, though!)The easiest workaround is just to do
fig.savefig('whatever.png', facecolor=fig.get_facecolor(), edgecolor='none')
(I'm specifying the edgecolor here because the default edgecolor for the actual figure is white, which will give you a white border around the saved figure)只需将
facecolor='red'
添加到plt.savefig()
就足够了。例如:Just adding the
facecolor='red'
to theplt.savefig()
is sufficient. For example: