2016-04-14 21 views
1

複数の色を使用してx軸上の領域を強調表示しようとしています。私は、この図に示すように、x軸に沿って領域を区切って解を見つけることに成功しました。しかし、代わりにy軸上で区切りが起きている解決策が必要です。プロットの6362を例に挙げてください。他のすべてのダッシュ(またはそれが何であれ)が紫色と赤色である破線のバーのようなものを作成する方法はありますか?Pyplot axvspan:複数の色を1つのスパンに(垂直方向に)

編集 ここでは、それぞれの「ダッシュ」を作成するaxvspanyminymaxオプションを使用してこれを行うことができ、水平

# Find exon's index 
e_index = sorted(list(all_samples.ensembl_exon_id.unique())).index(exon) 

# Total x-axis span incl offsets 
xmin = e_index-0.25 # Start of x-span 
xmax = e_index+0.25 # End of x-span 
diff = xmax-xmin  # Length of entire span 
buf = diff/len(s_names) # Length of each subsection 

# Go through each sample 
for sname in s_names: 
    # Get color of this sample 
    s_color = colors[sname] 

    # Get index of this sample 
    order = list(s_names).index(sname) 

    # Calc xmin and xmax for subsection 
    s_xmin = xmin + (buf * order) 
    s_xmax = s_xmin + buf 

    # Highlight 
    plt.axvspan(xmin=s_xmin, xmax=s_xmax, alpha=0.25, color=s_color, zorder=0.6, ymin=0, ymax=1) 
+0

あなたは私が思うのパッチを使用していることを構築する必要があります。 – armatita

+0

@armatita申し訳ありません、パッチを見ていきましょう、ありがとう。私はあなたが見てみたい場合は、現在強調表示をしている方法のコードを追加しました。 – Plasma

答えて

2

を各サブセクションを強調表示するための関連するコードです。軸間隔0-1をループすることで、すべてのダッシュを構築できます。

ここでは、やや自動化するためにまとめた簡単な機能を紹介します。領域をダッシュ​​で埋めるために必要なオプションを付けてvspandashと呼んでください。 http://matplotlib.org/examples/shapes_and_collections/artist_reference.htmlをあなたのコードとデータを見ることなく、それはより多くを語ることは困難です。

import matplotlib.pyplot as plt 
import numpy as np 

fig,ax = plt.subplots(1) 

x=y=np.arange(11) 

ax.plot(x,y,'go-') 

def vspandash(thisax,xmark,xwidth=0.6,ndash=10,colour1='r',colour2='m'): 

    interval = 1./ndash 
    hxwidth = xwidth/2. 

    for j in np.arange(0,1,interval*2): 
     thisax.axvspan(
       xmin=xmark-hxwidth,xmax=xmark+hxwidth, 
       ymin=j,ymax=j+interval, 
       facecolor=colour1,alpha=0.25,edgecolor='None' 
       ) 
     thisax.axvspan(
       xmin=xmark-hxwidth,xmax=xmark+hxwidth, 
       ymin=j+interval,ymax=j+interval*2., 
       facecolor=colour2,alpha=0.25,edgecolor='None' 
       ) 

# Lets explore the different options 
vspandash(ax,2)       # Default width, number of dashes, and colours 
vspandash(ax,4,ndash=20)     # Increase number of dashes 
vspandash(ax,6,xwidth=0.3)    # Change width of shaded region 
vspandash(ax,8,colour1='b',colour2='g') # Change colours of dashes 

plt.show() 

enter image description here

関連する問題