Skip to content

Commit 2a2ee8b

Browse files
committed
DOC: Add histogram category order example (#2779)
1 parent 2aebcaf commit 2a2ee8b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Diff for: doc/python/histograms.md

+19-4
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.7.3
23+
version: 3.8.5
2424
plotly:
2525
description: How to make Histograms in Python with Plotly.
2626
display_as: statistical
@@ -30,9 +30,9 @@ jupyter:
3030
order: 3
3131
page_type: example_index
3232
permalink: python/histograms/
33-
redirect_from:
34-
- /python/histogram-tutorial/
35-
- /python/histogram/
33+
redirect_from:
34+
- /python/histogram-tutorial/
35+
- /python/histogram/
3636
thumbnail: thumbnail/histogram.jpg
3737
---
3838

@@ -392,6 +392,21 @@ fig.update_layout(
392392
fig.show()
393393
```
394394

395+
### Sort Histogram by Category Order
396+
397+
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.
398+
399+
```python
400+
import plotly.express as px
401+
import pandas as pd
402+
403+
df = px.data.tips()
404+
fig1 = px.histogram(df, x="day").update_xaxes(categoryorder='total ascending')
405+
fig2 = px.histogram(df, x="day", color="smoker").update_xaxes(categoryorder='total descending')
406+
fig1.show()
407+
fig2.show()
408+
```
409+
395410
#### Reference
396411

397412
See https://plotly.com/python/reference/histogram/ for more information and chart attribute options!

0 commit comments

Comments
 (0)