Skip to content

Commit 91e75a1

Browse files
Merge pull request #2802 from hweecat/doc-histogram-categoryorder
DOC: Add histogram category order example (#2779)
2 parents 09017f3 + 5334bf0 commit 91e75a1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: doc/python/histograms.md

+15
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,21 @@ fig.update_layout(
449449
fig.show()
450450
```
451451

452+
### Sort Histogram by Category Order
453+
454+
Histogram bars can also be sorted based on the ordering logic of the categorical values using the [categoryorder](https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-categoryorder) attribute of the x-axis. Sorting of histogram bars using `categoryorder` also works with multiple traces on the same x-axis. In the following examples, the histogram bars are sorted based on the total numerical values.
455+
456+
```python
457+
import plotly.express as px
458+
import pandas as pd
459+
460+
df = px.data.tips()
461+
fig1 = px.histogram(df, x="day").update_xaxes(categoryorder='total ascending')
462+
fig2 = px.histogram(df, x="day", color="smoker").update_xaxes(categoryorder='total descending')
463+
fig1.show()
464+
fig2.show()
465+
```
466+
452467
#### Reference
453468

454469
See [function reference for `px.histogram()`](https://plotly.com/python-api-reference/generated/plotly.express.histogram) or https://plotly.com/python/reference/histogram/ for more information and chart attribute options!

0 commit comments

Comments
 (0)