Skip to content

[MRG] horizontal-bar-charts notebook #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
addressed comments, added px example
  • Loading branch information
emmanuelle committed Jun 13, 2019
commit 72c04e05f6e81f761822f436fc8d8ca19b602e5d
41 changes: 37 additions & 4 deletions notebooks/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,56 @@ jupyter:
permalink: python/horizontal-bar-charts/
thumbnail: thumbnail/horizontal-bar.jpg
title: Horizontal Bar Charts | plotly
v4upgrade: true
---

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


### Horizontal Bar Chart with plotly express

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'`.


#### Basic Horizontal Bar Chart with plotly express

```python
import plotly_express as px
tips = px.data.tips()
fig = px.bar(tips, x="total_bill", y="day", orientation='h')
fig.show()
```

#### Configure horizontal bar chart

In this example a column is used to color the bars, and we add the information from other columns to the hover data.

```python
import plotly_express as px
tips = px.data.tips()
fig = px.bar(tips, x="total_bill", y="sex", color='day', orientation='h',
hover_data=["tip", "size"],
height=400,
title='Restaurant bills')
fig.show()
```

### Horizontal Bar Chart with go.Bar

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


#### Basic Horizontal Bar Chart

```python
import plotly.graph_objs as go

data = [go.Bar(
trace = go.Bar(
x=[20, 14, 23],
y=['giraffes', 'orangutans', 'monkeys'],
orientation='h'
)]
orientation='h')

fig = go.Figure(data=data)
fig = go.Figure(data=[trace])
fig.show()
```

Expand Down