@@ -8,9 +8,19 @@ jupyter:
8
8
format_version : ' 1.1'
9
9
jupytext_version : 1.1.1
10
10
kernelspec :
11
- display_name : Python 2
11
+ display_name : Python 3
12
12
language : python
13
- 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
14
24
plotly :
15
25
description : How to make horizontal bar charts in Python with Plotly.
16
26
display_as : basic
@@ -24,80 +34,94 @@ jupyter:
24
34
permalink : python/horizontal-bar-charts/
25
35
thumbnail : thumbnail/horizontal-bar.jpg
26
36
title : Horizontal Bar Charts | plotly
37
+ v4upgrade : true
27
38
---
28
39
29
- #### New to Plotly?
30
- 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/ ) .
31
- <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 ) .
32
- <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!
33
- #### Version Check
34
- Plotly's python package is updated frequently. Run ` pip install plotly --upgrade ` to use the latest version.
40
+ See more examples of bar charts (including vertical bar charts) and styling options [ here] ( https://plot.ly/python/bar-charts/ ) .
41
+
42
+
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
+
51
+ ``` python
52
+ import plotly.express as px
53
+ tips = px.data.tips()
54
+ fig = px.bar(tips, x = " total_bill" , y = " day" , orientation = ' h' )
55
+ fig.show()
56
+ ```
57
+
58
+ #### Configure horizontal bar chart
59
+
60
+ In this example a column is used to color the bars, and we add the information from other columns to the hover data.
35
61
36
62
``` python
37
- import plotly
38
- plotly.__version__
63
+ import plotly.express as px
64
+ tips = px.data.tips()
65
+ fig = px.bar(tips, x = " total_bill" , y = " sex" , color = ' day' , orientation = ' h' ,
66
+ hover_data = [" tip" , " size" ],
67
+ height = 400 ,
68
+ title = ' Restaurant bills' )
69
+ fig.show()
39
70
```
40
71
72
+ ### Horizontal Bar Chart with go.Bar
73
+
74
+ 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
75
+
76
+
41
77
#### Basic Horizontal Bar Chart
42
78
43
79
``` python
44
- import plotly.plotly as py
45
- import plotly.graph_objs as go
80
+ import plotly.graph_objects as go
46
81
47
- data = [ go.Bar(
82
+ fig = go.Figure( go.Bar(
48
83
x = [20 , 14 , 23 ],
49
84
y = [' giraffes' , ' orangutans' , ' monkeys' ],
50
- orientation = ' h'
51
- )]
85
+ orientation = ' h' ))
52
86
53
- py.iplot(data, filename = ' horizontal-bar ' )
87
+ fig.show( )
54
88
```
55
89
56
90
### Colored Horizontal Bar Chart
57
91
58
92
``` python
59
- import plotly.plotly as py
60
- import plotly.graph_objs as go
93
+ import plotly.graph_objects as go
61
94
62
- trace1 = go.Bar(
95
+ fig = go.Figure()
96
+ fig.add_trace(go.Bar(
63
97
y = [' giraffes' , ' orangutans' , ' monkeys' ],
64
98
x = [20 , 14 , 23 ],
65
99
name = ' SF Zoo' ,
66
- orientation = ' h' ,
67
- marker = dict (
68
- color = ' rgba(246, 78, 139, 0.6)' ,
69
- line = dict (
70
- color = ' rgba(246, 78, 139, 1.0)' ,
71
- width = 3 )
100
+ orientation = ' h' ,
101
+ marker = dict (
102
+ color = ' rgba(246, 78, 139, 0.6)' ,
103
+ line = dict (color = ' rgba(246, 78, 139, 1.0)' , width = 3 )
72
104
)
73
- )
74
- trace2 = go.Bar(
105
+ ))
106
+ fig.add_trace( go.Bar(
75
107
y = [' giraffes' , ' orangutans' , ' monkeys' ],
76
108
x = [12 , 18 , 29 ],
77
109
name = ' LA Zoo' ,
78
- orientation = ' h' ,
79
- marker = dict (
80
- color = ' rgba(58, 71, 80, 0.6)' ,
81
- line = dict (
82
- color = ' rgba(58, 71, 80, 1.0)' ,
83
- width = 3 )
110
+ orientation = ' h' ,
111
+ marker = dict (
112
+ color = ' rgba(58, 71, 80, 0.6)' ,
113
+ line = dict (color = ' rgba(58, 71, 80, 1.0)' , width = 3 )
84
114
)
85
- )
86
-
87
- data = [trace1, trace2]
88
- layout = go.Layout(
89
- barmode = ' stack'
90
- )
115
+ ))
91
116
92
- fig = go.Figure( data = data, layout = layout )
93
- py.iplot(fig, filename = ' marker-h-bar ' )
117
+ fig.update_layout( barmode = ' stack ' )
118
+ fig.show( )
94
119
```
95
120
96
121
### Color Palette for Bar Chart
97
122
98
123
``` python
99
- import plotly.plotly as py
100
- import plotly.graph_objs as go
124
+ import plotly.graph_objects as go
101
125
102
126
top_labels = [' Strongly<br>agree' , ' Agree' , ' Neutral' , ' Disagree' ,
103
127
' Strongly<br>disagree' ]
@@ -117,24 +141,20 @@ y_data = ['The course was effectively<br>organized',
117
141
' my<br>ability to think critically about<br>the subject' ,
118
142
' I would recommend this<br>course to a friend' ]
119
143
120
-
121
- traces = []
144
+ fig = go.Figure()
122
145
123
146
for i in range (0 , len (x_data[0 ])):
124
147
for xd, yd in zip (x_data, y_data):
125
- traces.append(go.Bar(
126
- x = [xd[i]],
127
- y = [yd],
148
+ fig.add_trace(go.Bar(
149
+ x = [xd[i]], y = [yd],
128
150
orientation = ' h' ,
129
151
marker = dict (
130
152
color = colors[i],
131
- line = dict (
132
- color = ' rgb(248, 248, 249)' ,
133
- width = 1 )
153
+ line = dict (color = ' rgb(248, 248, 249)' , width = 1 )
134
154
)
135
155
))
136
156
137
- layout = go.Layout (
157
+ fig.update_layout (
138
158
xaxis = dict (
139
159
showgrid = False ,
140
160
showline = False ,
@@ -151,12 +171,7 @@ layout = go.Layout(
151
171
barmode = ' stack' ,
152
172
paper_bgcolor = ' rgb(248, 248, 255)' ,
153
173
plot_bgcolor = ' rgb(248, 248, 255)' ,
154
- margin = dict (
155
- l = 120 ,
156
- r = 10 ,
157
- t = 140 ,
158
- b = 80
159
- ),
174
+ margin = dict (l = 120 , r = 10 , t = 140 , b = 80 ),
160
175
showlegend = False ,
161
176
)
162
177
@@ -205,18 +220,16 @@ for yd, xd in zip(y_data, x_data):
205
220
showarrow = False ))
206
221
space += xd[i]
207
222
208
- layout[ ' annotations' ] = annotations
223
+ fig.update_layout( annotations = annotations)
209
224
210
- fig = go.Figure(data = traces, layout = layout)
211
- py.iplot(fig, filename = ' bar-colorscale' )
225
+ fig.show()
212
226
```
213
227
214
228
### Bar Chart with Line Plot
215
229
216
230
``` python
217
- import plotly.plotly as py
218
- import plotly.graph_objs as go
219
- from plotly import tools
231
+ import plotly.graph_objects as go
232
+ from plotly.subplots import make_subplots
220
233
221
234
import numpy as np
222
235
@@ -226,14 +239,17 @@ y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
226
239
y_net_worth = [93453.919999999998 , 81666.570000000007 , 69889.619999999995 ,
227
240
78381.529999999999 , 141395.29999999999 , 92969.020000000004 ,
228
241
66090.179999999993 , 122379.3 ]
229
- x_saving = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
230
- ' United States' , ' Belgium' , ' Sweden' , ' Switzerland' ]
231
- x_net_worth = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
232
- ' United States' , ' Belgium' , ' Sweden' , ' Switzerland'
233
- ]
234
- trace0 = go.Bar(
242
+ x = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
243
+ ' United States' , ' Belgium' , ' Sweden' , ' Switzerland' ]
244
+
245
+
246
+ # Creating two subplots
247
+ fig = make_subplots(rows = 1 , cols = 2 , specs = [[{}, {}]], shared_xaxes = True ,
248
+ shared_yaxes = False , vertical_spacing = 0.001 )
249
+
250
+ fig.append_trace(go.Bar(
235
251
x = y_saving,
236
- y = x_saving ,
252
+ y = x ,
237
253
marker = dict (
238
254
color = ' rgba(50, 171, 96, 0.6)' ,
239
255
line = dict (
@@ -242,16 +258,16 @@ trace0 = go.Bar(
242
258
),
243
259
name = ' Household savings, percentage of household disposable income' ,
244
260
orientation = ' h' ,
245
- )
246
- trace1 = go.Scatter(
247
- x = y_net_worth,
248
- y = x_net_worth ,
261
+ ), 1 , 1 )
262
+
263
+ fig.append_trace(go.Scatter(
264
+ x = y_net_worth, y = x ,
249
265
mode = ' lines+markers' ,
250
- line = dict (
251
- color = ' rgb(128, 0, 128)' ),
266
+ line_color = ' rgb(128, 0, 128)' ,
252
267
name = ' Household net worth, Million USD/capita' ,
253
- )
254
- layout = dict (
268
+ ), 1 , 2 )
269
+
270
+ fig.update_layout(
255
271
title = ' Household savings & net worth for eight OECD countries' ,
256
272
yaxis = dict (
257
273
showgrid = False ,
@@ -283,19 +299,8 @@ layout = dict(
283
299
side = ' top' ,
284
300
dtick = 25000 ,
285
301
),
286
- legend = dict (
287
- x = 0.029 ,
288
- y = 1.038 ,
289
- font = dict (
290
- size = 10 ,
291
- ),
292
- ),
293
- margin = dict (
294
- l = 100 ,
295
- r = 20 ,
296
- t = 70 ,
297
- b = 70 ,
298
- ),
302
+ legend = dict (x = 0.029 , y = 1.038 , font_size = 10 ),
303
+ margin = dict (l = 100 , r = 20 , t = 70 , b = 70 ),
299
304
paper_bgcolor = ' rgb(248, 248, 255)' ,
300
305
plot_bgcolor = ' rgb(248, 248, 255)' ,
301
306
)
@@ -306,7 +311,7 @@ y_s = np.round(y_saving, decimals=2)
306
311
y_nw = np.rint(y_net_worth)
307
312
308
313
# Adding labels
309
- for ydn, yd, xd in zip (y_nw, y_s, x_saving ):
314
+ for ydn, yd, xd in zip (y_nw, y_s, x ):
310
315
# labeling the scatter savings
311
316
annotations.append(dict (xref = ' x2' , yref = ' y2' ,
312
317
y = xd, x = ydn - 20000 ,
@@ -328,44 +333,13 @@ annotations.append(dict(xref='paper', yref='paper',
328
333
' (2015), Household savings (indicator), ' +
329
334
' Household net worth (indicator). doi: ' +
330
335
' 10.1787/cfc6f499-en (Accessed on 05 June 2015)' ,
331
- font = dict (family = ' Arial' , size = 10 ,
332
- color = ' rgb(150,150,150)' ),
336
+ font = dict (family = ' Arial' , size = 10 , color = ' rgb(150,150,150)' ),
333
337
showarrow = False ))
334
338
335
- layout[' annotations' ] = annotations
336
-
337
- # Creating two subplots
338
- fig = tools.make_subplots(rows = 1 , cols = 2 , specs = [[{}, {}]], shared_xaxes = True ,
339
- shared_yaxes = False , vertical_spacing = 0.001 )
340
-
341
- fig.append_trace(trace0, 1 , 1 )
342
- fig.append_trace(trace1, 1 , 2 )
339
+ fig.update_layout(annotations = annotations)
343
340
344
- fig[' layout' ].update(layout)
345
- py.iplot(fig, filename = ' oecd-networth-saving-bar-line' )
341
+ fig.show()
346
342
```
347
343
348
344
### Reference
349
345
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!
350
-
351
- ``` python
352
- from IPython.display import display, HTML
353
-
354
- 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" />' ))
355
- display(HTML(' <link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
356
-
357
- # ! pip install git+https://github.com/plotly/publisher.git --upgrade
358
- import publisher
359
- publisher.publish(
360
- ' horizontal-bars.ipynb' , ' python/horizontal-bar-charts/' , ' Horizontal Bar Charts | plotly' ,
361
- ' How to make horizontal bar charts in Python with Plotly.' ,
362
- title = ' Horizontal Bar Charts | plotly' ,
363
- name = ' Horizontal Bar Charts' ,
364
- thumbnail = ' thumbnail/horizontal-bar.jpg' , language = ' python' ,
365
- has_thumbnail = ' true' , display_as = ' basic' , order = 5 ,
366
- ipynb = ' ~notebook_demo/5' )
367
- ```
368
-
369
- ``` python
370
-
371
- ```
0 commit comments