在matplotlib中设置图像背景颜色

问题描述:

我试图在Python中使用Matplotlib来显示图像并在其上的各个点上显示文本。我想让图像部分透明,以增加文字的可见度。在matplotlib中设置图像背景颜色

但是,我想图像背后的背景颜色是白色而不是灰色,我无法弄清楚如何让这种变化坚持下去。这是我所在的地方。

img = plt.imread("counties.png") 
fig, ax = plt.subplots() 
plt.axis('off') 
plt.text(.6, .68,'matplotlib', ha='center', va='center', 
transform=ax.transAxes, color=(0,.16,.48), fontname='Kanit Light') 
plt.text(.5, .5,'test', ha='center', va='center', transform=ax.transAxes, 
color=(0,.16,.48)) 
ax.imshow(img, alpha=0.05) 
plt.show() 
+0

查看照片counties.png。 – Serenity

设定人脸的颜色(或背景色)的图形使用这个功能:

fig.patch.set_facecolor('grey') 

或者以另一种方式,您可致电:

plt.rcParams['figure.facecolor'] = 'grey' 

结果是这样的: enter image description here

但是没有你的图像结果是不完整的。 但是,如果你要保存你的数字使用命令是这样的:plt.savefig('counties2.png', facecolor = fig.get_facecolor(), transparent = True)