如何使用 Matplotlib 设置图形背景颜色的不透明度

发布于 2024-10-10 05:31:07 字数 53 浏览 2 评论 0原文

我一直在使用 Matplotlib,但不知道如何更改图形的背景颜色,或者如何使背景完全透明。

I've been playing around with Matplotlib and I can't figure out how to change the background colour of the graph, or how to make the background completely transparent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦开始←不甜 2024-10-17 05:31:07

如果您只希望图形和轴的整个背景都是透明的,则只需在使用 fig.savefig 保存图形时指定 transparent=True 即可。

例如:

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent=True)

如果您想要更细粒度的控制,您可以简单地设置图形和轴背景补丁的面部颜色和/或 alpha 值。 (要使补丁完全透明,我们可以将 alpha 设置为 0,或者将面颜色设置为 'none'(作为字符串,而不是对象 None!) )

例如:

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('blue')
fig.patch.set_alpha(0.7)

ax = fig.add_subplot(111)

ax.plot(range(10))

ax.patch.set_facecolor('red')
ax.patch.set_alpha(0.5)

# If we don't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
fig.savefig('temp.png', facecolor=fig.get_facecolor(), edgecolor='none')

plt.show()

alt text

If you just want the entire background for both the figure and the axes to be transparent, you can simply specify transparent=True when saving the figure with fig.savefig.

e.g.:

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent=True)

If you want more fine-grained control, you can simply set the facecolor and/or alpha values for the figure and axes background patch. (To make a patch completely transparent, we can either set the alpha to 0, or set the facecolor to 'none' (as a string, not the object None!))

e.g.:

import matplotlib.pyplot as plt

fig = plt.figure()

fig.patch.set_facecolor('blue')
fig.patch.set_alpha(0.7)

ax = fig.add_subplot(111)

ax.plot(range(10))

ax.patch.set_facecolor('red')
ax.patch.set_alpha(0.5)

# If we don't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
fig.savefig('temp.png', facecolor=fig.get_facecolor(), edgecolor='none')

plt.show()

alt text

故人如初 2024-10-17 05:31:07

另一种方法是设置适当的全局 rcParams 并简单地指定颜色。这是一个 MWE(我使用 RGBA 颜色格式来指定 alpha/不透明度):

import matplotlib.pyplot as plt

plt.rcParams.update({
    "figure.facecolor":  (1.0, 0.0, 0.0, 0.3),  # red   with alpha = 30%
    "axes.facecolor":    (0.0, 1.0, 0.0, 0.5),  # green with alpha = 50%
    "savefig.facecolor": (0.0, 0.0, 1.0, 0.2),  # blue  with alpha = 20%
})

plt.plot(range(10))
plt.savefig("temp.png")
plt.show()

figure.facecolor 是主要背景颜色,axes.facecolor 是背景颜色实际情节。无论出于何种原因,plt.savefig 使用 savefig.facecolor 作为主要背景颜色而不是 figure.facecolor,因此请务必更改此参数因此。

上面代码中的 plt.show() 会产生以下输出:

在此处输入图像描述

plt.savefig("temp.png") 结果如下输出:

enter image description here

如果你想让某些东西完全透明,只需将相应颜色的alpha值设置为0即可。对于plt.savefig,还有一个“lazy”选项通过将 rc 参数 savefig.transparent 设置为 True,这会将所有面部颜色的 alpha 设置为 0%。

请注意,更改 rcParams 具有 <强>全局效应,因此请记住,您的所有绘图都会受到这些变化的影响。但是,如果您有多个绘图,或者您想要更改无法更改源代码的绘图的外观,则此解决方案可能非常有用。

Another way is to set the appropriate global rcParams and simply specify the colors. Here is an MWE (I used the RGBA color format to specify the alpha/opacity):

import matplotlib.pyplot as plt

plt.rcParams.update({
    "figure.facecolor":  (1.0, 0.0, 0.0, 0.3),  # red   with alpha = 30%
    "axes.facecolor":    (0.0, 1.0, 0.0, 0.5),  # green with alpha = 50%
    "savefig.facecolor": (0.0, 0.0, 1.0, 0.2),  # blue  with alpha = 20%
})

plt.plot(range(10))
plt.savefig("temp.png")
plt.show()

The figure.facecolor is the main background color and the axes.facecolor the background color of the actual plot. For whatever reason, plt.savefig uses savefig.facecolor as the main background color rather than figure.facecolor, so make sure to change this parameter accordingly.

plt.show() from the code above results in the following output:

enter image description here

and plt.savefig("temp.png") results in this output:

enter image description here

If you want to make something completely transparent, simply set the alpha value of the corresponding color to 0. For plt.savefig, there is also a "lazy" option by setting the rc-parameter savefig.transparent to True, which sets the alpha of all facecolors to 0%.

Note that altering the rcParams has a global effect, so bear in mind that all your plots will be affected by these changes. However, this solution can be extremely useful if you have multiple plots, or if you want to change the appearance of plots where you cannot change the source code.

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