2017-06-19 48 views
3

我試圖設置一系列垂直軸跨度以表示不同時間的不同開關位置。例如,在下圖中,開關位置1(綠色)發生了很多次,在其他位置之間交替。將多個axvspan標籤設置爲圖例中的一個元素

Switching positions symbolized by axvspan plots

我繪製運行的for循環這些跨距在元組的列表,每個都包含每個間隔的初始和最終索引繪製axvspan。

def plotShades(timestamp, intervals, colour): 
    for i in range(len(intervals)): 
     md.plt.axvspan(timestamp[intervals[i][0]], timestamp[intervals[i][1]], alpha=0.5, color=colour, label="interval") 

那麼該功能被稱爲在一個又一個,即繪製每個不同的開關位置的色調:用代碼段我在這裏放(不是最好的代碼,這樣做

def plotAllOutcomes(timestamp, switches): 
    #switches is a list of 7 arrays indicating when the switcher is at each one of the 7 positions. If the array has a 1 value, the switcher is there. 0 otherwise. 

    colors = ['#d73027', '#fc8d59', '#fee08b', '#ffffbf', '#d9ef8b', '#91cf60', '#1a9850']  
    intervals = []   

    for i in range(len(switches)): 
     intervals.append(getIntervals(switches[i], timestamp)) 
     plotShades(timestamp, intervals[i], colors[i])  
     md.plt.legend() 

,我知道 - 我在Python中是相當新的!)圖例最後每個區間都有一個項目,這很糟糕。這是它的外觀:

我想獲得只有7個項目,每一個傳奇在我axvspans的情節單一顏色。我該如何繼續這樣做?我已經搜索了很多,但沒有找到這種情況之前被問到。預先感謝您的任何幫助!!

回答

2

一個小竅門,你可以申請使用的事實,開始"_"標籤將被忽略:

plt.axvspan(... , label = "_"*i + "interval") 

從而標籤的情況下i==0只被創建。

+0

非常感謝!這是一個很棒的技巧,對我的其他代碼也非常有用。非常感激! – Sergio

相關問題