Skip to content

Commit 319b91d

Browse files
Merge pull request #160 from plotly/chriddyp-patch-2
add a descending by value bar chart example
2 parents 1bf7baa + 73fb620 commit 319b91d

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

python/bar-charts.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,15 @@ fig.show()
329329

330330
### Bar Chart with Sorted or Ordered Categories
331331

332-
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+
Set `categoryorder` to `"category ascending"` or `"category descending"` for the alphanumerical order of the category names or `"total ascending"` or `"total descending"` for numerical order of values. [categoryorder](https://plot.ly/python/reference/#layout-xaxis-categoryorder) for more information. Note that sorting the bars by a particular trace isn't possible right now - it's only possible to sort by the total values. Of course, you can always sort your data _before_ plotting it if you need more customization.
333+
334+
This example orders the bar chart alphabetically with `categoryorder: 'category ascending'`
333335

334336
```python
335337
import plotly.graph_objects as go
336338

337-
x=['a','b','c','d']
338-
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal'))
339+
x=['b', 'a', 'c', 'd']
340+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
339341
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
340342
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
341343

@@ -348,15 +350,29 @@ This example shows how to customise sort ordering by defining `categoryorder` to
348350
```python
349351
import plotly.graph_objects as go
350352

351-
x=['a','b','c','d']
352-
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal'))
353+
x=['b', 'a', 'c', 'd']
354+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
353355
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
354356
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
355357

356358
fig.update_layout(barmode='stack', xaxis={'categoryorder':'array', 'categoryarray':['d','a','c','b']})
357359
fig.show()
358360
```
359361

362+
This example orders the bar chart by descending value with `categoryorder: 'total descending'`
363+
364+
```python
365+
import plotly.graph_objects as go
366+
367+
x=['b', 'a', 'c', 'd']
368+
fig = go.Figure(go.Bar(x=x, y=[2,5,1,9], name='Montreal'))
369+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16], name='Ottawa'))
370+
fig.add_trace(go.Bar(x=x, y=[6, 8, 4.5, 8], name='Toronto'))
371+
372+
fig.update_layout(barmode='stack', xaxis={'categoryorder':'total descending'})
373+
fig.show()
374+
```
375+
360376
### Horizontal Bar Charts
361377

362378
See examples of horizontal bar charts [here](https://plot.ly/python/horizontal-bar-charts/).

0 commit comments

Comments
 (0)