0

I am very new to the plotly(python)

I am trying to plot a graph with two subplots, however i am getting separate legends.

What i am expecting:

Have a common legend for both the plots. I should be able to isolate the traces when i hover/click the legends(all the common legends should be isolated)

if i click on 'legend' from fig1, 'legend' from fig2 should also be isolated since they share the common legend.

Kindly advice.

import plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
    xaxis='x1', yaxis='y1',
    name='legend'
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    xaxis='x2', yaxis='y2',
    name='legend'
)

data = [trace0, trace1]
layout = go.Layout(
    legend=dict(
        x=0,
        y=1,
        traceorder='normal',
        font=dict(
            family='sans-serif',
            size=12,
            color='#000'
        ),
        bgcolor='#E2E2E2',
        bordercolor='#FFFFFF',
        borderwidth=2
    ),
    annotations=[
        dict(
            x=0,
            y=1.05,
            xref='paper',
            yref='paper',
            text='Legend Title',
            showarrow=False
        )
    ]
)

layout = go.Layout(
                xaxis1=dict(
                    domain=[0, 0.45],
                   
                ),
                yaxis1=dict(
                    domain=[0.75, 1],
                    
                ),
                yaxis2=dict(
                    domain=[0.75, 1],
                    anchor="x2"
                ),
                xaxis2=dict(
                    domain=[0.55, 1],
                    showticklabels=False
                )

            )
fig = go.Figure(data=data, layout = layout)

iplot(fig)
```
  • 1
    A good place to start is showing what you've already tried or the relevant part of your code. Also, did you try looking into other answers here? This seems like it's already answered. – Kostas Mouratidis Aug 25 '20 at 10:53
  • Hi Kostas, Thanks for the response. I have added code to the question now. in the output. if i click on trace1 from any of the figures. trace1 from both the figures should be isolated. – Nagendra_databee Aug 25 '20 at 11:43
  • So did you take a look at the other question (https://stackoverflow.com/a/47957771/6655150)? `legendgroup` should be what you're looking for – Kostas Mouratidis Aug 25 '20 at 15:51
  • Hi Kostas, Yes i did. I am quite not able to get it. Is it possible for you to change my code with reference to the question you shared, please? – Nagendra_databee Aug 25 '20 at 16:22
  • 1
    Hi Kostas, apologies.. the link provides works perfectly fine for my use case. Thanks a lot. https://stackoverflow.com/questions/62678377/plotly-how-to-set-up-multiple-subplots-with-grouped-legends – Nagendra_databee Aug 25 '20 at 17:34

0 Answers0