Black Lives Matter. Please consider donating to Black Girls Code today.

Graphs sharing same legend

Hello,
I have three different graphs each with an unique id and callbacks with the same input, that have the same legend entries. Is there a way for an easier overview, that I have one legend for all three figures and if I select or deselect an entry that all three figures are updated?
Also is there a way for showing the plot title only once for all three graph, because I have the same title for all three?

Thank you!

Hello @majabaaaa,

could you provide an example of your graphs code and/or a printscreen of how your three graphs look like?

This would help us helping you :slight_smile:

Thanks

Like in the images I have different plots with the same data source, so the legend that is created by both of them is from the same source.
The last image is what I want to achieve. To have the plots use the same legend to show hide entries in both plots

Here is an example code using plotly.tools to make subplots, then I used the prop legendgroup to group together the traces.

# In[]:
# Import required libraries
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State, Event

import plotly.graph_objs as go
from plotly import tools

# Define the app
app = dash.Dash('')
server = app.server
app.config.suppress_callback_exceptions = False
app.scripts.config.serve_locally = True


fig = tools.make_subplots(
    rows=1, cols=2, print_grid=False,
    subplot_titles=('Graph1', 'Graph2'))

fig.append_trace(
    go.Bar(
        x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
           2003,
           2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
           2012],
        y=[219, 146, 112, 127, 124, 180, 236, 207, 236, 263,
           350, 430, 474, 526, 488, 537, 500, 439],
        name='Data_trace1',
        legendgroup='group1'
    ),
    1,
    1
)
fig.append_trace(
    go.Bar(
        x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
           2003,
           2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
           2012],
        y=[16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156,
           270,
           299, 340, 403, 549, 499],
        name='Data_trace2',
        legendgroup='group2'
    ),
    1,
    1
)

fig.append_trace(
    go.Bar(
        x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
           2003,
           2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
           2012],
        y=[219, 146, 112, 127, 124, 180, 236, 207, 236, 263,
           350, 430, 474, 526, 488, 537, 500, 439],
        name='',
        legendgroup='group1'
    ),
    1,
    2
)
fig.append_trace(
    go.Bar(
        x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
           2003,
           2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
           2012],
        y=[16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156,
           270,
           299, 340, 403, 549, 499],
        name='',
        legendgroup='group2'
    ),
    1,
    2
)

root_layout = html.Div(
    [
        dcc.Graph(
            figure=fig,
            id='my-graph'
        )
    ]
)

app.layout = root_layout


# In[]:
# Main
if __name__ == '__main__':
    app.run_server(debug=False)

Here is a visual

Hope it solves your issue :slight_smile:

I forgot to address this point in my previous reply, simply add the line

fig.layout.update(title='your_title_here', showlegend=True)