How to avoid duplicate legend labels in plotly or pass custom legend labels(如何避免重复的图例标签或通过自定义图例标签)
问题描述
如何避免子图中重复的图例标签?我在 matplotlib 中进行此操作的一种方法是将自定义图例标签传递给图例对象.我在 plotly 中找不到任何等效选项的文档.有任何想法吗?
How can I avoid duplicate legend labels in subplots? One way I would go about it in matplotlib would be to pass custom legend labels to an legend object. I couldn't find any documentation for an equivalent option in plotly. Any ideas?
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="c2VwYWwgbGVuZ3RoIChjbSk="),
xaxis2=XAxis(domain=[0.3, 0.5], title="c2VwYWwgd2lkdGggKGNtKQ=="),
xaxis3=XAxis(domain=[0.55, 0.75], title="cGV0YWwgbGVuZ3RoIChjbSk="),
xaxis4=XAxis(domain=[0.8, 1], title="cGV0YWwgd2lkdGggKGNtKQ=="),
yaxis=YAxis(title="Y291bnQ="),
title="RGlzdHJpYnV0aW9uIG9mIHRoZSBkaWZmZXJlbnQgSXJpcyBmbG93ZXIgZmVhdHVyZXM=")
fig = Figure(data=data, layout=layout)
py.iplot(fig)
推荐答案
Plotly 在跟踪级别控制这个.尝试在您不想出现在图例中的 Histogram
跟踪中传入 showlegend=False
.
Plotly controls this on the trace level. Try passing in showlegend=False
inside the Histogram
traces that you don't want to appear in the legend.
参考:https://plot.ly/python/reference/#Histogram-showlegend
示例:https://plot.ly/python/legend/#Hiding-图例条目
从上面的链接直接复制粘贴.
Direct copy-paste from the link above.
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
中.
The usage you want to see is shown in trace1
above.
这篇关于如何避免重复的图例标签或通过自定义图例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何避免重复的图例标签或通过自定义图例标签


- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01