@@ -102,12 +102,12 @@ fig.show()
102
102
To learn more, see the * link to px.bar reference page* .
103
103
104
104
105
- #### Basic Bar Chart with plotly.graph_objs
105
+ #### Basic Bar Chart with plotly.graph_objects
106
106
107
- When data are not available as tidy dataframes, it is also possible to use the more generic ` go.Bar ` function from ` plotly.graph_objs ` .
107
+ When data are not available as tidy dataframes, it is also possible to use the more generic ` go.Bar ` function from ` plotly.graph_objects ` .
108
108
109
109
``` python
110
- import plotly.graph_objs as go
110
+ import plotly.graph_objects as go
111
111
animals= [' giraffes' , ' orangutans' , ' monkeys' ]
112
112
113
113
fig = go.Figure([go.Bar(x = animals, y = [20 , 14 , 23 ])])
@@ -119,35 +119,37 @@ fig.show()
119
119
Customize the figure using ` fig.update ` .
120
120
121
121
``` python
122
- import plotly.graph_objs as go
122
+ import plotly.graph_objects as go
123
123
animals= [' giraffes' , ' orangutans' , ' monkeys' ]
124
124
125
125
fig = go.Figure(data = [
126
126
go.Bar(name = ' SF Zoo' , x = animals, y = [20 , 14 , 23 ]),
127
127
go.Bar(name = ' LA Zoo' , x = animals, y = [12 , 18 , 29 ])
128
128
])
129
129
# Change the bar mode
130
- fig.update(layout_barmode = ' group' )
130
+ fig.update_layout(barmode = ' group' )
131
+ fig.show()
131
132
```
132
133
133
134
### Stacked Bar Chart
134
135
135
136
``` python
136
- import plotly.graph_objs as go
137
+ import plotly.graph_objects as go
137
138
animals= [' giraffes' , ' orangutans' , ' monkeys' ]
138
139
139
140
fig = go.Figure(data = [
140
141
go.Bar(name = ' SF Zoo' , x = animals, y = [20 , 14 , 23 ]),
141
142
go.Bar(name = ' LA Zoo' , x = animals, y = [12 , 18 , 29 ])
142
143
])
143
144
# Change the bar mode
144
- fig.update(layout_barmode = ' stack' )
145
+ fig.update_layout(barmode = ' stack' )
146
+ fig.show()
145
147
```
146
148
147
149
### Bar Chart with Hover Text
148
150
149
151
``` python
150
- import plotly.graph_objs as go
152
+ import plotly.graph_objects as go
151
153
152
154
x = [' Product A' , ' Product B' , ' Product C' ]
153
155
y = [20 , 14 , 23 ]
@@ -158,14 +160,14 @@ fig = go.Figure(data=[go.Bar(x=x, y=y,
158
160
# Customize aspect
159
161
fig.update_traces(marker_color = ' rgb(158,202,225)' , marker_line_color = ' rgb(8,48,107)' ,
160
162
marker_line_width = 1.5 , opacity = 0.6 )
161
- fig.update( layout_title_text = ' January 2013 Sales Report' )
163
+ fig.update_layout( title_text = ' January 2013 Sales Report' )
162
164
fig.show()
163
165
```
164
166
165
167
### Bar Chart with Direct Labels
166
168
167
169
``` python
168
- import plotly.graph_objs as go
170
+ import plotly.graph_objects as go
169
171
170
172
x = [' Product A' , ' Product B' , ' Product C' ]
171
173
y = [20 , 14 , 23 ]
@@ -183,7 +185,7 @@ fig.show()
183
185
### Rotated Bar Chart Labels
184
186
185
187
``` python
186
- import plotly.graph_objs as go
188
+ import plotly.graph_objects as go
187
189
188
190
months = [' Jan' , ' Feb' , ' Mar' , ' Apr' , ' May' , ' Jun' ,
189
191
' Jul' , ' Aug' , ' Sep' , ' Oct' , ' Nov' , ' Dec' ]
@@ -203,13 +205,14 @@ fig.add_trace(go.Bar(
203
205
))
204
206
205
207
# Here we modify the tickangle of the xaxis, resulting in rotated labels.
206
- fig.update(layout_barmode = ' group' , layout_xaxis_tickangle = - 45 )
208
+ fig.update_layout(barmode = ' group' , xaxis_tickangle = - 45 )
209
+ fig.show()
207
210
```
208
211
209
212
### Customizing Individual Bar Colors
210
213
211
214
``` python
212
- import plotly.graph_objs as go
215
+ import plotly.graph_objects as go
213
216
214
217
colors = [' lightslategray' ,] * 5
215
218
colors[1 ] = ' crimson'
@@ -220,13 +223,13 @@ fig = go.Figure(data=[go.Bar(
220
223
y = [20 , 14 , 23 , 25 , 22 ],
221
224
marker_color = colors # marker color can be a single color value or an iterable
222
225
)])
223
- fig.update( layout_title_text = ' Least Used Feature' )
226
+ fig.update_layout( title_text = ' Least Used Feature' )
224
227
```
225
228
226
229
### Customizing Individual Bar Widths
227
230
228
231
``` python
229
- import plotly.graph_objs as go
232
+ import plotly.graph_objects as go
230
233
231
234
fig = go.Figure(data = [go.Bar(
232
235
x = [1 , 2 , 3 , 5.5 , 10 ],
@@ -241,7 +244,7 @@ fig.show()
241
244
242
245
243
246
``` python
244
- import plotly.graph_objs as go
247
+ import plotly.graph_objects as go
245
248
246
249
years = [' 2016' ,' 2017' ,' 2018' ]
247
250
@@ -264,7 +267,7 @@ fig.show()
264
267
In this example several parameters of the layout as customized, hence it is convenient to use directly the ` go.Layout(...) ` constructor instead of calling ` fig.update ` .
265
268
266
269
``` python
267
- import plotly.graph_objs as go
270
+ import plotly.graph_objects as go
268
271
269
272
years = [1995 , 1996 , 1997 , 1998 , 1999 , 2000 , 2001 , 2002 , 2003 ,
270
273
2004 , 2005 , 2006 , 2007 , 2008 , 2009 , 2010 , 2011 , 2012 ]
@@ -283,7 +286,7 @@ fig.add_trace(go.Bar(x=years,
283
286
marker_color = ' rgb(26, 118, 255)'
284
287
))
285
288
286
- layout = go.Layout (
289
+ fig.update_layout (
287
290
title = ' US Export of Plastic Scrap' ,
288
291
xaxis_tickfont_size = 14 ,
289
292
yaxis = dict (
@@ -301,14 +304,13 @@ layout = go.Layout(
301
304
bargap = 0.15 , # gap between bars of adjacent location coordinates.
302
305
bargroupgap = 0.1 # gap between bars of the same location coordinate.
303
306
)
304
- fig.update(layout = layout)
305
307
fig.show()
306
308
```
307
309
308
310
### Waterfall Bar Chart
309
311
310
312
``` python
311
- import plotly.graph_objs as go
313
+ import plotly.graph_objects as go
312
314
313
315
x_data = [' Product<br>Revenue' , ' Services<br>Revenue' ,
314
316
' Total<br>Revenue' , ' Fixed<br>Costs' ,
@@ -340,24 +342,21 @@ fig.add_trace(go.Bar(x=x_data,
340
342
marker_line_color = ' rgba(50, 171, 96, 1.0)'
341
343
))
342
344
343
- layout = go.Layout(
344
- title = ' Annual Profit- 2015' ,
345
- barmode = ' stack' ,
346
- paper_bgcolor = ' rgba(245, 246, 249, 1)' ,
347
- plot_bgcolor = ' rgba(245, 246, 249, 1)' ,
348
- showlegend = False
349
- )
350
-
351
345
annotations = []
352
346
353
347
for i in range (0 , 7 ):
354
348
annotations.append(dict (x = x_data[i], y = y_data[i], text = text[i],
355
349
font = dict (family = ' Arial' , size = 14 ,
356
350
color = ' rgba(245, 246, 249, 1)' ),
357
351
showarrow = False ,))
358
- layout[' annotations' ] = annotations
359
352
360
- fig.update(layout = layout)
353
+ fig.update_layout(annotations = annotations,
354
+ title = ' Annual Profit- 2015' ,
355
+ barmode = ' stack' ,
356
+ paper_bgcolor = ' rgba(245, 246, 249, 1)' ,
357
+ plot_bgcolor = ' rgba(245, 246, 249, 1)' ,
358
+ showlegend = False )
359
+
361
360
fig.update_traces(marker_line_width = 2 )
362
361
fig.show()
363
362
```
@@ -368,7 +367,7 @@ With "relative" barmode, the bars are stacked on top of one another, with negati
368
367
below the axis, positive values above.
369
368
370
369
``` python
371
- import plotly.graph_objs as go
370
+ import plotly.graph_objects as go
372
371
x = [1 , 2 , 3 , 4 ]
373
372
374
373
fig = go.Figure()
@@ -377,7 +376,8 @@ fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
377
376
fig.add_trace(go.Bar(x = x, y = [- 15 , - 3 , 4.5 , - 8 ]))
378
377
fig.add_trace(go.Bar(x = x, y = [- 1 , 3 , - 3 , - 4 ]))
379
378
380
- fig.update(layout_barmode = ' relative' , layout_title_text = ' Relative Barmode' )
379
+ fig.update_layout(barmode = ' relative' , title_text = ' Relative Barmode' )
380
+ fig.show()
381
381
```
382
382
383
383
### Horizontal Bar Charts
0 commit comments