2017-04-23 66 views
3

我試圖在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

回答

4

設定人臉的顏色(或背景色)的圖形使用這個功能:

fig.patch.set_facecolor('grey') 

或者以另一種方式,您可致電:

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

結果是這樣的: enter image description here

但是沒有你的圖像結果是不完整的。 但是,如果你要保存你的數字使用命令是這樣的:plt.savefig('counties2.png', facecolor = fig.get_facecolor(), transparent = True)