Skip to content

Commit ec640bd

Browse files
committed
add max height example
1 parent 3465529 commit ec640bd

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

doc/python/legend.md

Lines changed: 42 additions & 2 deletions
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.16.1
9+
jupytext_version: 1.17.2
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.10.11
23+
version: 3.9.0
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -254,6 +254,46 @@ fig.update_layout(legend=dict(
254254
fig.show()
255255
```
256256

257+
#### Legend Max Height
258+
259+
*New in 6.3*
260+
261+
By default, a legend can expand to fill up to half of the layout area height for a horizontal legend and the full height for a vertical legend. You can specify the maximum height of a legend with the `maxheight` parameter. In the following plot with many legend items, we set `maxheight` to a ratio of 0.10, giving the plot more space.
262+
263+
```python
264+
import plotly.express as px
265+
from plotly import data
266+
267+
df = data.gapminder().query("year==2007 and continent == 'Europe'")
268+
269+
fig = px.scatter(df,
270+
x="gdpPercap",
271+
y="lifeExp",
272+
color="country",
273+
size="pop",
274+
size_max=45,
275+
title="Life Expectancy vs. GDP per Capita in 2007 (by Country)",
276+
labels={"gdpPercap": "GDP per Capita"},
277+
)
278+
279+
fig.update_layout(
280+
xaxis=dict(
281+
side="top"
282+
),
283+
legend=dict(
284+
orientation="h",
285+
yanchor="bottom",
286+
y=-0.35,
287+
xanchor="center",
288+
x=0.5,
289+
maxheight=0.1, # Comment maxheight to see legend take up 0.5 of plotting area
290+
title_text="Country"
291+
),
292+
)
293+
294+
fig.show()
295+
```
296+
257297
#### Styling Legends
258298

259299
Legends support many styling options.

0 commit comments

Comments
 (0)