2014-04-26 28 views
2

設置背景色說,我有一個圖像由4個個子地塊,像這樣:的插曲

進口matplotlib.pyplot如PLT 進口numpy的爲NP

# Simple data to display in various forms 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

# Four axes, returned as a 2-d array 
f, axarr = plt.subplots(2, 2) 
axarr[0, 0].plot(x, y) 
axarr[0, 0].set_title('Axis [0,0]') 
axarr[0, 1].scatter(x, y) 
axarr[0, 1].set_title('Axis [0,1]') 
axarr[1, 0].plot(x, y ** 2) 
axarr[1, 0].set_title('Axis [1,0]') 
axarr[1, 1].scatter(x, y ** 2) 
axarr[1, 1].set_title('Axis [1,1]') 
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots 
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False) 
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False) 

plt.show() 

返回此:

enter image description here

我想添加一個灰色的背景到這個地塊之一,比如左下角的地方,因爲我喜歡R會顯示一些圖像(例如,請參見here)。我還沒有找到一個簡單的方法來與matplotlib這樣做,我錯過了什麼?

回答

4

您可以簡單地通過axarr[1,0].set_axis_bgcolor('grey')來手動更改任何特定軸的軸顏色。

matplotlib接受許多不同顏色的字符串(實例herehere)以及在HTML字符串(例如'#eeefff'hex值。 Plot

+0

優秀的答案,也很簡單,非常感謝你! – Gabriel