hw1: ex5 works (wip)

This commit is contained in:
Claudio Maggioni 2023-03-29 16:08:27 +02:00
parent 6e9bc0f9de
commit 971062f9a8

View file

@ -1803,8 +1803,8 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 127, "execution_count": 128,
"id": "f3c0739b", "id": "75225ed4",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -2009,7 +2009,7 @@
"[5 rows x 49 columns]" "[5 rows x 49 columns]"
] ]
}, },
"execution_count": 127, "execution_count": 128,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -2028,7 +2028,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 126, "execution_count": 166,
"id": "11612845", "id": "11612845",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -2036,11 +2036,11 @@
"data": { "data": {
"application/vnd.bokehjs_exec.v0+json": "", "application/vnd.bokehjs_exec.v0+json": "",
"text/html": [ "text/html": [
"<script id=\"p2994\">\n", "<script id=\"p7925\">\n",
" (function() {\n", " (function() {\n",
" const xhr = new XMLHttpRequest()\n", " const xhr = new XMLHttpRequest()\n",
" xhr.responseType = 'blob';\n", " xhr.responseType = 'blob';\n",
" xhr.open('GET', \"http://localhost:50568/autoload.js?bokeh-autoload-element=p2994&bokeh-absolute-url=http://localhost:50568&resources=none\", true);\n", " xhr.open('GET', \"http://localhost:50851/autoload.js?bokeh-autoload-element=p7925&bokeh-absolute-url=http://localhost:50851&resources=none\", true);\n",
" xhr.onload = function (event) {\n", " xhr.onload = function (event) {\n",
" const script = document.createElement('script');\n", " const script = document.createElement('script');\n",
" const src = URL.createObjectURL(event.target.response);\n", " const src = URL.createObjectURL(event.target.response);\n",
@ -2054,7 +2054,7 @@
}, },
"metadata": { "metadata": {
"application/vnd.bokehjs_exec.v0+json": { "application/vnd.bokehjs_exec.v0+json": {
"server_id": "41ac9a694474477fa4aff5c18022d7c8" "server_id": "de3272d393da49aba1d3fd3516574d15"
} }
}, },
"output_type": "display_data" "output_type": "display_data"
@ -2063,11 +2063,25 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"aaaaaaa\n", "Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"aaaaaaa\n", "Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"aaaaaaa\n", "Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"aaaaaaa\n", "Index(['NAME', 'geometry', 'routes'], dtype='object')\n",
"aaaaaaa\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"
] ]
} }
], ],
@ -2080,6 +2094,7 @@
"from bokeh.models.ranges import Range1d\n", "from bokeh.models.ranges import Range1d\n",
"from bokeh.palettes import Reds\n", "from bokeh.palettes import Reds\n",
"from bokeh.models import LinearColorMapper, LogColorMapper, ColorBar\n", "from bokeh.models import LinearColorMapper, LogColorMapper, ColorBar\n",
"from shapely import Point\n",
"\n", "\n",
"yurop_json = yurop.to_json()\n", "yurop_json = yurop.to_json()\n",
"\n", "\n",
@ -2102,28 +2117,44 @@
" tools = \"pan, wheel_zoom, box_zoom, reset\")\n", " tools = \"pan, wheel_zoom, box_zoom, reset\")\n",
"\n", "\n",
" geo_ds = GeoJSONDataSource(geojson=yurop_json)\n", " geo_ds = GeoJSONDataSource(geojson=yurop_json)\n",
" \n",
" plotted_districts = p.patches('xs','ys', source = geo_ds,\n", " plotted_districts = p.patches('xs','ys', source = geo_ds,\n",
" line_color = 'black', \n", " line_color = 'black', \n",
" line_width = 0.25)\n", " line_width = 0.25)\n",
"\n", "\n",
" p.xgrid.grid_line_color = None\n",
" p.ygrid.grid_line_color = None\n",
" p.axis.visible = False\n",
"\n",
" p.patches(\"xs\",\"ys\", source = geo_ds,\n", " p.patches(\"xs\",\"ys\", source = geo_ds,\n",
" fill_color = {\"field\" : \"TotaleAbitantiCurr\",\n", " fill_color = {\"field\" : \"routes\",\n",
" \"transform\" : color_mapper},\n", " \"transform\" : color_mapper},\n",
" line_color = \"gray\", \n", " line_color = \"gray\", \n",
" line_width = 0.25, \n", " line_width = 0.25, \n",
" fill_alpha = 1)\n", " fill_alpha = 1)\n",
" \n", " \n",
" p.xgrid.grid_line_color = None\n",
" p.ygrid.grid_line_color = None\n",
" p.axis.visible = False\n",
" \n",
" p.add_tools(HoverTool(renderers = [plotted_districts],\n", " p.add_tools(HoverTool(renderers = [plotted_districts],\n",
" tooltips = [(\"Country\",\"@NAME\")]))\n", " tooltips = [(\"Country\",\"@NAME\"),(\"# routes\",\"@routes\")]))\n",
"\n", "\n",
" def event():\n", " tool = TapTool()\n",
" print(\"aaaaaaa\")\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",
" \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", "\n",
" tap = p.add_tools(TapTool())\n", " tap = p.add_tools()\n",
" p.on_event(Tap, event)\n", " p.on_event(Tap, event)\n",
" \n", " \n",
" p.add_layout(color_bar, \"right\")\n", " p.add_layout(color_bar, \"right\")\n",