# Graphs sharing same legend

**URL:** <https://community.plotly.com/t/graphs-sharing-same-legend/12476>\
**Category:** Dash Python\
**Created:** [August 7, 2018, 6:16am UTC](https://community.plotly.com/t/graphs-sharing-same-legend/12476 "2018-08-07T06:16:40Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Bachibouzouk](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bachibouzouk/32/3594_2.png) [@Bachibouzouk](https://community.plotly.com/u/Bachibouzouk)\
**Post date:** [August 7, 2018, 11:10am UTC](https://community.plotly.com/t/graphs-sharing-same-legend/12476/4 "2018-08-07T11:10:07Z")

</div>

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

```auto
# 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

 ![2018-08-07_graph_legend](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/f/f225c2d6b82e2da83b6964bf5fc7fb61dcf0da13.png) ![2018-08-07_graph_legend2](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/778b54d6059bad7d691343103de14c6b696f8c80.png)

Hope it solves your issue 🙂

---

_[View the full topic](https://community.plotly.com/t/graphs-sharing-same-legend/12476)._
