如何避免在绘图中重复出现图例标签或通过自定义图例标签
问题内容:
如何避免子图中重复的图例标签?我会在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)
问题答案:
在跟踪级别上进行绘图控制。尝试将不想出现在图例中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
如上所示。