Skip to content

Commit 74c0e1a

Browse files
committed
Update legend.md
1 parent 66e7a67 commit 74c0e1a

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

doc/python/legend.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.14.1
9+
jupytext_version: 1.14.5
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.8.0
23+
version: 3.8.8
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -574,6 +574,70 @@ fig.update_layout(width=600, title_text='Exploration of a vector field using sev
574574
fig.show()
575575
```
576576

577+
### Adding Multiple Legends
578+
579+
*New in 5.15*
580+
581+
By default, all traces appear on one legend. To have multiple legends, specify an alternative legend for a trace using the `legend` property. For a second legend, set `legend="legend2"`. Specify more legends with `legend="legend3"`, `legend="legend4"` and so on.
582+
583+
In this example, the last two scatter traces display on the second legend, "legend2". On the figure's layout, we then position and style this legend to display on the right of the graph below the first legend.
584+
585+
586+
```python
587+
import plotly.graph_objects as go
588+
from plotly import data
589+
590+
df = data.gapminder()
591+
592+
df_germany = df.loc[(df.country.isin(["Germany"]))]
593+
df_france = df.loc[(df.country.isin(["France"]))]
594+
df_uk = df.loc[(df.country.isin(["United Kingdom"]))]
595+
596+
597+
df_averages_europe = (
598+
df.loc[(df.continent.isin(["Europe"]))].groupby(by="year").mean(numeric_only=True)
599+
)
600+
df_averages_americas = (
601+
df.loc[(df.continent.isin(["Americas"]))].groupby(by="year").mean(numeric_only=True)
602+
)
603+
604+
605+
fig = go.Figure(
606+
data=[
607+
go.Scatter(x=df_germany.year, y=df_germany.gdpPercap, name="Germany"),
608+
go.Scatter(x=df_france.year, y=df_france.gdpPercap, name="France"),
609+
go.Scatter(x=df_uk.year, y=df_uk.gdpPercap, name="UK"),
610+
go.Scatter(
611+
x=df_averages_europe.index,
612+
y=df_averages_europe.gdpPercap,
613+
name="Europe",
614+
legend="legend2",
615+
),
616+
go.Scatter(
617+
x=df_averages_americas.index,
618+
y=df_averages_americas.gdpPercap,
619+
name="Americas",
620+
legend="legend2",
621+
),
622+
],
623+
layout=dict(
624+
title="GDP Per Capita",
625+
legend={"title": "By country", "bgcolor": "Orange",},
626+
legend2={
627+
"x": 1.155,
628+
"y": 0.55,
629+
"xanchor": "right",
630+
"yanchor": "middle",
631+
"bgcolor": "Gold",
632+
"title": {"text": "By continent"},
633+
},
634+
),
635+
)
636+
637+
fig.show()
638+
639+
```
640+
577641
#### Reference
578642

579643
See https://plotly.com/python/reference/layout/#layout-legend for more information!

0 commit comments

Comments
 (0)