Skip to content

Commit 11f39aa

Browse files
committed
Update Bar chart section for Plotly 4 and colab.
1 parent 0c23a28 commit 11f39aa

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ venv.bak/
104104
.mypy_cache/
105105

106106
notebooks/my-first-scatter.html
107+
Untitled.ipynb
108+
gapminder_gdp_americas.csv

notebooks/03b-plotly-bar.ipynb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"if 'google.colab' in sys.modules:\n",
4141
" get_ipython().events.register('pre_run_cell', enable_plotly_in_cell)\n",
4242
" !mkdir -p data\n",
43-
" !wget -P data https://raw.githubusercontent.com/ualberta-rcg/python-plotting/master/notebooks/data/gapminder_gdp_europe.csv"
43+
" !wget -P data https://raw.githubusercontent.com/ualberta-rcg/python-plotting/master/notebooks/data/gapminder_gdp_europe.csv\n",
44+
" !mkdir -p solutions\n",
45+
" !wget -P solutions https://github.com/ualberta-rcg/python-plotting/blob/master/notebooks/solutions/plotly-bar-chart-north-america.py"
4446
]
4547
},
4648
{
@@ -161,6 +163,65 @@
161163
"fig = go.Figure(data=data, layout=layout)\n",
162164
"py.iplot(fig)"
163165
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"metadata": {},
170+
"source": [
171+
"## Exercise"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"Let's grab some North American GDP data (code to download below).\n",
179+
"\n",
180+
"Using what you learned here and in the scatter plot section, do the following:\n",
181+
"\n",
182+
"* Using this data set, make a bar chart of the per-capita GDP growth of Canada, the United States, and Mexico.\n",
183+
"* Label the chart \"Per-capita GDP Growth in North America\".\n",
184+
"* Make the Canada bar red, the US bar blue, and the Mexico bar green. **Hint!** Check the documentation on how to set the color of the bars: https://plot.ly/python/bar-charts/\n",
185+
"* Label the axes!"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {},
192+
"outputs": [],
193+
"source": [
194+
"# Grabbing the data:\n",
195+
"!mkdir -p data\n",
196+
"!wget -P data https://raw.githubusercontent.com/ualberta-rcg/python-intro/gh-pages/data/gapminder_gdp_americas.csv"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"df = pd.read_csv('data/gapminder_gdp_americas.csv', index_col='country')\n",
206+
"df = df.drop(columns=['continent'])\n",
207+
"years = df.columns.str.strip('gdpPercap_')\n",
208+
"df.columns = years.astype(int)\n",
209+
"\n",
210+
"# Your code goes here ..."
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"%load solutions/plotly-bar-chart-north-america.py\n",
220+
"\n",
221+
"# SOLUTION\n",
222+
"# Run this cell once to load the solution\n",
223+
"# Run this cell a second time to actually run the code"
224+
]
164225
}
165226
],
166227
"metadata": {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
df = pd.read_csv('data/gapminder_gdp_americas.csv', index_col='country')
2+
df = df.drop(columns=['continent'])
3+
years = df.columns.str.strip('gdpPercap_')
4+
df.columns = years.astype(int)
5+
6+
trace0 = go.Bar(
7+
x = df.columns,
8+
y = df.loc['Canada'],
9+
name = 'Canada',
10+
marker_color = 'rgb(200, 0, 0)'
11+
)
12+
trace1 = go.Bar(
13+
x = df.columns,
14+
y = df.loc['United States'],
15+
name = 'United States',
16+
marker_color = 'rgb(0, 0, 200)'
17+
)
18+
trace2 = go.Bar(
19+
x = df.columns,
20+
y = df.loc['Mexico'],
21+
name = 'Mexico',
22+
marker_color = 'rgb(0, 200, 0)'
23+
)
24+
25+
data = [trace0, trace1, trace2]
26+
layout = dict(
27+
title = 'Per-capita GDP Growth in North America',
28+
xaxis = dict(
29+
title = 'Year'
30+
),
31+
yaxis = dict(
32+
title = 'GDP per-capita'
33+
)
34+
)
35+
fig = dict(data=data, layout=layout)
36+
py.iplot(fig)

0 commit comments

Comments
 (0)