Skip to content

Commit e5d8c1e

Browse files
committed
horizontal-bar-charts notebook
1 parent 4c5740b commit e5d8c1e

File tree

1 file changed

+45
-99
lines changed

1 file changed

+45
-99
lines changed

notebooks/horizontal-bar-charts.md

Lines changed: 45 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
78
format_version: '1.1'
89
jupytext_version: 1.1.1
910
kernelspec:
10-
display_name: Python 2
11+
display_name: Python 3
1112
language: python
12-
name: python2
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1324
plotly:
1425
description: How to make horizontal bar charts in Python with Plotly.
1526
display_as: basic
@@ -25,77 +36,57 @@ jupyter:
2536
title: Horizontal Bar Charts | plotly
2637
---
2738

28-
#### New to Plotly?
29-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
30-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
31-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
32-
#### Version Check
33-
Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version.
39+
See more examples of bar charts (including vertical bar charts) and styling options [here](https://plot.ly/python/bar-charts/).
3440

35-
```python
36-
import plotly
37-
plotly.__version__
38-
```
3941

4042
#### Basic Horizontal Bar Chart
4143

4244
```python
43-
import plotly.plotly as py
4445
import plotly.graph_objs as go
4546

4647
data = [go.Bar(
4748
x=[20, 14, 23],
4849
y=['giraffes', 'orangutans', 'monkeys'],
49-
orientation = 'h'
50+
orientation='h'
5051
)]
5152

52-
py.iplot(data, filename='horizontal-bar')
53+
fig = go.Figure(data=data)
54+
fig.show()
5355
```
5456

5557
### Colored Horizontal Bar Chart
5658

5759
```python
58-
import plotly.plotly as py
5960
import plotly.graph_objs as go
6061

61-
trace1 = go.Bar(
62+
trace0 = go.Bar(
6263
y=['giraffes', 'orangutans', 'monkeys'],
6364
x=[20, 14, 23],
6465
name='SF Zoo',
65-
orientation = 'h',
66-
marker = dict(
67-
color = 'rgba(246, 78, 139, 0.6)',
68-
line = dict(
69-
color = 'rgba(246, 78, 139, 1.0)',
70-
width = 3)
66+
orientation='h',
67+
marker=dict(
68+
color='rgba(246, 78, 139, 0.6)',
69+
line=dict(color='rgba(246, 78, 139, 1.0)', width=3)
7170
)
7271
)
73-
trace2 = go.Bar(
72+
trace1 = go.Bar(
7473
y=['giraffes', 'orangutans', 'monkeys'],
7574
x=[12, 18, 29],
7675
name='LA Zoo',
77-
orientation = 'h',
78-
marker = dict(
79-
color = 'rgba(58, 71, 80, 0.6)',
80-
line = dict(
81-
color = 'rgba(58, 71, 80, 1.0)',
82-
width = 3)
76+
orientation='h',
77+
marker=dict(
78+
color='rgba(58, 71, 80, 0.6)',
79+
line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
8380
)
8481
)
8582

86-
data = [trace1, trace2]
87-
layout = go.Layout(
88-
barmode='stack'
89-
)
90-
91-
fig = go.Figure(data=data, layout=layout)
92-
py.iplot(fig, filename='marker-h-bar')
83+
fig = go.Figure(data=[trace0, trace1])
84+
fig.update(layout_barmode='stack')
9385
```
9486

9587
### Color Palette for Bar Chart
9688

9789
```python
98-
import plotly.plotly as py
9990
import plotly.graph_objs as go
10091

10192
top_labels = ['Strongly<br>agree', 'Agree', 'Neutral', 'Disagree',
@@ -122,14 +113,11 @@ traces = []
122113
for i in range(0, len(x_data[0])):
123114
for xd, yd in zip(x_data, y_data):
124115
traces.append(go.Bar(
125-
x=[xd[i]],
126-
y=[yd],
116+
x=[xd[i]], y=[yd],
127117
orientation='h',
128118
marker=dict(
129119
color=colors[i],
130-
line=dict(
131-
color='rgb(248, 248, 249)',
132-
width=1)
120+
line=dict(color='rgb(248, 248, 249)', width=1)
133121
)
134122
))
135123

@@ -150,12 +138,7 @@ layout = go.Layout(
150138
barmode='stack',
151139
paper_bgcolor='rgb(248, 248, 255)',
152140
plot_bgcolor='rgb(248, 248, 255)',
153-
margin=dict(
154-
l=120,
155-
r=10,
156-
t=140,
157-
b=80
158-
),
141+
margin=dict(l=120, r=10, t=140, b=80),
159142
showlegend=False,
160143
)
161144

@@ -204,18 +187,16 @@ for yd, xd in zip(y_data, x_data):
204187
showarrow=False))
205188
space += xd[i]
206189

207-
layout['annotations'] = annotations
208-
190+
layout.update(annotations=annotations)
209191
fig = go.Figure(data=traces, layout=layout)
210-
py.iplot(fig, filename='bar-colorscale')
192+
fig.show()
211193
```
212194

213195
### Bar Chart with Line Plot
214196

215197
```python
216-
import plotly.plotly as py
217198
import plotly.graph_objs as go
218-
from plotly import tools
199+
from plotly.subplots import make_subplots
219200

220201
import numpy as np
221202

@@ -243,11 +224,9 @@ trace0 = go.Bar(
243224
orientation='h',
244225
)
245226
trace1 = go.Scatter(
246-
x=y_net_worth,
247-
y=x_net_worth,
227+
x=y_net_worth, y=x_net_worth,
248228
mode='lines+markers',
249-
line=dict(
250-
color='rgb(128, 0, 128)'),
229+
line_color='rgb(128, 0, 128)',
251230
name='Household net worth, Million USD/capita',
252231
)
253232
layout = dict(
@@ -282,19 +261,8 @@ layout = dict(
282261
side='top',
283262
dtick=25000,
284263
),
285-
legend=dict(
286-
x=0.029,
287-
y=1.038,
288-
font=dict(
289-
size=10,
290-
),
291-
),
292-
margin=dict(
293-
l=100,
294-
r=20,
295-
t=70,
296-
b=70,
297-
),
264+
legend=dict(x=0.029, y=1.038, font_size=10),
265+
margin=dict(l=100, r=20, t=70, b=70),
298266
paper_bgcolor='rgb(248, 248, 255)',
299267
plot_bgcolor='rgb(248, 248, 255)',
300268
)
@@ -327,44 +295,22 @@ annotations.append(dict(xref='paper', yref='paper',
327295
'(2015), Household savings (indicator), ' +
328296
'Household net worth (indicator). doi: ' +
329297
'10.1787/cfc6f499-en (Accessed on 05 June 2015)',
330-
font=dict(family='Arial', size=10,
331-
color='rgb(150,150,150)'),
298+
font=dict(family='Arial', size=10, color='rgb(150,150,150)'),
332299
showarrow=False))
333300

334-
layout['annotations'] = annotations
301+
layout.update(annotations=annotations)
335302

336303
# Creating two subplots
337-
fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
338-
shared_yaxes=False, vertical_spacing=0.001)
304+
fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
305+
shared_yaxes=False, vertical_spacing=0.001)
339306

340307
fig.append_trace(trace0, 1, 1)
341308
fig.append_trace(trace1, 1, 2)
342309

343-
fig['layout'].update(layout)
344-
py.iplot(fig, filename='oecd-networth-saving-bar-line')
310+
fig.update(layout=layout)
311+
fig.show()
345312
```
346313

347314
### Reference
348315
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!
349316

350-
```python
351-
from IPython.display import display, HTML
352-
353-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
354-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
355-
356-
#! pip install git+https://github.com/plotly/publisher.git --upgrade
357-
import publisher
358-
publisher.publish(
359-
'horizontal-bars.ipynb', 'python/horizontal-bar-charts/', 'Horizontal Bar Charts | plotly',
360-
'How to make horizontal bar charts in Python with Plotly.',
361-
title = 'Horizontal Bar Charts | plotly',
362-
name = 'Horizontal Bar Charts',
363-
thumbnail='thumbnail/horizontal-bar.jpg', language='python',
364-
has_thumbnail='true', display_as='basic', order=5,
365-
ipynb= '~notebook_demo/5')
366-
```
367-
368-
```python
369-
370-
```

0 commit comments

Comments
 (0)