hw1: done

This commit is contained in:
Claudio Maggioni 2023-03-29 17:17:07 +02:00
parent 971062f9a8
commit eb31323113
1 changed files with 258 additions and 276 deletions

View File

@ -1508,7 +1508,7 @@
},
{
"cell_type": "code",
"execution_count": 122,
"execution_count": 194,
"id": "5d1fad2a",
"metadata": {},
"outputs": [
@ -1541,8 +1541,8 @@
" <th>Bosnia and Herzegovina</th>\n",
" <th>Bulgaria</th>\n",
" <th>Croatia</th>\n",
" <th>Cyprus</th>\n",
" <th>Czech Republic</th>\n",
" <th>Denmark</th>\n",
" <th>...</th>\n",
" <th>San Marino</th>\n",
" <th>Serbia</th>\n",
@ -1583,174 +1583,166 @@
" <tbody>\n",
" <tr>\n",
" <th>Albania</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Andorra</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Austria</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>15</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>6</td>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>15.0</td>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" <td>3.0</td>\n",
" <td>6.0</td>\n",
" <td>1.0</td>\n",
" <td>5.0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>40</td>\n",
" <td>4</td>\n",
" <td>11</td>\n",
" <td>10</td>\n",
" <td>11</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>3.0</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" <td>40.0</td>\n",
" <td>4.0</td>\n",
" <td>11.0</td>\n",
" <td>10.0</td>\n",
" <td>11.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Belarus</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Belgium</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>4</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>4.0</td>\n",
" <td>5.0</td>\n",
" <td>4.0</td>\n",
" <td>5.0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>60</td>\n",
" <td>6</td>\n",
" <td>6</td>\n",
" <td>2</td>\n",
" <td>17</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" <td>3.0</td>\n",
" <td>60.0</td>\n",
" <td>6.0</td>\n",
" <td>6.0</td>\n",
" <td>2.0</td>\n",
" <td>17.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 47 columns</p>\n",
"<p>5 rows × 46 columns</p>\n",
"</div>"
],
"text/plain": [
"country Albania Andorra Austria Belarus Belgium \\\n",
"country_dest \n",
"Albania 0 0 1 0 0 \n",
"Andorra 0 0 0 0 0 \n",
"Austria 1 0 15 2 2 \n",
"Belarus 0 0 2 0 0 \n",
"Belgium 0 0 2 0 1 \n",
"Albania 0.0 0.0 1.0 0.0 0.0 \n",
"Andorra 0.0 0.0 0.0 0.0 0.0 \n",
"Austria 1.0 0.0 15.0 2.0 2.0 \n",
"Belarus 0.0 0.0 2.0 0.0 0.0 \n",
"Belgium 0.0 0.0 2.0 0.0 1.0 \n",
"\n",
"country Bosnia and Herzegovina Bulgaria Croatia Cyprus \\\n",
"country_dest \n",
"Albania 0 0 0 0 \n",
"Andorra 0 0 0 0 \n",
"Austria 1 3 6 3 \n",
"Belarus 0 0 0 1 \n",
"Belgium 0 4 5 2 \n",
"country Bosnia and Herzegovina Bulgaria Croatia Czech Republic \\\n",
"country_dest \n",
"Albania 0.0 0.0 0.0 0.0 \n",
"Andorra 0.0 0.0 0.0 0.0 \n",
"Austria 1.0 3.0 6.0 1.0 \n",
"Belarus 0.0 0.0 0.0 2.0 \n",
"Belgium 0.0 4.0 5.0 4.0 \n",
"\n",
"country Czech Republic ... San Marino Serbia Slovakia Slovenia \\\n",
"country_dest ... \n",
"Albania 0 ... 0 0 0 1 \n",
"Andorra 0 ... 0 0 0 0 \n",
"Austria 1 ... 0 3 1 2 \n",
"Belarus 2 ... 0 0 0 0 \n",
"Belgium 4 ... 0 2 1 3 \n",
"country Denmark ... San Marino Serbia Slovakia Slovenia Spain \\\n",
"country_dest ... \n",
"Albania 0.0 ... 0.0 0.0 0.0 1.0 0.0 \n",
"Andorra 0.0 ... 0.0 0.0 0.0 0.0 0.0 \n",
"Austria 5.0 ... 0.0 3.0 1.0 2.0 40.0 \n",
"Belarus 0.0 ... 0.0 0.0 0.0 0.0 1.0 \n",
"Belgium 5.0 ... 0.0 2.0 1.0 3.0 60.0 \n",
"\n",
"country Spain Sweden Switzerland Ukraine United Kingdom \\\n",
"country_dest \n",
"Albania 0 0 0 0 1 \n",
"Andorra 0 0 0 0 0 \n",
"Austria 40 4 11 10 11 \n",
"Belarus 1 1 1 2 1 \n",
"Belgium 60 6 6 2 17 \n",
"country Sweden Switzerland Ukraine United Kingdom Vatican City \n",
"country_dest \n",
"Albania 0.0 0.0 0.0 1.0 0.0 \n",
"Andorra 0.0 0.0 0.0 0.0 0.0 \n",
"Austria 4.0 11.0 10.0 11.0 0.0 \n",
"Belarus 1.0 1.0 2.0 1.0 0.0 \n",
"Belgium 6.0 6.0 2.0 17.0 0.0 \n",
"\n",
"country Vatican City \n",
"country_dest \n",
"Albania 0 \n",
"Andorra 0 \n",
"Austria 0 \n",
"Belarus 0 \n",
"Belgium 0 \n",
"\n",
"[5 rows x 47 columns]"
"[5 rows x 46 columns]"
]
},
"execution_count": 122,
"execution_count": 194,
"metadata": {},
"output_type": "execute_result"
}
@ -1762,7 +1754,9 @@
"df_routes = pd.read_csv(\"./datasets/routes.csv\", na_values=['\\\\N'], sep=\";\") \\\n",
" .rename(lambda x: x.strip(), axis=1)\n",
"\n",
"df_countries = pd.read_csv(\"./datasets/countries.csv\") \\\n",
"# Note that I consider a country to be 'European' if the 'continent' country in countries.csv is equal to 'eu' \n",
"df_countries = pd.read_csv(\"./datasets/countries.csv\") \n",
"df_countries = df_countries.loc[df_countries.continent == 'eu', :] \\\n",
" .rename(columns={'name': 'country'}).drop(columns=['continent'])\n",
"\n",
"df_countries.loc[df_countries.country == 'Faroe Is.', 'country'] = 'Faroe Islands'\n",
@ -1779,7 +1773,7 @@
" .join(df_id_country, how='right', on='source_airport') \\\n",
" .join(df_id_country, how='right', on='destination_airport', rsuffix='_dest')\n",
"\n",
"# Count only a pair of notna source and destination airport as a valid route\n",
"# Count only a pair of notna source and destination airport as a valid flight\n",
"# When this is not a case the row is an artifact of the right join. We assign 0\n",
"# as a value so that in the final sum the value will still appear to include \n",
"# no-flight countries, albeit with a total number of routes to 0\n",
@ -1795,16 +1789,16 @@
" .fillna(0) \\\n",
" .sort_values('country_dest')\n",
"\n",
"# Change type of cells and remove column level for geopandas compatibility\n",
"df_routes_count = df_routes_count[df_routes_count.columns].astype(int)\n",
"# Change type of cells to float and remove column level for geopandas compatibility\n",
"df_routes_count = df_routes_count[df_routes_count.columns].astype(float)\n",
"df_routes_count.columns = df_routes_count.columns.droplevel(0)\n",
"df_routes_count.head()"
]
},
{
"cell_type": "code",
"execution_count": 128,
"id": "75225ed4",
"execution_count": 195,
"id": "87bd101d",
"metadata": {},
"outputs": [
{
@ -1854,30 +1848,6 @@
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Azerbaijan</td>\n",
" <td>MULTIPOLYGON (((45.08332 39.76804, 45.26639 39...</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Albania</td>\n",
" <td>POLYGON ((19.43621 41.02107, 19.45055 41.06000...</td>\n",
" <td>0.0</td>\n",
@ -1901,31 +1871,7 @@
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Armenia</td>\n",
" <td>MULTIPOLYGON (((45.57305 40.63249, 45.52888 40...</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>...</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <th>1</th>\n",
" <td>Bosnia and Herzegovina</td>\n",
" <td>POLYGON ((17.64984 42.88908, 17.57853 42.94382...</td>\n",
" <td>0.0</td>\n",
@ -1949,7 +1895,7 @@
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <th>2</th>\n",
" <td>Bulgaria</td>\n",
" <td>POLYGON ((27.87917 42.84110, 27.89500 42.80250...</td>\n",
" <td>0.0</td>\n",
@ -1972,63 +1918,111 @@
" <td>10.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Denmark</td>\n",
" <td>MULTIPOLYGON (((11.51389 54.82972, 11.56444 54...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>5.0</td>\n",
" <td>0.0</td>\n",
" <td>5.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>6.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>0.0</td>\n",
" <td>2.0</td>\n",
" <td>31.0</td>\n",
" <td>9.0</td>\n",
" <td>7.0</td>\n",
" <td>0.0</td>\n",
" <td>22.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Ireland</td>\n",
" <td>MULTIPOLYGON (((-9.65639 53.22222, -9.66333 53...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>3.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>3.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>53.0</td>\n",
" <td>2.0</td>\n",
" <td>3.0</td>\n",
" <td>0.0</td>\n",
" <td>63.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 49 columns</p>\n",
"<p>5 rows × 48 columns</p>\n",
"</div>"
],
"text/plain": [
" NAME geometry \\\n",
"0 Azerbaijan MULTIPOLYGON (((45.08332 39.76804, 45.26639 39... \n",
"1 Albania POLYGON ((19.43621 41.02107, 19.45055 41.06000... \n",
"2 Armenia MULTIPOLYGON (((45.57305 40.63249, 45.52888 40... \n",
"3 Bosnia and Herzegovina POLYGON ((17.64984 42.88908, 17.57853 42.94382... \n",
"4 Bulgaria POLYGON ((27.87917 42.84110, 27.89500 42.80250... \n",
"0 Albania POLYGON ((19.43621 41.02107, 19.45055 41.06000... \n",
"1 Bosnia and Herzegovina POLYGON ((17.64984 42.88908, 17.57853 42.94382... \n",
"2 Bulgaria POLYGON ((27.87917 42.84110, 27.89500 42.80250... \n",
"3 Denmark MULTIPOLYGON (((11.51389 54.82972, 11.56444 54... \n",
"4 Ireland MULTIPOLYGON (((-9.65639 53.22222, -9.66333 53... \n",
"\n",
" Albania Andorra Austria Belarus Belgium Bosnia and Herzegovina \\\n",
"0 NaN NaN NaN NaN NaN NaN \n",
"1 0.0 0.0 1.0 0.0 0.0 0.0 \n",
"2 NaN NaN NaN NaN NaN NaN \n",
"3 0.0 0.0 1.0 0.0 0.0 2.0 \n",
"4 0.0 0.0 3.0 0.0 4.0 0.0 \n",
"0 0.0 0.0 1.0 0.0 0.0 0.0 \n",
"1 0.0 0.0 1.0 0.0 0.0 2.0 \n",
"2 0.0 0.0 3.0 0.0 4.0 0.0 \n",
"3 0.0 0.0 5.0 0.0 5.0 1.0 \n",
"4 0.0 0.0 1.0 0.0 3.0 0.0 \n",
"\n",
" Bulgaria Croatia ... San Marino Serbia Slovakia Slovenia Spain \\\n",
"0 NaN NaN ... NaN NaN NaN NaN NaN \n",
"1 0.0 0.0 ... 0.0 0.0 0.0 1.0 0.0 \n",
"2 NaN NaN ... NaN NaN NaN NaN NaN \n",
"3 0.0 1.0 ... 0.0 3.0 0.0 1.0 0.0 \n",
"4 6.0 0.0 ... 0.0 2.0 0.0 0.0 8.0 \n",
"0 0.0 0.0 ... 0.0 0.0 0.0 1.0 0.0 \n",
"1 0.0 1.0 ... 0.0 3.0 0.0 1.0 0.0 \n",
"2 6.0 0.0 ... 0.0 2.0 0.0 0.0 8.0 \n",
"3 1.0 6.0 ... 0.0 2.0 0.0 2.0 31.0 \n",
"4 1.0 3.0 ... 0.0 0.0 1.0 0.0 53.0 \n",
"\n",
" Sweden Switzerland Ukraine United Kingdom Vatican City \n",
"0 NaN NaN NaN NaN NaN \n",
"1 0.0 0.0 0.0 1.0 0.0 \n",
"2 NaN NaN NaN NaN NaN \n",
"3 3.0 1.0 0.0 0.0 0.0 \n",
"4 0.0 1.0 0.0 10.0 0.0 \n",
"0 0.0 0.0 0.0 1.0 0.0 \n",
"1 3.0 1.0 0.0 0.0 0.0 \n",
"2 0.0 1.0 0.0 10.0 0.0 \n",
"3 9.0 7.0 0.0 22.0 0.0 \n",
"4 2.0 3.0 0.0 63.0 0.0 \n",
"\n",
"[5 rows x 49 columns]"
"[5 rows x 48 columns]"
]
},
"execution_count": 128,
"execution_count": 195,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Some countries have column values = 'NaN'. These countries are not in Europe according to the dataset,\n",
"# but I choose to include them in the map as 'no data' (i.e. in grey)\n",
"# Note the inner join to drop countries that we do not consider part of 'Europe'\n",
"# (according to the countries.csv file)\n",
"yurop = gpd.read_file(\"./datasets/europe.geojson\") \\\n",
" .loc[:, ['NAME', 'geometry']] \\\n",
" .set_index('NAME') \\\n",
" .join(df_routes_count, how='left') \\\n",
" .reset_index()\n",
" .join(df_routes_count, how='inner') \\\n",
" .reset_index(names='NAME')\n",
"\n",
"yurop.head()"
]
},
{
"cell_type": "code",
"execution_count": 166,
"execution_count": 218,
"id": "11612845",
"metadata": {},
"outputs": [
@ -2036,11 +2030,11 @@
"data": {
"application/vnd.bokehjs_exec.v0+json": "",
"text/html": [
"<script id=\"p7925\">\n",
"<script id=\"p13596\">\n",
" (function() {\n",
" const xhr = new XMLHttpRequest()\n",
" xhr.responseType = 'blob';\n",
" xhr.open('GET', \"http://localhost:50851/autoload.js?bokeh-autoload-element=p7925&bokeh-absolute-url=http://localhost:50851&resources=none\", true);\n",
" xhr.open('GET', \"http://localhost:51173/autoload.js?bokeh-autoload-element=p13596&bokeh-absolute-url=http://localhost:51173&resources=none\", true);\n",
" xhr.onload = function (event) {\n",
" const script = document.createElement('script');\n",
" const src = URL.createObjectURL(event.target.response);\n",
@ -2054,76 +2048,50 @@
},
"metadata": {
"application/vnd.bokehjs_exec.v0+json": {
"server_id": "de3272d393da49aba1d3fd3516574d15"
"server_id": "3422a6f3dd7d4e33b8dd93a4c485662e"
}
},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"Index(['NAME', 'geometry', 'routes'], dtype='object')\n"
]
}
],
"source": [
"from bokeh.events import Tap\n",
"from bokeh.models.widgets import MultiSelect, Slider, DateRangeSlider\n",
"from bokeh.layouts import row\n",
"from bokeh.application import Application\n",
"from bokeh.application.handlers import FunctionHandler\n",
"from bokeh.models.ranges import Range1d\n",
"from bokeh.palettes import Reds\n",
"from bokeh.models import LinearColorMapper, LogColorMapper, ColorBar\n",
"from bokeh.models import LinearColorMapper, ColorBar\n",
"from shapely import Point\n",
"\n",
"yurop_json = yurop.to_json()\n",
"from bokeh.models import Title\n",
"\n",
"def figure_flights(doc):\n",
" palette = Reds[6]\n",
" palette = Reds[8]\n",
" palette = palette[::-1]\n",
"\n",
" color_mapper = LinearColorMapper(palette = palette, low = 0, high = 600)\n",
"\n",
" \n",
" color_bar = ColorBar(color_mapper = color_mapper, \n",
" width = 20, height = 600,\n",
" label_standoff = 8,\n",
" location = (0,0))\n",
"\n",
" \n",
" p = figure(title = 'Number of flights to each country', \n",
" p = figure(title = '', \n",
" frame_height = 600,\n",
" frame_width = 800, \n",
" toolbar_location = 'below',\n",
" tools = \"pan, wheel_zoom, box_zoom, reset\")\n",
"\n",
" geo_ds = GeoJSONDataSource(geojson=yurop_json)\n",
" geo_ds = GeoJSONDataSource()\n",
" \n",
" plotted_districts = p.patches('xs','ys', source = geo_ds,\n",
" line_color = 'black', \n",
" line_width = 0.25)\n",
" \n",
" p.add_layout(Title(text=\"WARNING: color scale dynamically changes according to selected country\", \\\n",
" text_font_style=\"italic\"), 'above')\n",
"\n",
" p.patches(\"xs\",\"ys\", source = geo_ds,\n",
" fill_color = {\"field\" : \"routes\",\n",
" fill_color = {\"field\" : \"flights\",\n",
" \"transform\" : color_mapper},\n",
" line_color = \"gray\", \n",
" line_width = 0.25, \n",
@ -2133,27 +2101,41 @@
" p.ygrid.grid_line_color = None\n",
" p.axis.visible = False\n",
" \n",
" p.add_tools(HoverTool(renderers = [plotted_districts],\n",
" tooltips = [(\"Country\",\"@NAME\"),(\"# routes\",\"@routes\")]))\n",
" ht = HoverTool(renderers = [plotted_districts])\n",
" p.add_tools(ht)\n",
" \n",
" def set_gdf_as_datasource(gdf):\n",
" if gdf is None or len(gdf) == 0:\n",
" geo_ds.geojson = yurop.to_json()\n",
" ht.tooltips = [(\"Country\", \"@NAME\")]\n",
" color_mapper.high = 600\n",
" p.title.text = '# flights to each country: click to select country of departure'\n",
" else:\n",
" routes_from_country = gdf.iloc[0, 2:].to_frame(name='flights')\n",
" gdf_country_flights = yurop.set_index('NAME').loc[:, ['geometry']] \\\n",
" .join(routes_from_country) \\\n",
" .reset_index(names='NAME')\n",
"\n",
" country = gdf.iloc[0, :]['NAME']\n",
" max_flights = gdf_country_flights['flights'].max()\n",
" \n",
" geo_ds.geojson = gdf_country_flights.to_json()\n",
" ht.tooltips = [(\"Country\", \"@NAME\"), (f\"# flights from {country}\", \"@flights\")]\n",
" p.title.text = f\"# flights to each country from {country}: click to select another country of departure\"\n",
" \n",
" # The max value of the colorscale is dynamically computed to the ceiling of the first two significant \n",
" # digits of the max value\n",
" color_mapper.high = np.round(np.power(10, np.ceil(np.log10(max_flights) * 10) / 10) / 10) * 10\n",
" \n",
" set_gdf_as_datasource(None)\n",
"\n",
" tool = TapTool()\n",
" \n",
" def event(x):\n",
" # Figure out the country that intersects the coordinates we clicked\n",
" intersects = yurop[yurop.intersects(Point(x.x, x.y))]\n",
" if len(intersects) == 0:\n",
" return\n",
" set_gdf_as_datasource(intersects)\n",
" \n",
" routes_from_country = intersects.iloc[0, 2:].to_frame(name='routes')\n",
" gdf_country_flights = yurop.set_index('NAME').loc[:, ['geometry']] \\\n",
" .join(routes_from_country) \\\n",
" .reset_index()\n",
" print(gdf_country_flights.columns)\n",
" \n",
" geo_ds_country = gdf_country_flights.to_json()\n",
" \n",
" geo_ds.geojson = geo_ds_country\n",
"\n",
" tap = p.add_tools()\n",
" p.on_event(Tap, event)\n",
" \n",