2016-04-14 65 views
1

我試圖使用多種顏色在x軸上突出顯示一個區域。我已經設法找到一個解決方案,沿着x軸分區域,如下圖所示: enter image description herePyplot axvspan:一個範圍內的多種顏色(垂直)

但是,我想要一個解決方案,在y軸上進行切片。以情節6362爲例。有沒有什麼辦法可以創建一個像虛線條那樣的東西,其中每一個破折號(或者其他所謂的)都是紫色和紅色的?

編輯 下面是突出顯示每個小節的相關代碼水平

# 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

你必須建立一個使用補丁,我認爲:http://matplotlib.org/examples/shapes_and_collections/artist_reference.html沒有看到你的代碼和數據很難說了。 – armatita

+0

@armatita好的,我會研究補丁,謝謝。如果您想查看,我添加了代碼以顯示我目前如何進行突出顯示。 – Plasma

回答

2

您可以在此使用yminymax選項axvspan創建的每個「衝刺」做。通過循環軸間隔0-1,您可以構建所有的破折號。

這裏有一個快速的功能,我把它放在一起使它有點自動化。請撥打vspandash並填寫您所需的選項,以便用短劃線填充區域。

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