2016-10-04 41 views
0

不同顏色的劇情背景矩形下面的代碼是從超過3軸繪製幾個財務指標大得多函數的代碼段:情節在數軸上

left, width = 0.1, 0.8 
rect1 = [left, 0.7, width, 0.2] 
rect2 = [left, 0.3, width, 0.4] 
rect3 = [left, 0.1, width, 0.2] 
fig = plt.figure(facecolor='white') 
axescolor = '#f6f6f6' # the axes background color 
ax1 = fig.add_axes(rect1)#, axisbg=axescolor) # left, bottom, width, height 
ax2 = fig.add_axes(rect2,sharex=ax1)#, axisbg=axescolor) 
ax3 = fig.add_axes(rect3,sharex=ax1)#, axisbg=axescolor 

最後的情節看起來像這樣:

enter image description here

現在我想添加背景與矩形覆蓋完整的x軸(即在所有軸)的高度和幾分鐘的寬度,這樣的:

enter image description here

我該怎麼做?

回答

1

這工作:

ax1.axvspan(start, end, facecolor='g', alpha=0.25, label=my_label) 
+0

謝謝您的回答!我標記了第一個答案,因爲它提前。但是你的解決方案也適用。 – user1934212

+0

我現在已經標記了您的解決方案,因爲無需導入額外的模塊即可實現。不幸的是,上面提出的修補程序解決方案在我使用的ipython環境中不可用,因爲修補程序模塊被提供程序列入黑名單。 – user1934212

+0

確實更容易! – MMF

2

可以使用patchesmatplotlib

import matplotlib.patches as patches 

left, width = 0.1, 0.8 
rect1 = [left, 0.7, width, 0.2] 
rect2 = [left, 0.3, width, 0.4] 
rect3 = [left, 0.1, width, 0.2] 
fig = plt.figure(facecolor='white') 
axescolor = '#f6f6f6' # the axes background color 
ax1 = fig.add_axes(rect1)#, axisbg=axescolor) # left, bottom, width, height 
ax2 = fig.add_axes(rect2,sharex=ax1)#, axisbg=axescolor) 
ax3 = fig.add_axes(rect3,sharex=ax1)#, axisbg=axescolor 
ax1.add_patch(patches.Rectangle(
     (0.1, 0.1), # (x,y) 
     0.5,   # width 
     0.5,   # height 
     alpha = 0.5)) #transparency