Skip to content

Commit 119d41f

Browse files
committed
Bar graph stuff for Plotly 4.
1 parent 95e7c81 commit 119d41f

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

notebooks/03b-plotly-bar.ipynb

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
23-
"import plotly.offline as py\n",
2423
"import numpy as np\n",
25-
"import plotly.graph_objs as go\n",
26-
"import pandas as pd\n",
27-
"\n",
28-
"py.init_notebook_mode(connected=False)"
24+
"import plotly.graph_objects as go\n",
25+
"import pandas as pd"
2926
]
3027
},
3128
{
3229
"cell_type": "markdown",
3330
"metadata": {},
3431
"source": [
35-
"People using Colab need this (initialization and download of supporting materials)"
32+
"We may need to download some files ..."
3633
]
3734
},
3835
{
@@ -41,22 +38,25 @@
4138
"metadata": {},
4239
"outputs": [],
4340
"source": [
44-
"# Stuff for Colab ...\n",
45-
"import sys\n",
46-
"def enable_plotly_in_cell():\n",
47-
" import IPython\n",
48-
" from plotly.offline import init_notebook_mode\n",
49-
" display(IPython.core.display.HTML('''\n",
50-
" <script src=\"/static/components/requirejs/require.js\"></script>\n",
51-
" '''))\n",
52-
" init_notebook_mode(connected=False)\n",
41+
"# Download data and solutions\n",
42+
"\n",
43+
"import urllib.request\n",
44+
"import os\n",
45+
"\n",
46+
"def download_data(path):\n",
47+
" if os.path.exists(path):\n",
48+
" return\n",
49+
" if not os.path.exists('data'):\n",
50+
" os.mkdir('data')\n",
51+
" if not os.path.exists('solutions'):\n",
52+
" os.mkdir('solutions')\n",
53+
" url = 'https://raw.githubusercontent.com/ualberta-rcg/python-plotting/master/notebooks/' + path\n",
54+
" output_file = path\n",
55+
" urllib.request.urlretrieve(url, output_file)\n",
56+
" print(\"Downloaded \" + path)\n",
5357
"\n",
54-
"if 'google.colab' in sys.modules:\n",
55-
" get_ipython().events.register('pre_run_cell', enable_plotly_in_cell)\n",
56-
" !mkdir -p data\n",
57-
" !wget -P data https://raw.githubusercontent.com/ualberta-rcg/python-plotting/master/notebooks/data/gapminder_gdp_europe.csv\n",
58-
" !mkdir -p solutions\n",
59-
" !wget -P solutions https://github.com/ualberta-rcg/python-plotting/blob/master/notebooks/solutions/plotly-bar-chart-north-america.py"
58+
"download_data('data/gapminder_gdp_europe.csv')\n",
59+
"download_data('solutions/plotly-bar-chart-north-america.py')"
6060
]
6161
},
6262
{
@@ -90,6 +90,8 @@
9090
"metadata": {},
9191
"outputs": [],
9292
"source": [
93+
"fig = go.Figure()\n",
94+
"\n",
9395
"trace0 = go.Scatter(\n",
9496
" x = df.columns,\n",
9597
" y = df.loc['Netherlands'],\n",
@@ -101,8 +103,9 @@
101103
" name = 'France'\n",
102104
")\n",
103105
"\n",
104-
"data = [trace0, trace1]\n",
105-
"py.iplot(data)"
106+
"fig.add_trace(trace0)\n",
107+
"fig.add_trace(trace1)\n",
108+
"fig.show()"
106109
]
107110
},
108111
{
@@ -118,6 +121,8 @@
118121
"metadata": {},
119122
"outputs": [],
120123
"source": [
124+
"fig = go.Figure()\n",
125+
"\n",
121126
"trace0 = go.Bar(\n",
122127
" x = df.columns,\n",
123128
" y = df.loc['Netherlands'],\n",
@@ -129,8 +134,9 @@
129134
" name = 'France'\n",
130135
")\n",
131136
"\n",
132-
"data = [trace0, trace1]\n",
133-
"py.iplot(data)"
137+
"fig.add_trace(trace0)\n",
138+
"fig.add_trace(trace1)\n",
139+
"fig.show()"
134140
]
135141
},
136142
{
@@ -150,8 +156,9 @@
150156
" barmode='group'\n",
151157
")\n",
152158
"\n",
153-
"fig = go.Figure(data=data, layout=layout)\n",
154-
"py.iplot(fig)"
159+
"fig = go.Figure(data=[trace0, trace1],\n",
160+
" layout=layout)\n",
161+
"fig.show()"
155162
]
156163
},
157164
{
@@ -174,8 +181,9 @@
174181
" barmode='stack'\n",
175182
")\n",
176183
"\n",
177-
"fig = go.Figure(data=data, layout=layout)\n",
178-
"py.iplot(fig)"
184+
"fig = go.Figure(data=[trace0, trace1],\n",
185+
" layout=layout)\n",
186+
"fig.show()"
179187
]
180188
},
181189
{
@@ -205,9 +213,14 @@
205213
"metadata": {},
206214
"outputs": [],
207215
"source": [
208-
"# Grabbing the data:\n",
209-
"!mkdir -p data\n",
210-
"!wget -P data https://raw.githubusercontent.com/ualberta-rcg/python-intro/gh-pages/data/gapminder_gdp_americas.csv"
216+
"import urllib.request\n",
217+
"import os\n",
218+
"\n",
219+
"output_file = 'data/gapminder_gdp_americas.csv'\n",
220+
"if not os.path.exists(output_file):\n",
221+
" url = 'https://raw.githubusercontent.com/ualberta-rcg/python-intro/gh-pages/data/gapminder_gdp_americas.csv'\n",
222+
" urllib.request.urlretrieve(url, output_file)\n",
223+
" print(\"Downloaded \" + output_file)"
211224
]
212225
},
213226
{
@@ -252,7 +265,7 @@
252265
"name": "python",
253266
"nbconvert_exporter": "python",
254267
"pygments_lexer": "ipython3",
255-
"version": "3.7.3"
268+
"version": "3.8.6"
256269
}
257270
},
258271
"nbformat": 4,

notebooks/solutions/plotly-bar-chart-north-america.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
)
2424

2525
data = [trace0, trace1, trace2]
26-
layout = dict(
26+
layout = go.Layout(
2727
title = 'Per-capita GDP Growth in North America',
28-
xaxis = dict(
29-
title = 'Year'
30-
),
31-
yaxis = dict(
32-
title = 'GDP per-capita'
33-
)
28+
xaxis_title = 'Year',
29+
yaxis_title = 'GDP per-capita'
3430
)
35-
fig = dict(data=data, layout=layout)
36-
py.iplot(fig)
31+
32+
fig = go.Figure(data=data, layout=layout)
33+
fig.show()

0 commit comments

Comments
 (0)