Matplotlib图形facecolor(背景颜色)

发布于 2024-10-14 01:17:21 字数 842 浏览 0 评论 0原文

有人可以解释一下为什么下面的代码在设置图形的面部颜色时不起作用吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为你拒绝所有暧昧 2024-10-21 01:17:21

我必须使用透明关键字来获取我用首字母选择的颜色,

fig=figure(facecolor='black')

如下所示:

savefig('figname.png', facecolor=fig.get_facecolor(), transparent=True)

I had to use the transparent keyword to get the color I chose with my initial

fig=figure(facecolor='black')

like this:

savefig('figname.png', facecolor=fig.get_facecolor(), transparent=True)
以为你会在 2024-10-21 01:17:21

savefig 有自己的 facecolor 参数。
我认为比接受的答案更简单的方法是全局设置它们仅一次,而不是每次都放置facecolor=fig.get_facecolor()

plt.rcParams['axes.facecolor']='red'
plt.rcParams['savefig.facecolor']='red'

savefig has its own parameter for facecolor.
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:

plt.rcParams['axes.facecolor']='red'
plt.rcParams['savefig.facecolor']='red'
一个人练习一个人 2024-10-21 01:17:21

这是因为 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 to savefig. 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)

青朷 2024-10-21 01:17:21

只需将 facecolor='red' 添加到 plt.savefig() 就足够了。例如:

plt.savefig('figname.png', facecolor='red')

Just adding the facecolor='red' to the plt.savefig() is sufficient. For example:

plt.savefig('figname.png', facecolor='red')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文