简体   繁体   中英

Python: Share legend among subplots in Plotly

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

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)

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 legend(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.

It sounds like you're wanting to control related traces via a single legend item. If I've understood correctly, have a look below.

The key attributes to note are: legendgroup and showlegend . Here is a link to Plotly's docs on the subject .

Sample Code:

import numpy as np
from plotly.offline import iplot

# Creating a dataset.
x = np.linspace(0, np.pi*2, 250)
y1 = [np.sin(i) for i in x]
y2 = [np.cos(i) for i in x]
y3 = [np.sin(i) for i in x**2]
y4 = [np.cos(i) for i in x**2]

# Plot the data with grouped legends.
data = []
layout = {}
data.append({'x': x, 'y': y1, 'name': 'sin', 'legendgroup': 'sin'})
data.append({'x': x, 'y': y2, 'name': 'cos', 'legendgroup': 'cos'})
data.append({'x': x, 'y': y3, 'yaxis': 'y2', 'name': 'sin', 'legendgroup': 'sin', 'showlegend': False})
data.append({'x': x, 'y': y4, 'yaxis': 'y2', 'name': 'cos', 'legendgroup': 'cos', 'showlegend': False})

layout['xaxis1'] = {'domain': [0.00, 1.00], 'anchor': 'y2'}
layout['yaxis1'] = {'domain': [0.55, 1.00]}
layout['yaxis2'] = {'domain': [0.00, 0.45]}

iplot({'data': data, 'layout': layout})

Output:

Notice that in Figure 1, both the sin and cos traces are showing in both graphs. Then, in Figure 2, I have turned off the cos trace, in both graphs, using the single legend item.

Figure 1:

在此处输入图片说明

Figure 2:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM