Skip to content

Commit 4024629

Browse files
legend order
1 parent 196b5b0 commit 4024629

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

doc/python/axes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Other kinds of subplots and axes are described in other tutorials:
4646

4747
**See also** the tutorials on [facet plots](/python/facet-plots/), [subplots](/python/subplots) and [multiple axes](/python/multiple-axes/).
4848

49-
### Axis Types
49+
### 2-D Cartesian Axis Types and Auto-Detection
5050

5151
The different types of Cartesian axes are configured via the `xaxis.type` or `yaxis.type` attribute, which can take on the following values:
5252

doc/python/categorical-axes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jupyter:
3636

3737
This page shows examples of how to configure [2-dimensional Cartesian axes](/python/figure-structure/#2d-cartesian-trace-types-and-subplots) to visualize categorical (i.e. qualitative, nominal or ordinal data as opposed to continuous numerical data). Such axes are a natural fit for bar charts, waterfall charts, funnel charts, heatmaps, violin charts and box plots, but can also be used with scatter plots and line charts. [Configuring gridlines, ticks, tick labels and axis titles](/python/axes/) on logarithmic axes is done the same was as with [linear axes](/python/axes/).
3838

39-
### Axis Type and Auto-Detection
39+
### 2-D Cartesian Axis Type and Auto-Detection
4040

4141
The different types of Cartesian axes are configured via the `xaxis.type` or `yaxis.type` attribute, which can take on the following values:
4242

doc/python/legend.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,46 @@ fig = px.scatter(df, x="total_bill", y="tip", color="sex", symbol="smoker", face
5757
fig.show()
5858
```
5959

60+
### Legend Order
61+
62+
By default, Plotly Express lays out legend items in the order in which values appear in the underlying data. Every Plotly Express function also includes a `category_orders` keyword argument which can be used to control [the order in which categorical axes are drawn](/python/categorical-axes/), but beyond that can also control the order in which legend items appear, and [the order in which facets are laid out](/python/facet-plots/).
63+
64+
```python
65+
import plotly.express as px
66+
df = px.data.tips()
67+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group", facet_col="sex",
68+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
69+
"smoker": ["Yes", "No"],
70+
"sex": ["Male", "Female"]})
71+
fig.show()
72+
```
73+
74+
When using stacked bars, the bars are stacked from the bottom in the same order as they appear in the legend, so it can make sense to set `layout.legend.traceorder` to `"reversed"` to get the legend and stacks to match:
75+
76+
```python
77+
import plotly.express as px
78+
df = px.data.tips()
79+
fig = px.bar(df, x="day", y="total_bill", color="smoker", barmode="stack", facet_col="sex",
80+
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
81+
"smoker": ["Yes", "No"],
82+
"sex": ["Male", "Female"]})
83+
fig.update_layout(legend_traceorder="reversed")
84+
fig.show()
85+
```
86+
87+
When using [`plotly.graph_objects`](/python/graph-objects/) rather than Plotly Express, legend items will appear in the order that traces appear in the `data`:
88+
89+
```python
90+
import plotly.graph_objects as go
91+
92+
fig = go.Figure()
93+
fig.add_trace(go.Bar(name="first", x=["a", "b"], y=[1,2]))
94+
fig.add_trace(go.Bar(name="second", x=["a", "b"], y=[2,1]))
95+
fig.add_trace(go.Bar(name="third", x=["a", "b"], y=[1,2]))
96+
fig.add_trace(go.Bar(name="fourth", x=["a", "b"], y=[2,1]))
97+
fig.show()
98+
```
99+
60100
#### Showing and Hiding the Legend
61101

62102
By default the legend is displayed on Plotly charts with multiple traces, and this can be explicitly set with the `layout.showlegend` attribute:
@@ -419,4 +459,4 @@ fig.show()
419459

420460
#### Reference
421461

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

0 commit comments

Comments
 (0)