Skip to content

Commit 6f7adda

Browse files
committed
update_layout, graph_objects
1 parent 684c76c commit 6f7adda

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

notebooks/bar-charts.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ fig.show()
102102
To learn more, see the *link to px.bar reference page*.
103103

104104

105-
#### Basic Bar Chart with plotly.graph_objs
105+
#### Basic Bar Chart with plotly.graph_objects
106106

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`.
108108

109109
```python
110-
import plotly.graph_objs as go
110+
import plotly.graph_objects as go
111111
animals=['giraffes', 'orangutans', 'monkeys']
112112

113113
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
@@ -119,35 +119,37 @@ fig.show()
119119
Customize the figure using `fig.update`.
120120

121121
```python
122-
import plotly.graph_objs as go
122+
import plotly.graph_objects as go
123123
animals=['giraffes', 'orangutans', 'monkeys']
124124

125125
fig = go.Figure(data=[
126126
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
127127
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
128128
])
129129
# Change the bar mode
130-
fig.update(layout_barmode='group')
130+
fig.update_layout(barmode='group')
131+
fig.show()
131132
```
132133

133134
### Stacked Bar Chart
134135

135136
```python
136-
import plotly.graph_objs as go
137+
import plotly.graph_objects as go
137138
animals=['giraffes', 'orangutans', 'monkeys']
138139

139140
fig = go.Figure(data=[
140141
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
141142
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
142143
])
143144
# Change the bar mode
144-
fig.update(layout_barmode='stack')
145+
fig.update_layout(barmode='stack')
146+
fig.show()
145147
```
146148

147149
### Bar Chart with Hover Text
148150

149151
```python
150-
import plotly.graph_objs as go
152+
import plotly.graph_objects as go
151153

152154
x = ['Product A', 'Product B', 'Product C']
153155
y = [20, 14, 23]
@@ -158,14 +160,14 @@ fig = go.Figure(data=[go.Bar(x=x, y=y,
158160
# Customize aspect
159161
fig.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',
160162
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')
162164
fig.show()
163165
```
164166

165167
### Bar Chart with Direct Labels
166168

167169
```python
168-
import plotly.graph_objs as go
170+
import plotly.graph_objects as go
169171

170172
x = ['Product A', 'Product B', 'Product C']
171173
y = [20, 14, 23]
@@ -183,7 +185,7 @@ fig.show()
183185
### Rotated Bar Chart Labels
184186

185187
```python
186-
import plotly.graph_objs as go
188+
import plotly.graph_objects as go
187189

188190
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
189191
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
@@ -203,13 +205,14 @@ fig.add_trace(go.Bar(
203205
))
204206

205207
# 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()
207210
```
208211

209212
### Customizing Individual Bar Colors
210213

211214
```python
212-
import plotly.graph_objs as go
215+
import plotly.graph_objects as go
213216

214217
colors = ['lightslategray',] * 5
215218
colors[1] = 'crimson'
@@ -220,13 +223,13 @@ fig = go.Figure(data=[go.Bar(
220223
y=[20, 14, 23, 25, 22],
221224
marker_color=colors # marker color can be a single color value or an iterable
222225
)])
223-
fig.update(layout_title_text='Least Used Feature')
226+
fig.update_layout(title_text='Least Used Feature')
224227
```
225228

226229
### Customizing Individual Bar Widths
227230

228231
```python
229-
import plotly.graph_objs as go
232+
import plotly.graph_objects as go
230233

231234
fig = go.Figure(data=[go.Bar(
232235
x=[1, 2, 3, 5.5, 10],
@@ -241,7 +244,7 @@ fig.show()
241244

242245

243246
```python
244-
import plotly.graph_objs as go
247+
import plotly.graph_objects as go
245248

246249
years = ['2016','2017','2018']
247250

@@ -264,7 +267,7 @@ fig.show()
264267
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`.
265268

266269
```python
267-
import plotly.graph_objs as go
270+
import plotly.graph_objects as go
268271

269272
years = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
270273
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
@@ -283,7 +286,7 @@ fig.add_trace(go.Bar(x=years,
283286
marker_color='rgb(26, 118, 255)'
284287
))
285288

286-
layout = go.Layout(
289+
fig.update_layout(
287290
title='US Export of Plastic Scrap',
288291
xaxis_tickfont_size=14,
289292
yaxis=dict(
@@ -301,14 +304,13 @@ layout = go.Layout(
301304
bargap=0.15, # gap between bars of adjacent location coordinates.
302305
bargroupgap=0.1 # gap between bars of the same location coordinate.
303306
)
304-
fig.update(layout=layout)
305307
fig.show()
306308
```
307309

308310
### Waterfall Bar Chart
309311

310312
```python
311-
import plotly.graph_objs as go
313+
import plotly.graph_objects as go
312314

313315
x_data = ['Product<br>Revenue', 'Services<br>Revenue',
314316
'Total<br>Revenue', 'Fixed<br>Costs',
@@ -340,24 +342,21 @@ fig.add_trace(go.Bar(x=x_data,
340342
marker_line_color='rgba(50, 171, 96, 1.0)'
341343
))
342344

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-
351345
annotations = []
352346

353347
for i in range(0, 7):
354348
annotations.append(dict(x=x_data[i], y=y_data[i], text=text[i],
355349
font=dict(family='Arial', size=14,
356350
color='rgba(245, 246, 249, 1)'),
357351
showarrow=False,))
358-
layout['annotations'] = annotations
359352

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+
361360
fig.update_traces(marker_line_width=2)
362361
fig.show()
363362
```
@@ -368,7 +367,7 @@ With "relative" barmode, the bars are stacked on top of one another, with negati
368367
below the axis, positive values above.
369368

370369
```python
371-
import plotly.graph_objs as go
370+
import plotly.graph_objects as go
372371
x = [1, 2, 3, 4]
373372

374373
fig = go.Figure()
@@ -377,7 +376,8 @@ fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
377376
fig.add_trace(go.Bar(x=x, y=[-15, -3, 4.5, -8]))
378377
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))
379378

380-
fig.update(layout_barmode='relative', layout_title_text='Relative Barmode')
379+
fig.update_layout(barmode='relative', title_text='Relative Barmode')
380+
fig.show()
381381
```
382382

383383
### Horizontal Bar Charts

0 commit comments

Comments
 (0)