- Notifications
You must be signed in to change notification settings - Fork1
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
228 changes: 101 additions & 127 deletionsnotebooks/horizontal-bar-charts.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -8,9 +8,19 @@ jupyter: | ||
format_version: '1.1' | ||
jupytext_version: 1.1.1 | ||
kernelspec: | ||
display_name: Python3 | ||
language: python | ||
name: python3 | ||
language_info: | ||
codemirror_mode: | ||
name: ipython | ||
version: 3 | ||
file_extension: .py | ||
mimetype: text/x-python | ||
name: python | ||
nbconvert_exporter: python | ||
pygments_lexer: ipython3 | ||
version: 3.6.7 | ||
plotly: | ||
description: How to make horizontal bar charts in Python with Plotly. | ||
display_as: basic | ||
@@ -24,80 +34,94 @@ 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_objects`. All the options of `go.Bar` are documented in the reference https://plot.ly/python/reference/#bar | ||
#### Basic Horizontal Bar Chart | ||
```python | ||
emmanuelle marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
import plotly.graph_objects as go | ||
fig =go.Figure(go.Bar( | ||
x=[20, 14, 23], | ||
y=['giraffes', 'orangutans', 'monkeys'], | ||
orientation='h')) | ||
fig.show() | ||
``` | ||
### Colored Horizontal Bar Chart | ||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure() | ||
fig.add_trace(go.Bar( | ||
y=['giraffes', 'orangutans', 'monkeys'], | ||
x=[20, 14, 23], | ||
name='SF Zoo', | ||
orientation='h', | ||
marker=dict( | ||
color='rgba(246, 78, 139, 0.6)', | ||
line=dict(color='rgba(246, 78, 139, 1.0)', width=3) | ||
) | ||
)) | ||
fig.add_trace(go.Bar( | ||
y=['giraffes', 'orangutans', 'monkeys'], | ||
x=[12, 18, 29], | ||
name='LA Zoo', | ||
orientation='h', | ||
marker=dict( | ||
color='rgba(58, 71, 80, 0.6)', | ||
line=dict(color='rgba(58, 71, 80, 1.0)', width=3) | ||
) | ||
)) | ||
fig.update_layout(barmode='stack') | ||
fig.show() | ||
``` | ||
### Color Palette for Bar Chart | ||
```python | ||
import plotly.graph_objects as go | ||
top_labels = ['Strongly<br>agree', 'Agree', 'Neutral', 'Disagree', | ||
'Strongly<br>disagree'] | ||
@@ -117,24 +141,20 @@ y_data = ['The course was effectively<br>organized', | ||
'my<br>ability to think critically about<br>the subject', | ||
'I would recommend this<br>course to a friend'] | ||
fig = go.Figure() | ||
for i in range(0, len(x_data[0])): | ||
for xd, yd in zip(x_data, y_data): | ||
fig.add_trace(go.Bar( | ||
x=[xd[i]], y=[yd], | ||
orientation='h', | ||
marker=dict( | ||
color=colors[i], | ||
line=dict(color='rgb(248, 248, 249)', width=1) | ||
) | ||
)) | ||
fig.update_layout( | ||
xaxis=dict( | ||
showgrid=False, | ||
showline=False, | ||
@@ -151,12 +171,7 @@ layout = go.Layout( | ||
barmode='stack', | ||
paper_bgcolor='rgb(248, 248, 255)', | ||
plot_bgcolor='rgb(248, 248, 255)', | ||
margin=dict(l=120, r=10, t=140, b=80), | ||
showlegend=False, | ||
) | ||
@@ -205,18 +220,16 @@ for yd, xd in zip(y_data, x_data): | ||
showarrow=False)) | ||
space += xd[i] | ||
fig.update_layout(annotations=annotations) | ||
fig.show() | ||
``` | ||
### Bar Chart with Line Plot | ||
```python | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
import numpy as np | ||
@@ -226,14 +239,17 @@ y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996, | ||
y_net_worth = [93453.919999999998, 81666.570000000007, 69889.619999999995, | ||
78381.529999999999, 141395.29999999999, 92969.020000000004, | ||
66090.179999999993, 122379.3] | ||
x = ['Japan', 'United Kingdom', 'Canada', 'Netherlands', | ||
'United States', 'Belgium', 'Sweden', 'Switzerland'] | ||
# Creating two subplots | ||
fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True, | ||
shared_yaxes=False, vertical_spacing=0.001) | ||
fig.append_trace(go.Bar( | ||
x=y_saving, | ||
y=x, | ||
marker=dict( | ||
color='rgba(50, 171, 96, 0.6)', | ||
line=dict( | ||
@@ -242,16 +258,16 @@ trace0 = go.Bar( | ||
), | ||
name='Household savings, percentage of household disposable income', | ||
orientation='h', | ||
), 1, 1) | ||
fig.append_trace(go.Scatter( | ||
x=y_net_worth, y=x, | ||
mode='lines+markers', | ||
line_color='rgb(128, 0, 128)', | ||
name='Household net worth, Million USD/capita', | ||
), 1, 2) | ||
fig.update_layout( | ||
title='Household savings & net worth for eight OECD countries', | ||
yaxis=dict( | ||
showgrid=False, | ||
@@ -283,19 +299,8 @@ layout = dict( | ||
side='top', | ||
dtick=25000, | ||
), | ||
legend=dict(x=0.029, y=1.038, font_size=10), | ||
margin=dict(l=100, r=20, t=70, b=70), | ||
paper_bgcolor='rgb(248, 248, 255)', | ||
plot_bgcolor='rgb(248, 248, 255)', | ||
) | ||
@@ -306,7 +311,7 @@ y_s = np.round(y_saving, decimals=2) | ||
y_nw = np.rint(y_net_worth) | ||
# Adding labels | ||
for ydn, yd, xd in zip(y_nw, y_s,x): | ||
# labeling the scatter savings | ||
annotations.append(dict(xref='x2', yref='y2', | ||
y=xd, x=ydn - 20000, | ||
@@ -328,44 +333,13 @@ annotations.append(dict(xref='paper', yref='paper', | ||
'(2015), Household savings (indicator), ' + | ||
'Household net worth (indicator). doi: ' + | ||
'10.1787/cfc6f499-en (Accessed on 05 June 2015)', | ||
font=dict(family='Arial', size=10, color='rgb(150,150,150)'), | ||
showarrow=False)) | ||
fig.update_layout(annotations=annotations) | ||
fig.show() | ||
``` | ||
### Reference | ||
See more examples of bar charts and styling options [here](https://plot.ly/python/bar-charts/).<br> See https://plot.ly/python/reference/#bar for more information and chart attribute options! | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.