Skip to content

Commit 7e15fdf

Browse files
authored
add a descending by value bar chart example
and other fixes
1 parent 1bf7baa commit 7e15fdf

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

python/bar-charts.md

Lines changed: 20 additions & 4 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 bars 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

@@ -349,14 +351,28 @@ This example shows how to customise sort ordering by defining `categoryorder` to
349351
import plotly.graph_objects as go
350352

351353
x=['a','b','c','d']
352-
fig = go.Figure(go.Bar(x =x, y=[2,5,1,9], name='Montreal'))
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 bars 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)