Skip to content

Commit 942d9c1

Browse files
committed
Update legend.md
1 parent f2447a4 commit 942d9c1

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

doc/python/legend.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.8.8
23+
version: 3.10.11
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -638,6 +638,45 @@ fig.show()
638638

639639
```
640640

641+
### Positioning Legends
642+
643+
*New in 5.15*
644+
645+
In the previous example, we position the second legend by specifying x and y values. By default, these values are based on the width and height of the plot area. It is also possible to specify values that reference the container width and height by setting "xref=container" and "yref="container" (the default values are "xref=paper" and "yref="paper"). When set to "container", the margin grows so the legend and plot don't overlap.
646+
647+
```python
648+
import plotly.graph_objects as go
649+
from plotly import data
650+
651+
df = data.gapminder()
652+
653+
df_germany = df.loc[(df.country.isin(["Germany"]))]
654+
df_france = df.loc[(df.country.isin(["France"]))]
655+
df_uk = df.loc[(df.country.isin(["United Kingdom"]))]
656+
657+
fig = go.Figure(
658+
data=[
659+
go.Scatter(x=df_germany.year, y=df_germany.gdpPercap, name="Germany"),
660+
go.Scatter(x=df_france.year, y=df_france.gdpPercap, name="France"),
661+
go.Scatter(x=df_uk.year, y=df_uk.gdpPercap, name="UK"),
662+
],
663+
layout=dict(
664+
title="GDP Per Capita",
665+
legend={
666+
"x": 0.9,
667+
"y": 0.9,
668+
"xref": "container",
669+
"yref": "container",
670+
"bgcolor": "Gold",
671+
"title": {"text": "By continent"},
672+
},
673+
),
674+
)
675+
676+
fig.show()
677+
678+
```
679+
641680
#### Reference
642681

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

0 commit comments

Comments
 (0)