Skip to content

Commit 72c04e0

Browse files
committed
addressed comments, added px example
1 parent e5d8c1e commit 72c04e0

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

notebooks/horizontal-bar-charts.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,56 @@ jupyter:
3434
permalink: python/horizontal-bar-charts/
3535
thumbnail: thumbnail/horizontal-bar.jpg
3636
title: Horizontal Bar Charts | plotly
37+
v4upgrade: true
3738
---
3839

3940
See more examples of bar charts (including vertical bar charts) and styling options [here](https://plot.ly/python/bar-charts/).
4041

4142

43+
### Horizontal Bar Chart with plotly express
44+
45+
Plotly express functions take as argument a tidy [pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html). For a horizontal bar char, use the `px.bar` function with `orientation='h'`.
46+
47+
48+
#### Basic Horizontal Bar Chart with plotly express
49+
50+
```python
51+
import plotly_express as px
52+
tips = px.data.tips()
53+
fig = px.bar(tips, x="total_bill", y="day", orientation='h')
54+
fig.show()
55+
```
56+
57+
#### Configure horizontal bar chart
58+
59+
In this example a column is used to color the bars, and we add the information from other columns to the hover data.
60+
61+
```python
62+
import plotly_express as px
63+
tips = px.data.tips()
64+
fig = px.bar(tips, x="total_bill", y="sex", color='day', orientation='h',
65+
hover_data=["tip", "size"],
66+
height=400,
67+
title='Restaurant bills')
68+
fig.show()
69+
```
70+
71+
### Horizontal Bar Chart with go.Bar
72+
73+
When data are not available as a tidy dataframe, you can use the more generic function `go.Bar` from `plotly.graph_objs`. All the options of `go.Bar` are documented in the reference https://plot.ly/python/reference/#bar
74+
75+
4276
#### Basic Horizontal Bar Chart
4377

4478
```python
4579
import plotly.graph_objs as go
4680

47-
data = [go.Bar(
81+
trace = go.Bar(
4882
x=[20, 14, 23],
4983
y=['giraffes', 'orangutans', 'monkeys'],
50-
orientation='h'
51-
)]
84+
orientation='h')
5285

53-
fig = go.Figure(data=data)
86+
fig = go.Figure(data=[trace])
5487
fig.show()
5588
```
5689

0 commit comments

Comments
 (0)