@@ -48,7 +48,7 @@ Plotly express functions take as argument a tidy [pandas DataFrame](https://pand
48
48
#### Basic Horizontal Bar Chart with plotly express
49
49
50
50
``` python
51
- import plotly_express as px
51
+ import plotly.express as px
52
52
tips = px.data.tips()
53
53
fig = px.bar(tips, x = " total_bill" , y = " day" , orientation = ' h' )
54
54
fig.show()
@@ -59,7 +59,7 @@ fig.show()
59
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
60
61
61
``` python
62
- import plotly_express as px
62
+ import plotly.express as px
63
63
tips = px.data.tips()
64
64
fig = px.bar(tips, x = " total_bill" , y = " sex" , color = ' day' , orientation = ' h' ,
65
65
hover_data = [" tip" , " size" ],
@@ -70,29 +70,29 @@ fig.show()
70
70
71
71
### Horizontal Bar Chart with go.Bar
72
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
73
+ 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
74
74
75
75
76
76
#### Basic Horizontal Bar Chart
77
77
78
78
``` python
79
- import plotly.graph_objs as go
79
+ import plotly.graph_objects as go
80
80
81
- trace = go.Bar(
81
+ fig = go.Figure( go.Bar(
82
82
x = [20 , 14 , 23 ],
83
83
y = [' giraffes' , ' orangutans' , ' monkeys' ],
84
- orientation = ' h' )
84
+ orientation = ' h' ))
85
85
86
- fig = go.Figure(data = [trace])
87
86
fig.show()
88
87
```
89
88
90
89
### Colored Horizontal Bar Chart
91
90
92
91
``` python
93
- import plotly.graph_objs as go
92
+ import plotly.graph_objects as go
94
93
95
- trace0 = go.Bar(
94
+ fig = go.Figure()
95
+ fig.add_trace(go.Bar(
96
96
y = [' giraffes' , ' orangutans' , ' monkeys' ],
97
97
x = [20 , 14 , 23 ],
98
98
name = ' SF Zoo' ,
@@ -101,8 +101,8 @@ trace0 = go.Bar(
101
101
color = ' rgba(246, 78, 139, 0.6)' ,
102
102
line = dict (color = ' rgba(246, 78, 139, 1.0)' , width = 3 )
103
103
)
104
- )
105
- trace1 = go.Bar(
104
+ ))
105
+ fig.add_trace( go.Bar(
106
106
y = [' giraffes' , ' orangutans' , ' monkeys' ],
107
107
x = [12 , 18 , 29 ],
108
108
name = ' LA Zoo' ,
@@ -111,16 +111,16 @@ trace1 = go.Bar(
111
111
color = ' rgba(58, 71, 80, 0.6)' ,
112
112
line = dict (color = ' rgba(58, 71, 80, 1.0)' , width = 3 )
113
113
)
114
- )
114
+ ))
115
115
116
- fig = go.Figure( data = [trace0, trace1] )
117
- fig.update( layout_barmode = ' stack ' )
116
+ fig.update_layout( barmode = ' stack ' )
117
+ fig.show( )
118
118
```
119
119
120
120
### Color Palette for Bar Chart
121
121
122
122
``` python
123
- import plotly.graph_objs as go
123
+ import plotly.graph_objects as go
124
124
125
125
top_labels = [' Strongly<br>agree' , ' Agree' , ' Neutral' , ' Disagree' ,
126
126
' Strongly<br>disagree' ]
@@ -140,12 +140,11 @@ y_data = ['The course was effectively<br>organized',
140
140
' my<br>ability to think critically about<br>the subject' ,
141
141
' I would recommend this<br>course to a friend' ]
142
142
143
-
144
- traces = []
143
+ fig = go.Figure()
145
144
146
145
for i in range (0 , len (x_data[0 ])):
147
146
for xd, yd in zip (x_data, y_data):
148
- traces.append (go.Bar(
147
+ fig.add_trace (go.Bar(
149
148
x = [xd[i]], y = [yd],
150
149
orientation = ' h' ,
151
150
marker = dict (
@@ -154,7 +153,7 @@ for i in range(0, len(x_data[0])):
154
153
)
155
154
))
156
155
157
- layout = go.Layout (
156
+ fig.update_layout (
158
157
xaxis = dict (
159
158
showgrid = False ,
160
159
showline = False ,
@@ -220,15 +219,15 @@ for yd, xd in zip(y_data, x_data):
220
219
showarrow = False ))
221
220
space += xd[i]
222
221
223
- layout.update (annotations = annotations)
224
- fig = go.Figure( data = traces, layout = layout)
222
+ fig.update_layout (annotations = annotations)
223
+
225
224
fig.show()
226
225
```
227
226
228
227
### Bar Chart with Line Plot
229
228
230
229
``` python
231
- import plotly.graph_objs as go
230
+ import plotly.graph_objects as go
232
231
from plotly.subplots import make_subplots
233
232
234
233
import numpy as np
@@ -239,14 +238,17 @@ y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
239
238
y_net_worth = [93453.919999999998 , 81666.570000000007 , 69889.619999999995 ,
240
239
78381.529999999999 , 141395.29999999999 , 92969.020000000004 ,
241
240
66090.179999999993 , 122379.3 ]
242
- x_saving = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
243
- ' United States' , ' Belgium' , ' Sweden' , ' Switzerland' ]
244
- x_net_worth = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
245
- ' United States' , ' Belgium' , ' Sweden' , ' Switzerland'
246
- ]
247
- trace0 = go.Bar(
241
+ x = [' Japan' , ' United Kingdom' , ' Canada' , ' Netherlands' ,
242
+ ' United States' , ' Belgium' , ' Sweden' , ' Switzerland' ]
243
+
244
+
245
+ # Creating two subplots
246
+ fig = make_subplots(rows = 1 , cols = 2 , specs = [[{}, {}]], shared_xaxes = True ,
247
+ shared_yaxes = False , vertical_spacing = 0.001 )
248
+
249
+ fig.append_trace(go.Bar(
248
250
x = y_saving,
249
- y = x_saving ,
251
+ y = x ,
250
252
marker = dict (
251
253
color = ' rgba(50, 171, 96, 0.6)' ,
252
254
line = dict (
@@ -255,14 +257,16 @@ trace0 = go.Bar(
255
257
),
256
258
name = ' Household savings, percentage of household disposable income' ,
257
259
orientation = ' h' ,
258
- )
259
- trace1 = go.Scatter(
260
- x = y_net_worth, y = x_net_worth,
260
+ ), 1 , 1 )
261
+
262
+ fig.append_trace(go.Scatter(
263
+ x = y_net_worth, y = x,
261
264
mode = ' lines+markers' ,
262
265
line_color = ' rgb(128, 0, 128)' ,
263
266
name = ' Household net worth, Million USD/capita' ,
264
- )
265
- layout = dict (
267
+ ), 1 , 2 )
268
+
269
+ fig.update_layout(
266
270
title = ' Household savings & net worth for eight OECD countries' ,
267
271
yaxis = dict (
268
272
showgrid = False ,
@@ -306,7 +310,7 @@ y_s = np.round(y_saving, decimals=2)
306
310
y_nw = np.rint(y_net_worth)
307
311
308
312
# Adding labels
309
- for ydn, yd, xd in zip (y_nw, y_s, x_saving ):
313
+ for ydn, yd, xd in zip (y_nw, y_s, x ):
310
314
# labeling the scatter savings
311
315
annotations.append(dict (xref = ' x2' , yref = ' y2' ,
312
316
y = xd, x = ydn - 20000 ,
@@ -331,16 +335,8 @@ annotations.append(dict(xref='paper', yref='paper',
331
335
font = dict (family = ' Arial' , size = 10 , color = ' rgb(150,150,150)' ),
332
336
showarrow = False ))
333
337
334
- layout.update(annotations = annotations)
335
-
336
- # Creating two subplots
337
- fig = make_subplots(rows = 1 , cols = 2 , specs = [[{}, {}]], shared_xaxes = True ,
338
- shared_yaxes = False , vertical_spacing = 0.001 )
339
-
340
- fig.append_trace(trace0, 1 , 1 )
341
- fig.append_trace(trace1, 1 , 2 )
338
+ fig.update_layout(annotations = annotations)
342
339
343
- fig.update(layout = layout)
344
340
fig.show()
345
341
```
346
342
0 commit comments