I have been using plot.ly and would like to group multiple traces to the same key in the legend. I have subplots that show data about a particular user. Id like to have each key represent a user rather than user.data1, user.data2, etc.
Here is an example of what I have now:
I would like to group all 1s in to their own key, all 2s to their own key, and all 3s to their own key.
This part of my code demonstrating how I am currently doing the traces
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
Here is the final view of my solution adding legendgroup
, marker=dict(color='')
, as well as showlegend
led me to the desired outcome.
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)
I think legendgroup
is what you're searching for:
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With