2010-05-12 57 views
1

我正在用matplotlib構建一個簡單的折線圖,並且我想斑馬條紋圖的背景,以便每個交替行的着色方式不同。有沒有辦法做到這一點?matplotlib - 斑馬條紋圖的背景顏色?

我的圖表已經有網格,並且只有主要的刻度。

編輯:從下面的評論我的代碼,但更爲清晰可辨:

yTicks = ax.get_yticks()[:-1] 
xTicks = ax.get_xticks() 
ax.barh(yTicks, [max(xTicks)-min(xTicks)] * len(yTicks), 
    height=(yTicks[1]-yTicks[0]), left=min(xTicks), color=['w','#F0FFFF']) 

回答

2

這是一個使用一個條形圖(axes.barh)來模擬條紋快速破解。

import matplotlib.pyplot as plt 

# initial plot 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot([1,2,3,4,5]) 

yTickPos,_ = plt.yticks() 
yTickPos = yTickPos[:-1] #slice off the last as it is the top of the plot 
# create bars at yTickPos that are the length of our greatest xtick and have a height equal to our tick spacing 
ax.barh(yTickPos, [max(plt.xticks()[0])] * len(yTickPos), height=(yTickPos[1]-yTickPos[0]), color=['g','w']) 

plt.show() 

產地:

alt text http://i42.tinypic.com/21lvqxf.png

+1

謝謝!我開始時有點麻煩,因爲我的x軸包含日期,所以我改變了它: yTicks = ax.get_yticks()[: - 1] xTicks = ax.get_xticks() ax.barh(yTicks,[max(xTicks)-min(xTicks)] * len(yTicks), height =(yTicks [1] -yTicks [0]),left = min(xTicks),color = ['w ','#F0FFFF']) (請注意,有一個新的left = arg,並且每個單獨的條寬爲max(xticks) - min(xticks),而不僅僅是max(xticks)。 – jbreed 2010-05-12 18:43:49