Plot.ly-图例中有多个迹线指向相同的键

时间:2019-06-10 21:44:03

标签: plotly

我一直在使用plot.ly,并希望将多个迹线分组到图例中的同一键。 我有一些子图显示有关特定用户的数据。我想让每个键代表一个用户,而不是user.data1,user.data2等。

这是我现在拥有的一个例子:

我想将所有1分组到自己的密钥,将所有2分组到自己的密钥,并将所有3分组到自己的密钥。

我的代码的这一部分演示了我目前如何进行跟踪

trace1_A = go.Scatter(x=file1.a, y=file1.b, name='1') #plot1
trace1_B = go.Scatter(x=file1.c, y=file1.d, name='1') #plot2
trace1_C = go.Scatter(x=file1.e, y=file1.f, name='1') #plot3
trace1_D = go.Scatter(x=file1.g, y=file1.h, name='1') #plot4

2 个答案:

答案 0 :(得分:1)

我认为legendgroup是您要搜索的内容:

trace1_A = go.Scatter(x=file1.a, y=file1.b, name='1', legendgroup='1') #plot1
trace1_B = go.Scatter(x=file1.c, y=file1.d, name='1', legendgroup='1') #plot2
trace1_C = go.Scatter(x=file1.e, y=file1.f, name='1', legendgroup='1') #plot3
trace1_D = go.Scatter(x=file1.g, y=file1.h, name='1', legendgroup='1') #plot4
...

答案 1 :(得分:1)

以下是我的解决方案的最终视图,其中添加了legendgroupmarker=dict(color='')showlegend使我达到了预期的结果。

trace1_A = go.Scatter(x=file1.a, y=file1.b, name='1', legendgroup='1', marker=dict(color='red'))
trace1_B = go.Scatter(x=file1.c, y=file1.d, name='1', legendgroup='1', marker=dict(color='red'), showlegend=False)
trace1_C = go.Scatter(x=file1.e, y=file1.f, name='1', legendgroup='1', marker=dict(color='red'), showlegend=False)
trace1_D = go.Scatter(x=file1.g, y=file1.h, name='1', legendgroup='1', marker=dict(color='red'), showlegend=False)
trace2_A = go.Scatter(x=file2.a, y=file2.b, name='2', legendgroup='2', marker=dict(color='blue'))
trace2_B = go.Scatter(x=file2.c, y=file2.d, name='2', legendgroup='2', marker=dict(color='blue'), showlegend=False)
trace2_C = go.Scatter(x=file2.e, y=file2.f, name='2', legendgroup='2', marker=dict(color='blue'), showlegend=False)
trace2_D = go.Scatter(x=file2.g, y=file2.h, name='2', legendgroup='2', marker=dict(color='blue'), showlegend=False)
trace3_A = go.Scatter(x=file3.a, y=file3.b, name='3', legendgroup='3', marker=dict(color='green'))
trace3_B = go.Scatter(x=file3.c, y=file3.d, name='3', legendgroup='3', marker=dict(color='green'), showlegend=False)
trace3_C = go.Scatter(x=file3.e, y=file3.f, name='3', legendgroup='3', marker=dict(color='green'), showlegend=False)
trace3_D = go.Scatter(x=file3.g, y=file3.h, name='3', legendgroup='3', marker=dict(color='green'), showlegend=False)