Skip to content

Commit 2b32640

Browse files
authored
Merge pull request #112 from plotly/categoryorder
categoryorder in python bar chart
2 parents 747bc88 + 403dc87 commit 2b32640

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

python/bar-charts.md

Lines changed: 32 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.1'
9-
jupytext_version: 1.1.1
9+
jupytext_version: 1.2.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.3
2424
plotly:
2525
description: How to make Bar Charts in Python with Plotly.
2626
display_as: basic
@@ -326,6 +326,36 @@ fig.update_layout(barmode='relative', title_text='Relative Barmode')
326326
fig.show()
327327
```
328328

329+
### Ordering Categorical Variables
330+
331+
Set `categoryorder` to "category ascending/descending" for the alphanumerical order of the category names or "total ascending/descending" for numerical order of values. [categoryorder](https://plot.ly/python/reference/#layout-xaxis-categoryorder) for more information.
332+
333+
```python
334+
import plotly.graph_objects as go
335+
336+
x=['a','b','c','d']
337+
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal'))
338+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
339+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
340+
341+
fig.update_layout(barmode='stack', xaxis={'categoryorder':'category ascending'})
342+
fig.show()
343+
```
344+
345+
This example shows how to customise ordering by defining `categoryorder` to "array" to derive the ordering from the attribute `categoryarray`.
346+
347+
```python
348+
import plotly.graph_objects as go
349+
350+
x=['a','b','c','d']
351+
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal'))
352+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
353+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
354+
355+
fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
356+
fig.show()
357+
```
358+
329359
### Horizontal Bar Charts
330360
See examples of horizontal bar charts [here](https://plot.ly/python/horizontal-bar-charts/).
331361

0 commit comments

Comments
 (0)