2014-11-14 126 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