2014-11-14 128 views
2

如何避免子圖中的重複圖例標籤?我會在matplotlib中介紹的一種方法是將自定義圖例標籤傳遞給圖例對象。我無法在劇情中找到相應選項的任何文檔。有任何想法嗎?如何避免重複圖例標籤或傳遞自定義圖例標籤

traces = [] 

colors = {'Iris-setosa': 'rgb(31, 119, 180)', 
      'Iris-versicolor': 'rgb(255, 127, 14)', 
      'Iris-virginica': 'rgb(44, 160, 44)'} 

for col in range(4): 
    for key in colors: 
     traces.append(Histogram(x=X[y==key, col], 
         opacity=0.75, 
         xaxis='x%s' %(col+1), 
         marker=Marker(color=colors[key]), 
         name=key 
         ) 
        ) 

data = Data(traces) 

layout = Layout(barmode='overlay', 
       xaxis=XAxis(domain=[0, 0.25], title='sepal length (cm)'), 
       xaxis2=XAxis(domain=[0.3, 0.5], title='sepal width (cm)'), 
       xaxis3=XAxis(domain=[0.55, 0.75], title='petal length (cm)'), 
       xaxis4=XAxis(domain=[0.8, 1], title='petal width (cm)'), 
       yaxis=YAxis(title='count'), 
       title='Distribution of the different Iris flower features') 

fig = Figure(data=data, layout=layout) 

py.iplot(fig) 

enter image description here

回答

3

Plotly控制該上的跟蹤級別。嘗試傳入showlegend=False內的Histogram您不希望出現在圖例中的痕跡。

參考:https://plot.ly/python/reference/#Histogram-showlegend

實施例:從上面的鏈接https://plot.ly/python/legend/#Hiding-Legend-Entries

直接複製 - 粘貼。

import plotly.plotly as py 
from plotly.graph_objs import * 
# Fill in with your personal username and API key 
# or, use this public demo account 
py.sign_in('Python-Demo-Account', 'gwt101uhh0') 

trace1 = Scatter(
    x=[0, 1, 2], 
    y=[1, 2, 3], 
    name='First Trace', 
    showlegend=False 
) 
trace2 = Scatter(
    x=[0, 1, 2, 3], 
    y=[8, 4, 2, 0], 
    name='Second Trace', 
    showlegend=True 
) 
data = Data([trace1, trace2]) 
plot_url = py.plot(data, filename='show-legend') 

你想看到上面trace1顯示的使用。

+0

謝謝,完美的作品! – Sebastian 2014-11-14 23:13:34

+4

@SebastianRaschka但是,如果'trace1'的圖例被隱藏,那麼您無法控制或隱藏'trace1'的痕跡。 – 2016-07-07 16:46:22