diff --git a/notebooks/Coronavirus_by_country.ipynb b/notebooks/Coronavirus_by_country.ipynb index e1b4418d1..0f5466ac4 100644 --- a/notebooks/Coronavirus_by_country.ipynb +++ b/notebooks/Coronavirus_by_country.ipynb @@ -28,17 +28,410 @@ }, { "cell_type": "code", - "execution_count": 200, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Collecting countryinfo\n", + " Using cached https://files.pythonhosted.org/packages/b1/f3/71c45e391eb8cc3a1641313fac6d013ef46b9ec23594943723e3d7ef247a/countryinfo-0.1.0.tar.gz\n", + "Building wheels for collected packages: countryinfo\n", + " Building wheel for countryinfo (setup.py) ... \u001b[?25ldone\n", + "\u001b[?25h Created wheel for countryinfo: filename=countryinfo-0.1.0-cp36-none-any.whl size=598099 sha256=af2b286f171332d0beb96c00d2dee505f92d503a0e9ea042ae8ec62ae3e776d5\n", + " Stored in directory: /home/nbuser/.cache/pip/wheels/78/59/c4/93bb8e79dcbf95375817c1ecf4c1573bd0c17e9376e2052f95\n", + "Successfully built countryinfo\n", + "Installing collected packages: countryinfo\n", + "Successfully installed countryinfo-0.1.0\n", + "\u001b[33mWARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.\n", + "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" + ] + } + ], "source": [ - "# import urllib\n", + "!pip install countryinfo" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "downloading\n", + "downloaded\n", + "getting pop for country: Afghanistan\n", + "pop is 26023100\n", + "getting pop for country: Albania\n", + "pop is 2895947\n", + "getting pop for country: Algeria\n", + "pop is 38700000\n", + "getting pop for country: Andorra\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'andorra'\n", + "\n", + "getting pop for country: Antigua and Barbuda\n", + "pop is 86295\n", + "getting pop for country: Argentina\n", + "pop is 42669500\n", + "getting pop for country: Armenia\n", + "pop is 3009800\n", + "getting pop for country: Australia\n", + "pop is 23696900\n", + "getting pop for country: Austria\n", + "pop is 8527230\n", + "getting pop for country: Azerbaijan\n", + "pop is 9552500\n", + "getting pop for country: Bahamas\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'bahamas'\n", + "\n", + "getting pop for country: Bahrain\n", + "pop is 1316500\n", + "getting pop for country: Bangladesh\n", + "pop is 157486000\n", + "getting pop for country: Belarus\n", + "pop is 9475100\n", + "getting pop for country: Belgium\n", + "pop is 11225469\n", + "getting pop for country: Bhutan\n", + "pop is 755030\n", + "getting pop for country: Bolivia\n", + "pop is 10027254\n", + "getting pop for country: Bosnia and Herzegovina\n", + "pop is 3791622\n", + "getting pop for country: Brazil\n", + "pop is 203586000\n", + "getting pop for country: Brunei Darussalam\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'brunei darussalam'\n", + "\n", + "getting pop for country: Bulgaria\n", + "pop is 7245677\n", + "getting pop for country: Burkina Faso\n", + "pop is 17322796\n", + "getting pop for country: Cambodia\n", + "pop is 15184116\n", + "getting pop for country: Cameroon\n", + "pop is 20386799\n", + "getting pop for country: Canada\n", + "pop is 35540419\n", + "getting pop for country: Cases on an international conveyance Japan\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'cases on an international conveyance japan'\n", + "\n", + "getting pop for country: Central African Republic\n", + "pop is 4709000\n", + "getting pop for country: Chile\n", + "pop is 17819054\n", + "getting pop for country: China\n", + "pop is 1367110000\n", + "getting pop for country: Colombia\n", + "pop is 47907800\n", + "Congo 1 2020-03-16 00:00:00 nan\n", + "getting pop for country: Congo\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'congo'\n", + "\n", + "getting pop for country: Costa Rica\n", + "pop is 4713168\n", + "getting pop for country: Cote dIvoire\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'cote divoire'\n", + "\n", + "getting pop for country: Croatia\n", + "pop is 4267558\n", + "getting pop for country: Cuba\n", + "pop is 11210064\n", + "getting pop for country: Cyprus\n", + "pop is 858000\n", + "getting pop for country: Czech Republic\n", + "pop is 10521600\n", + "getting pop for country: Czech republic\n", + "pop is 10521600\n", + "getting pop for country: Democratic Republic of the Congo\n", + "pop is 69360000\n", + "getting pop for country: Denmark\n", + "pop is 5655750\n", + "getting pop for country: Dominican Republic\n", + "pop is 10378267\n", + "getting pop for country: Ecuador\n", + "pop is 15888900\n", + "getting pop for country: Egypt\n", + "pop is 87668100\n", + "getting pop for country: Equatorial Guinea\n", + "pop is 1430000\n", + "getting pop for country: Estonia\n", + "pop is 1315819\n", + "getting pop for country: Ethiopia\n", + "pop is 87952991\n", + "getting pop for country: Finland\n", + "pop is 5470437\n", + "getting pop for country: France\n", + "pop is 66078000\n", + "getting pop for country: Gabon\n", + "pop is 1711000\n", + "getting pop for country: Georgia\n", + "pop is 4490500\n", + "getting pop for country: Germany\n", + "pop is 80783000\n", + "getting pop for country: Ghana\n", + "pop is 27043093\n", + "getting pop for country: Greece\n", + "pop is 10992589\n", + "getting pop for country: Guatemala\n", + "pop is 15806675\n", + "getting pop for country: Guinea\n", + "pop is 10628972\n", + "getting pop for country: Guyana\n", + "pop is 784894\n", + "getting pop for country: Holy See\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'holy see'\n", + "\n", + "getting pop for country: Honduras\n", + "pop is 8725111\n", + "getting pop for country: Hungary\n", + "pop is 9879000\n", + "getting pop for country: Iceland\n", + "pop is 328170\n", + "getting pop for country: India\n", + "pop is 1263930000\n", + "getting pop for country: Indonesia\n", + "pop is 252164800\n", + "getting pop for country: Iran\n", + "pop is 77966400\n", + "getting pop for country: Iraq\n", + "pop is 36004552\n", + "getting pop for country: Ireland\n", + "pop is 6378000\n", + "getting pop for country: Israel\n", + "pop is 8268400\n", + "getting pop for country: Italy\n", + "pop is 60769102\n", + "getting pop for country: Jamaica\n", + "pop is 2717991\n", + "getting pop for country: Japan\n", + "pop is 127080000\n", + "getting pop for country: Jordan\n", + "pop is 6666960\n", + "getting pop for country: Kazakhstan\n", + "pop is 17377800\n", + "getting pop for country: Kenya\n", + "pop is 41800000\n", + "getting pop for country: Kosovo\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'kosovo'\n", + "\n", + "getting pop for country: Kuwait\n", + "pop is 3268431\n", + "getting pop for country: Latvia\n", + "pop is 1991800\n", + "getting pop for country: Lebanon\n", + "pop is 4104000\n", + "getting pop for country: Liechtenstein\n", + "pop is 37132\n", + "getting pop for country: Lithuania\n", + "pop is 2927310\n", + "getting pop for country: Luxembourg\n", + "pop is 549700\n", + "getting pop for country: Malaysia\n", + "pop is 30430500\n", + "getting pop for country: Maldives\n", + "pop is 341256\n", + "getting pop for country: Malta\n", + "pop is 416055\n", + "getting pop for country: Mauritania\n", + "pop is 3545620\n", + "getting pop for country: Mexico\n", + "pop is 119713203\n", + "getting pop for country: Moldova\n", + "pop is 3557600\n", + "getting pop for country: Monaco\n", + "pop is 36950\n", + "getting pop for country: Mongolia\n", + "pop is 2987733\n", + "getting pop for country: Morocco\n", + "pop is 33465000\n", + "getting pop for country: Namibia\n", + "pop is 2113077\n", + "getting pop for country: Nepal\n", + "pop is 27646053\n", + "getting pop for country: Netherlands\n", + "pop is 16881000\n", + "getting pop for country: New Zealand\n", + "pop is 4547900\n", + "getting pop for country: Nigeria\n", + "pop is 178517000\n", + "getting pop for country: North Macedonia\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'north macedonia'\n", + "\n", + "getting pop for country: Norway\n", + "pop is 5156450\n", + "getting pop for country: Oman\n", + "pop is 4089076\n", + "getting pop for country: Pakistan\n", + "pop is 188410000\n", + "getting pop for country: Palestine\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'palestine'\n", + "\n", + "getting pop for country: Panama\n", + "pop is 3713312\n", + "getting pop for country: Paraguay\n", + "pop is 6893727\n", + "getting pop for country: Peru\n", + "pop is 30814175\n", + "getting pop for country: Philippines\n", + "pop is 100697400\n", + "getting pop for country: Poland\n", + "pop is 38496000\n", + "getting pop for country: Portugal\n", + "pop is 10477800\n", + "getting pop for country: Qatar\n", + "pop is 2269672\n", + "getting pop for country: Romania\n", + "pop is 19942642\n", + "getting pop for country: Russia\n", + "pop is 146233000\n", + "getting pop for country: Rwanda\n", + "pop is 10996891\n", + "getting pop for country: Saint Lucia\n", + "pop is 184000\n", + "getting pop for country: Saint Vincent and the Grenadines\n", + "pop is 109000\n", + "getting pop for country: San Marino\n", + "pop is 32743\n", + "getting pop for country: Saudi Arabia\n", + "pop is 30770375\n", + "getting pop for country: Senegal\n", + "pop is 13508715\n", + "getting pop for country: Serbia\n", + "Traceback (most recent call last):\n", + " File \"\", line 60, in prepare_data_from_ecdc\n", + " pop = countryinfo.population()\n", + " File \"/home/nbuser/anaconda3_501/lib/python3.6/site-packages/countryinfo/countryinfo.py\", line 231, in population\n", + " _population = self.__countries[self.__country_name]['population']\n", + "KeyError: 'serbia'\n", + "\n", + "getting pop for country: Seychelles\n", + "pop is 89949\n", + "getting pop for country: Singapore\n", + "pop is 5469700\n", + "getting pop for country: Slovakia\n", + "pop is 5415949\n", + "getting pop for country: Slovenia\n", + "pop is 2064966\n", + "getting pop for country: South Africa\n", + "pop is 54002000\n", + "getting pop for country: South Korea\n", + "pop is 50423955\n", + "getting pop for country: Spain\n", + "pop is 46507760\n", + "getting pop for country: Sri Lanka\n", + "pop is 20277597\n", + "getting pop for country: Sudan\n", + "pop is 37289406\n", + "getting pop for country: Suriname\n", + "pop is 534189\n", + "getting pop for country: Swaziland\n", + "pop is 1106189\n", + "getting pop for country: Sweden\n", + "pop is 9737521\n", + "getting pop for country: Switzerland\n", + "pop is 8183800\n", + "getting pop for country: switzerland\n", + "pop is 8183800\n", + "getting pop for country: Taiwan\n", + "pop is 23424615\n", + "getting pop for country: Thailand\n", + "pop is 64871000\n", + "getting pop for country: Togo\n", + "pop is 6993000\n", + "getting pop for country: Trinidad and Tobago\n", + "pop is 1328019\n", + "getting pop for country: Tunisia\n", + "pop is 10982754\n", + "getting pop for country: Turkey\n", + "pop is 76667864\n", + "getting pop for country: Ukraine\n", + "pop is 42973696\n", + "getting pop for country: United Arab Emirates\n", + "pop is 9446000\n", + "getting pop for country: United Kingdom\n", + "pop is 64105654\n", + "getting pop for country: United kingdom\n", + "pop is 64105654\n", + "getting pop for country: United States\n", + "pop is 319259000\n", + "getting pop for country: Uruguay\n", + "pop is 3404189\n", + "getting pop for country: Uzbekistan\n", + "pop is 30492800\n", + "getting pop for country: Venezuela\n", + "pop is 30206307\n", + "getting pop for country: Vietnam\n", + "pop is 89708900\n", + "done\n" + ] + } + ], + "source": [ + "import urllib\n", "import datetime\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "import pathlib\n", "import math\n", "import json\n", + "from countryinfo import CountryInfo\n", + "import re\n", + "import traceback\n", "\n", "%matplotlib notebook\n", "\n", @@ -53,14 +446,18 @@ " import matplotlib.pyplot as plt\n", " import pandas as pd \n", "\n", - " date = \"2020-03-15\"\n", + " print(\"downloading\")\n", + " date = \"2020-03-16\"\n", " url = \"https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-%s.xls\" % date \n", " data = urllib.request.urlopen(url).read() \n", " filename = \"data/ecdc/\" + date + \".xls\"\n", " f = open(filename, \"wb\")\n", " f.write(data)\n", " f.close()\n", + " print(\"downloaded\")\n", "\n", + " pops = {}\n", + " \n", " df = pd.read_excel(filename)\n", " for i in range(df.shape[0]):\n", " country = str(df[\"CountryExp\"][i])\n", @@ -70,12 +467,28 @@ " deaths = int(df[\"NewDeaths\"][i])\n", " except Exception as e:\n", " deaths = 0\n", - "# print(country, cases, date, df[\"NewDeaths\"][i])\n", + " print(country, cases, date, df[\"NewDeaths\"][i])\n", "\n", " if country not in country_data:\n", " country_data[country] = []\n", "\n", - " entry = {\"date\":date, \"cases\":cases, \"deaths\":deaths}\n", + " if country not in pops.keys():\n", + " pop = 1000000\n", + "\n", + " try:\n", + " countrywithspaces = re.sub(\"\\_\", \" \", country)\n", + " if countrywithspaces == 'United States of America':\n", + " countrywithspaces = 'United States'\n", + " print(\"getting pop for country: %s\" % (countrywithspaces))\n", + " countryinfo = CountryInfo(countrywithspaces)\n", + " pop = countryinfo.population()\n", + " print(\"pop is %d\" % (pop))\n", + " except Exception:\n", + " print(traceback.format_exc())\n", + " pops[country] = pop\n", + "\n", + " entry = {\"date\":date, \"cases\":((cases * 1000000) / pops[country]), \"deaths\":((deaths * 1000000) / pops[country])}\n", + " \n", " country_data[country].append(entry)\n", " \n", " for country, data in country_data.items():\n", @@ -88,7 +501,8 @@ " deaths += d[\"deaths\"]\n", " d[\"cases\"] = cases\n", " d[\"deaths\"] = deaths\n", - " \n", + " print(\"done\")\n", + " \n", "def get_data_from_ecdc(country):\n", " global country_data\n", " prepare_data_from_ecdc()\n", @@ -104,7 +518,7 @@ }, { "cell_type": "code", - "execution_count": 201, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -120,25 +534,28 @@ "\n", "china_offset= 37\n", "offset_by_country = dict(China=0,\n", - " France=-9.5 - china_offset, \n", - " Italy=0 - china_offset, \n", - " Germany=-9 - china_offset, \n", - " United_States_of_America=-8 - china_offset,\n", - " South_Korea=2 - china_offset,\n", - " Iran=2 - china_offset,\n", - " Japan=30 - china_offset,\n", - " Spain=-9 - china_offset)" + " France=-3.5 - china_offset, \n", + " Italy=3 - china_offset, \n", + " Germany=-2 - china_offset, \n", + " United_States_of_America=-5 - china_offset,\n", + " South_Korea=9 - china_offset,\n", + " Iran=5 - china_offset,\n", + " Japan=33 - china_offset,\n", + " Spain=-2 - china_offset,\n", + " United_Kingdom=-5 - china_offset)" ] }, { "cell_type": "code", - "execution_count": 202, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ + "import dateutil.parser\n", + "\n", "def plot_date_values(country, values, offset, multiplier, log_axis, label, key):\n", " start_time = datetime.datetime(2020, 1, 1)\n", - " dates = [(datetime.datetime.fromisoformat(v[\"date\"]) - start_time).days + offset for v in values]\n", + " dates = [(dateutil.parser.isoparse(v[\"date\"]) - start_time).days + offset for v in values]\n", " y = [v[key] * multiplier for v in values]\n", " if log_axis:\n", " y = [a for a in y]\n", @@ -154,7 +571,7 @@ " linestyle='dotted')\n", "\n", " _ = plt.legend()\n", - " plt.title(\"%s by country over time.\\n Logarithmic scale.\\n X axis is custom shifted for each country to take into account different start time.\" % key.capitalize(),\n", + " plt.title(\"%s per 1M of population by country over time.\\n Logarithmic scale.\\n X axis is custom shifted for each country to take into account different start time.\" % key.capitalize(),\n", " fontdict = {'fontweight' : \"bold\", 'verticalalignment': 'baseline'})\n", " plt.xlabel(\"Days since 01-01-2020 (for China, use offset for other countries)\")\n", " plt.ylabel(key)\n", @@ -188,782 +605,11 @@ }, { "cell_type": "code", - "execution_count": 203, + "execution_count": 58, "metadata": {}, "outputs": [ { "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support. ' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " fig.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
');\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], "text/plain": [ "" ] @@ -1804,7 +679,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1843,21 +718,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.6", "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.3" + "name": "python36" } }, "nbformat": 4,