2016-10-04 7 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

あなたはあなたの答えをpatches

matplotlibから
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 
関連する問題