如何设置图表智慧Matplotlib的背景色的不透明度 [英] How to set opacity of background colour of graph wit Matplotlib

查看:179
本文介绍了如何设置图表智慧Matplotlib的背景色的不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩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.

推荐答案

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

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.

例如:

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

如果要进行更细粒度的控制,只需设置图形和轴背景色块的面色和/或alpha值即可. (要使补丁完全透明,我们可以将alpha设置为0,或将facecolor设置为'none'(作为字符串,而不是对象None!))

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!))

例如:

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()

这篇关于如何设置图表智慧Matplotlib的背景色的不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆