Skip to content

Commit 4725913

Browse files
committed
update with details on shapes in legends
1 parent b729e33 commit 4725913

File tree

1 file changed

+86
-14
lines changed

1 file changed

+86
-14
lines changed

doc/python/legend.md

Lines changed: 86 additions & 14 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.14.6
9+
jupytext_version: 1.14.7
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.10.4
2424
plotly:
2525
description: How to configure and style the legend in Plotly with Python.
2626
display_as: file_settings
@@ -35,7 +35,7 @@ jupyter:
3535

3636
### Trace Types, Legends and Color Bars
3737

38-
[Traces](/python/figure-structure) of most types can be optionally associated with a single legend item in the [legend](/python/legend/). Whether or not a given trace appears in the legend is controlled via the `showlegend` attribute. Traces which are their own subplots (see above) do not support this, with the exception of traces of type `pie` and `funnelarea` for which every distinct color represented in the trace gets a separate legend item. Users may show or hide traces by clicking or double-clicking on their associated legend item. Traces that support legend items also support the `legendgroup` attribute, and all traces with the same legend group are treated the same way during click/double-click interactions.
38+
[Traces](/python/figure-structure) of most types and shapes can be optionally associated with a single legend item in the [legend](/python/legend/). Whether or not a given trace or shape appears in the legend is controlled via the `showlegend` attribute. Traces which are their own subplots (see above) do not support this, with the exception of traces of type `pie` and `funnelarea` for which every distinct color represented in the trace gets a separate legend item. Users may show or hide traces by clicking or double-clicking on their associated legend item. Traces that support legend items and shapes also support the `legendgroup` attribute, and all traces and shapes with the same legend group are treated the same way during click/double-click interactions.
3939

4040
The fact that legend items are linked to traces means that when using [discrete color](/python/discrete-color/), a figure must have one trace per color in order to get a meaningful legend. [Plotly Express has robust support for discrete color](/python/discrete-color/) to make this easy.
4141

@@ -97,26 +97,66 @@ fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2,1]))
9797
fig.show()
9898
```
9999

100-
*New in v5.0*
100+
*New in 5.16*
101101

102-
The `legendrank` attribute of a trace can be used to control its placement within the legend, without regard for its placement in the `data` list.
102+
If you have shapes that appear in a legend, these are displayed after all traces:
103103

104-
The default `legendrank` for traces is 1000 and ties are broken as described above, meaning that any trace can be pulled up to the top if it is the only one with a legend rank less than 1000 and pushed to the bottom if it is the only one with a rank greater than 1000.
104+
```python
105+
import plotly.graph_objects as go
106+
107+
fig = go.Figure()
108+
fig.add_trace(go.Bar(name="first", x=["a", "b"], y=[1, 2]))
109+
fig.add_trace(go.Bar(name="second", x=["a", "b"], y=[2, 1]))
110+
fig.add_shape(
111+
name="first shape",
112+
showlegend=True,
113+
type="rect",
114+
xref="paper",
115+
line=dict(dash="dash"),
116+
x0=0.85,
117+
x1=0.95,
118+
y0=0,
119+
y1=1.5,
120+
)
121+
fig.add_trace(go.Bar(name="third", x=["a", "b"], y=[1, 2]))
122+
fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2, 1]))
123+
v
124+
fig.show()
125+
126+
```
127+
128+
The `legendrank` attribute of a trace or shape can be used to control its placement in the legend.
129+
The default `legendrank` for traces and shapes is 1000. When all traces and shapes have the same `legendrank`, traces appear in the order they appear in the data, followed by shapes in the order they are defined.
130+
131+
Any trace or shape can be pulled up to the top of the legend if it is the only one with a legend rank less than 1000 and pushed to the bottom if it is the only one with a rank greater than 1000.
132+
133+
In this example, we add a `legendrank` for each trace and shape, giving the shape the lowest rank so it appears first, and moving the first trace defined to the bottom of the legend by giving it the highest rank.
105134

106135
```python
107136
import plotly.graph_objects as go
108137

109138
fig = go.Figure()
110-
fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2,1], legendrank=4))
111-
fig.add_trace(go.Bar(name="second", x=["a", "b"], y=[2,1], legendrank=2))
112-
fig.add_trace(go.Bar(name="first", x=["a", "b"], y=[1,2], legendrank=1))
139+
fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2,1], legendrank=5))
140+
fig.add_trace(go.Bar(name="second", x=["a", "b"], y=[2,1], legendrank=4))
141+
fig.add_trace(go.Bar(name="first", x=["a", "b"], y=[1,2], legendrank=2))
113142
fig.add_trace(go.Bar(name="third", x=["a", "b"], y=[1,2], legendrank=3))
143+
fig.add_shape(
144+
legendrank=1,
145+
showlegend=True,
146+
type="line",
147+
xref="paper",
148+
line=dict(dash="5px"),
149+
x0=0.05,
150+
x1=0.45,
151+
y0=1.5,
152+
y1=1.5,
153+
)
114154
fig.show()
115155
```
116156

117157
#### Showing and Hiding the Legend
118158

119-
By default the legend is displayed on Plotly charts with multiple traces, and this can be explicitly set with the `layout.showlegend` attribute:
159+
By default the legend is displayed on Plotly charts with multiple traces, and this can be explicitly set with the `layout.showlegend` attribute.
120160

121161
```python
122162
import plotly.express as px
@@ -193,7 +233,7 @@ fig.show()
193233

194234
*New in 5.11*
195235

196-
Set the width of horizontal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width using `entrywidthmode='fraction`.
236+
Set the width of horizontal legend entries by setting `entrywidth`. Here we set it to `70` pixels. Pixels is the default unit for `entrywidth`, but you can set it to be a fraction of the plot width using `entrywidthmode='fraction'`.
197237

198238
```python
199239
import plotly.express as px
@@ -253,7 +293,7 @@ When creating figures using [graph objects](/python/graph-objects/) without usin
253293

254294
#### Legend Item Names
255295

256-
Legend items appear per trace, and the legend item name is taken from the trace's `name` attribute.
296+
For traces, legend items appear per trace, and the legend item name is taken from the trace's `name` attribute.
257297

258298
```python
259299
import plotly.graph_objects as go
@@ -275,6 +315,38 @@ fig.add_trace(go.Scatter(
275315
fig.show()
276316
```
277317

318+
By default, for shapes, legend items are disabled. Set `showlegend=True` on a shape for it to dislay a legend item.
319+
The name that appears for the shape in the legend is the shape's `name` if it is provided. If no `name` is provided, the shape label's `text` is used. If neither is provided, the legend item appears as "shape \<shape number>". For example, "shape 1".
320+
321+
```python
322+
import plotly.graph_objects as go
323+
324+
fig = go.Figure()
325+
326+
fig.add_trace(go.Scatter(
327+
x=[1, 2, 3, 4, 5],
328+
y=[1, 2, 3, 4, 5],
329+
name="Positive"
330+
))
331+
332+
fig.add_trace(go.Scatter(
333+
x=[1, 2, 3, 4, 5],
334+
y=[5, 4, 3, 2, 1],
335+
name="Negative"
336+
))
337+
338+
fig.add_shape(
339+
showlegend=True,
340+
type="rect",
341+
x0=2,
342+
x1=4,
343+
y0=4.5,
344+
y1=5,
345+
)
346+
347+
fig.show()
348+
```
349+
278350
#### Legend titles
279351

280352
```python
@@ -324,7 +396,7 @@ fig.show()
324396

325397
#### Hiding the Trace Initially
326398

327-
Traces have a `visible` attribute. If set to `legendonly`, the trace is hidden from the graph implicitly. Click on the name in the legend to display the hidden trace.
399+
Traces and shapes have a `visible` attribute. If set to `legendonly`, the trace or shape is hidden from the graph implicitly. Click on the name in the legend to display the hidden trace or shape.
328400

329401
```python
330402
import plotly.graph_objects as go
@@ -578,7 +650,7 @@ fig.show()
578650

579651
*New in 5.15*
580652

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.
653+
By default, all traces and shapes appear on one legend. To have multiple legends, specify an alternative legend for a trace or shape using the `legend` property. For a second legend, set `legend="legend2"`. Specify more legends with `legend="legend3"`, `legend="legend4"` and so on.
582654

583655
In this example, the last two scatter traces display on the second legend, "legend2". On the figure's layout, we then position and style each legend.
584656

0 commit comments

Comments
 (0)