Skip to content

Commit 915b907

Browse files
committed
plotly.express, graph_objects, update_layout
1 parent 72c04e0 commit 915b907

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

notebooks/horizontal-bar-charts.md

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Plotly express functions take as argument a tidy [pandas DataFrame](https://pand
4848
#### Basic Horizontal Bar Chart with plotly express
4949

5050
```python
51-
import plotly_express as px
51+
import plotly.express as px
5252
tips = px.data.tips()
5353
fig = px.bar(tips, x="total_bill", y="day", orientation='h')
5454
fig.show()
@@ -59,7 +59,7 @@ fig.show()
5959
In this example a column is used to color the bars, and we add the information from other columns to the hover data.
6060

6161
```python
62-
import plotly_express as px
62+
import plotly.express as px
6363
tips = px.data.tips()
6464
fig = px.bar(tips, x="total_bill", y="sex", color='day', orientation='h',
6565
hover_data=["tip", "size"],
@@ -70,29 +70,29 @@ fig.show()
7070

7171
### Horizontal Bar Chart with go.Bar
7272

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
7474

7575

7676
#### Basic Horizontal Bar Chart
7777

7878
```python
79-
import plotly.graph_objs as go
79+
import plotly.graph_objects as go
8080

81-
trace = go.Bar(
81+
fig = go.Figure(go.Bar(
8282
x=[20, 14, 23],
8383
y=['giraffes', 'orangutans', 'monkeys'],
84-
orientation='h')
84+
orientation='h'))
8585

86-
fig = go.Figure(data=[trace])
8786
fig.show()
8887
```
8988

9089
### Colored Horizontal Bar Chart
9190

9291
```python
93-
import plotly.graph_objs as go
92+
import plotly.graph_objects as go
9493

95-
trace0 = go.Bar(
94+
fig = go.Figure()
95+
fig.add_trace(go.Bar(
9696
y=['giraffes', 'orangutans', 'monkeys'],
9797
x=[20, 14, 23],
9898
name='SF Zoo',
@@ -101,8 +101,8 @@ trace0 = go.Bar(
101101
color='rgba(246, 78, 139, 0.6)',
102102
line=dict(color='rgba(246, 78, 139, 1.0)', width=3)
103103
)
104-
)
105-
trace1 = go.Bar(
104+
))
105+
fig.add_trace(go.Bar(
106106
y=['giraffes', 'orangutans', 'monkeys'],
107107
x=[12, 18, 29],
108108
name='LA Zoo',
@@ -111,16 +111,16 @@ trace1 = go.Bar(
111111
color='rgba(58, 71, 80, 0.6)',
112112
line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
113113
)
114-
)
114+
))
115115

116-
fig = go.Figure(data=[trace0, trace1])
117-
fig.update(layout_barmode='stack')
116+
fig.update_layout(barmode='stack')
117+
fig.show()
118118
```
119119

120120
### Color Palette for Bar Chart
121121

122122
```python
123-
import plotly.graph_objs as go
123+
import plotly.graph_objects as go
124124

125125
top_labels = ['Strongly<br>agree', 'Agree', 'Neutral', 'Disagree',
126126
'Strongly<br>disagree']
@@ -140,12 +140,11 @@ y_data = ['The course was effectively<br>organized',
140140
'my<br>ability to think critically about<br>the subject',
141141
'I would recommend this<br>course to a friend']
142142

143-
144-
traces = []
143+
fig = go.Figure()
145144

146145
for i in range(0, len(x_data[0])):
147146
for xd, yd in zip(x_data, y_data):
148-
traces.append(go.Bar(
147+
fig.add_trace(go.Bar(
149148
x=[xd[i]], y=[yd],
150149
orientation='h',
151150
marker=dict(
@@ -154,7 +153,7 @@ for i in range(0, len(x_data[0])):
154153
)
155154
))
156155

157-
layout = go.Layout(
156+
fig.update_layout(
158157
xaxis=dict(
159158
showgrid=False,
160159
showline=False,
@@ -220,15 +219,15 @@ for yd, xd in zip(y_data, x_data):
220219
showarrow=False))
221220
space += xd[i]
222221

223-
layout.update(annotations=annotations)
224-
fig = go.Figure(data=traces, layout=layout)
222+
fig.update_layout(annotations=annotations)
223+
225224
fig.show()
226225
```
227226

228227
### Bar Chart with Line Plot
229228

230229
```python
231-
import plotly.graph_objs as go
230+
import plotly.graph_objects as go
232231
from plotly.subplots import make_subplots
233232

234233
import numpy as np
@@ -239,14 +238,17 @@ y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
239238
y_net_worth = [93453.919999999998, 81666.570000000007, 69889.619999999995,
240239
78381.529999999999, 141395.29999999999, 92969.020000000004,
241240
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(
248250
x=y_saving,
249-
y=x_saving,
251+
y=x,
250252
marker=dict(
251253
color='rgba(50, 171, 96, 0.6)',
252254
line=dict(
@@ -255,14 +257,16 @@ trace0 = go.Bar(
255257
),
256258
name='Household savings, percentage of household disposable income',
257259
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,
261264
mode='lines+markers',
262265
line_color='rgb(128, 0, 128)',
263266
name='Household net worth, Million USD/capita',
264-
)
265-
layout = dict(
267+
), 1, 2)
268+
269+
fig.update_layout(
266270
title='Household savings & net worth for eight OECD countries',
267271
yaxis=dict(
268272
showgrid=False,
@@ -306,7 +310,7 @@ y_s = np.round(y_saving, decimals=2)
306310
y_nw = np.rint(y_net_worth)
307311

308312
# 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):
310314
# labeling the scatter savings
311315
annotations.append(dict(xref='x2', yref='y2',
312316
y=xd, x=ydn - 20000,
@@ -331,16 +335,8 @@ annotations.append(dict(xref='paper', yref='paper',
331335
font=dict(family='Arial', size=10, color='rgb(150,150,150)'),
332336
showarrow=False))
333337

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)
342339

343-
fig.update(layout=layout)
344340
fig.show()
345341
```
346342

0 commit comments

Comments
 (0)