|
20 | 20 | "metadata": {}, |
21 | 21 | "outputs": [], |
22 | 22 | "source": [ |
23 | | - "import plotly.offline as py\n", |
24 | 23 | "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" |
29 | 26 | ] |
30 | 27 | }, |
31 | 28 | { |
32 | 29 | "cell_type": "markdown", |
33 | 30 | "metadata": {}, |
34 | 31 | "source": [ |
35 | | - "People using Colab need this (initialization and download of supporting materials)" |
| 32 | + "We may need to download some files ..." |
36 | 33 | ] |
37 | 34 | }, |
38 | 35 | { |
|
41 | 38 | "metadata": {}, |
42 | 39 | "outputs": [], |
43 | 40 | "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", |
53 | 57 | "\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')" |
60 | 60 | ] |
61 | 61 | }, |
62 | 62 | { |
|
90 | 90 | "metadata": {}, |
91 | 91 | "outputs": [], |
92 | 92 | "source": [ |
| 93 | + "fig = go.Figure()\n", |
| 94 | + "\n", |
93 | 95 | "trace0 = go.Scatter(\n", |
94 | 96 | " x = df.columns,\n", |
95 | 97 | " y = df.loc['Netherlands'],\n", |
|
101 | 103 | " name = 'France'\n", |
102 | 104 | ")\n", |
103 | 105 | "\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()" |
106 | 109 | ] |
107 | 110 | }, |
108 | 111 | { |
|
118 | 121 | "metadata": {}, |
119 | 122 | "outputs": [], |
120 | 123 | "source": [ |
| 124 | + "fig = go.Figure()\n", |
| 125 | + "\n", |
121 | 126 | "trace0 = go.Bar(\n", |
122 | 127 | " x = df.columns,\n", |
123 | 128 | " y = df.loc['Netherlands'],\n", |
|
129 | 134 | " name = 'France'\n", |
130 | 135 | ")\n", |
131 | 136 | "\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()" |
134 | 140 | ] |
135 | 141 | }, |
136 | 142 | { |
|
150 | 156 | " barmode='group'\n", |
151 | 157 | ")\n", |
152 | 158 | "\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()" |
155 | 162 | ] |
156 | 163 | }, |
157 | 164 | { |
|
174 | 181 | " barmode='stack'\n", |
175 | 182 | ")\n", |
176 | 183 | "\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()" |
179 | 187 | ] |
180 | 188 | }, |
181 | 189 | { |
|
205 | 213 | "metadata": {}, |
206 | 214 | "outputs": [], |
207 | 215 | "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)" |
211 | 224 | ] |
212 | 225 | }, |
213 | 226 | { |
|
252 | 265 | "name": "python", |
253 | 266 | "nbconvert_exporter": "python", |
254 | 267 | "pygments_lexer": "ipython3", |
255 | | - "version": "3.7.3" |
| 268 | + "version": "3.8.6" |
256 | 269 | } |
257 | 270 | }, |
258 | 271 | "nbformat": 4, |
|
0 commit comments