Skip to content

Instantly share code, notes, and snippets.

@kantale
Created December 19, 2017 09:18
Show Gist options
  • Select an option

  • Save kantale/92437d2f99aa2e3cd4c611ea18827427 to your computer and use it in GitHub Desktop.

Select an option

Save kantale/92437d2f99aa2e3cd4c611ea18827427 to your computer and use it in GitHub Desktop.
Εισαγωγή στον προγραμματισμό με τη γλώσσα python 2017-2018, διάλεξη 9η
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Προγραμματισμός με τη γλώσσα python\n",
"### Alexandros Kanterakis [kantale@ics.forth.gr](kantale@ics.forth.gr)\n",
"\n",
"\n",
"### Διάλεξη 9η, Παρασκευή 15 Δεκεμβρίου 2017\n",
"\n",
"### Plotting (cont'd)\n",
"\n",
"\n",
"Μέχρι στιγμής έχουμε δει πως κάνουμε plot μέσω της matplotlib στις [περσινές](https://gist.github.com/kantale/c1df807a05353185666cd9189b0fa759) και [φετινές](https://gist.github.com/kantale/fb90ef176a35220a6fb11f9e43160236) σημειώσεις. Η matplotlib είναι κατάλληλη για \"στατικά\" plots που μπορούμε να σώσουμε σε ένα αρχείο εικόνας. Αυτά τα plots δεν μας επιτρέπουν να αλληλεπιδράσουμε με τα δεδομένα και να τα εξερευνήσουμε!\n",
"\n",
"Μία από τις πιο γνωστές βιβιλιοθήκες για αυτό το σκοπό είναι η [bokeh](https://bokeh.pydata.org/en/latest/). Για αρχή ας την εγκαταστήσουμε:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fetching package metadata .........\n",
"Solving package specifications: .\n",
"\n",
"Package plan for installation in environment /Users/alexandroskanterakis/anaconda3/envs/arkalos:\n",
"\n",
"The following NEW packages will be INSTALLED:\n",
"\n",
" bkcharts: 0.2-py36_0 \n",
" bokeh: 0.12.7-py36_0\n",
" jinja2: 2.9.6-py36_0 \n",
" markupsafe: 1.0-py36_0 \n",
" pyyaml: 3.12-py36_0 \n",
" tornado: 4.5.2-py36_0 \n",
" yaml: 0.1.6-0 \n",
"\n",
"yaml-0.1.6-0.t 100% |################################| Time: 0:00:00 1.76 MB/s\n",
"markupsafe-1.0 100% |################################| Time: 0:00:00 14.11 MB/s\n",
"pyyaml-3.12-py 100% |################################| Time: 0:00:00 3.07 MB/s\n",
"tornado-4.5.2- 100% |################################| Time: 0:00:00 1.91 MB/s\n",
"bkcharts-0.2-p 100% |################################| Time: 0:00:00 13.39 MB/s\n",
"jinja2-2.9.6-p 100% |################################| Time: 0:00:00 5.16 MB/s\n",
"bokeh-0.12.7-p 100% |################################| Time: 0:00:00 9.62 MB/s\n"
]
}
],
"source": [
"!conda install -y bokeh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Κάνουμε import:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from bokeh.io import output_notebook, show\n",
"from bokeh.plotting import figure"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Η παρακάτω εντολή, ρυθμίζει τη bokeh να ενσωματώνει τα plots στο jupyter:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"https://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"f9d6d8fa-600a-49a0-9923-28744e7fddf2\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" var force = true;\n",
"\n",
" if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
"\n",
"\n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 5000;\n",
" root._bokeh_failed_load = false;\n",
" }\n",
"\n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
"\n",
" function display_loaded() {\n",
" if (root.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"f9d6d8fa-600a-49a0-9923-28744e7fddf2\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
"\n",
"\n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" }\n",
" finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
"\n",
" function load_libs(js_urls, callback) {\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"f9d6d8fa-600a-49a0-9923-28744e7fddf2\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'f9d6d8fa-600a-49a0-9923-28744e7fddf2' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
"\n",
" var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.7.min.js\"];\n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" },\n",
" \n",
" function(Bokeh) {\n",
" \n",
" document.getElementById(\"f9d6d8fa-600a-49a0-9923-28744e7fddf2\").textContent = \"BokehJS is loading...\";\n",
" },\n",
" function(Bokeh) {\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.css\");\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.7.min.css\");\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.7.min.css\");\n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" \n",
" if ((root.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"f9d6d8fa-600a-49a0-9923-28744e7fddf2\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
"\n",
" }\n",
"\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
"}(window));"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"output_notebook()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ας φτιάξουμε ένα απλό plot:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" <div class=\"bk-root\">\n",
" <div class=\"bk-plotdiv\" id=\"b2c03490-0d58-4c20-a238-0d730bfdbac7\"></div>\n",
" </div>\n",
"<script type=\"text/javascript\">\n",
" \n",
" (function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
" \n",
" var force = false;\n",
" \n",
" if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
" \n",
" \n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 0;\n",
" root._bokeh_failed_load = false;\n",
" }\n",
" \n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
" \n",
" function display_loaded() {\n",
" if (root.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"b2c03490-0d58-4c20-a238-0d730bfdbac7\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
" \n",
" \n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" }\n",
" finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
" \n",
" function load_libs(js_urls, callback) {\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"b2c03490-0d58-4c20-a238-0d730bfdbac7\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'b2c03490-0d58-4c20-a238-0d730bfdbac7' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
" \n",
" var js_urls = [];\n",
" \n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" (function() {\n",
" var fn = function() {\n",
" var docs_json = {\"322bbb04-2d1a-4266-8784-97ed855868ce\":{\"roots\":{\"references\":[{\"attributes\":{\"data_source\":{\"id\":\"b81e9ed9-0152-4274-bf3a-d37607bf5ed9\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"ceb9cd93-151f-4949-b94c-d290264f976c\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"372d6118-8cfb-485d-9a34-4e06d7991db1\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"9913321a-3519-47e8-b019-f4f9806b717c\",\"type\":\"CDSView\"}},\"id\":\"7105bb20-8fe0-4ead-924a-d57dc79c5d53\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"88c58ca9-0ebe-455a-9310-bdccf682583b\",\"type\":\"LinearScale\"},{\"attributes\":{\"formatter\":{\"id\":\"1e6975a1-3f00-455c-be47-74c3d842528d\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"af4490a3-b9c3-4951-926d-8ac6908425e8\",\"type\":\"BasicTicker\"}},\"id\":\"04c5507a-7c5a-48d4-becc-4aeb3596dfee\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"230d6afe-6298-4e6f-87fd-77058551a3fc\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"3ed3ba6f-f691-4a76-8f73-a9d556ad46a3\",\"type\":\"SaveTool\"},{\"attributes\":{\"overlay\":{\"id\":\"f5e20912-ffe6-4fb9-ae12-6718808175ed\",\"type\":\"BoxAnnotation\"}},\"id\":\"fdca9fb6-f147-40e4-b69c-129888c4b55c\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"af4490a3-b9c3-4951-926d-8ac6908425e8\",\"type\":\"BasicTicker\"}},\"id\":\"36ee8856-b745-4cfc-80c9-9d06cc05429e\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"fa03b895-ae69-48b9-bfa6-90719a9d1f57\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"76b4e194-0553-4bc3-a217-3f83479a1e82\",\"type\":\"PanTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"f5e20912-ffe6-4fb9-ae12-6718808175ed\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"87d521ab-d623-4fbd-bafd-a7216b668a85\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"f3adaa2f-552a-4871-886c-8264e7617ed0\",\"type\":\"HelpTool\"},{\"attributes\":{\"plot\":{\"id\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1858f774-8baa-402f-8ca2-5d83abade2a4\",\"type\":\"BasicTicker\"}},\"id\":\"2ab6ecff-9c53-4200-94d5-b9eac405762c\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1e6975a1-3f00-455c-be47-74c3d842528d\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"af4490a3-b9c3-4951-926d-8ac6908425e8\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"e3a691c3-e545-4f7e-935b-2143b5b10aa4\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"source\":{\"id\":\"b81e9ed9-0152-4274-bf3a-d37607bf5ed9\",\"type\":\"ColumnDataSource\"}},\"id\":\"9913321a-3519-47e8-b019-f4f9806b717c\",\"type\":\"CDSView\"},{\"attributes\":{\"formatter\":{\"id\":\"e3a691c3-e545-4f7e-935b-2143b5b10aa4\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1858f774-8baa-402f-8ca2-5d83abade2a4\",\"type\":\"BasicTicker\"}},\"id\":\"165a7cc5-3a2b-4c25-8eac-41815766ee6c\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"e926b8b1-7257-411d-ac2a-772e446cd552\",\"type\":\"LinearScale\"},{\"attributes\":{\"callback\":null},\"id\":\"968d9ee9-6173-4dd3-9634-40d5dd3644b6\",\"type\":\"DataRange1d\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":[1,2,3,4,5],\"y\":[6,7,2,4,5]}},\"id\":\"b81e9ed9-0152-4274-bf3a-d37607bf5ed9\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"below\":[{\"id\":\"165a7cc5-3a2b-4c25-8eac-41815766ee6c\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"04c5507a-7c5a-48d4-becc-4aeb3596dfee\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"165a7cc5-3a2b-4c25-8eac-41815766ee6c\",\"type\":\"LinearAxis\"},{\"id\":\"2ab6ecff-9c53-4200-94d5-b9eac405762c\",\"type\":\"Grid\"},{\"id\":\"04c5507a-7c5a-48d4-becc-4aeb3596dfee\",\"type\":\"LinearAxis\"},{\"id\":\"36ee8856-b745-4cfc-80c9-9d06cc05429e\",\"type\":\"Grid\"},{\"id\":\"f5e20912-ffe6-4fb9-ae12-6718808175ed\",\"type\":\"BoxAnnotation\"},{\"id\":\"7105bb20-8fe0-4ead-924a-d57dc79c5d53\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"87d521ab-d623-4fbd-bafd-a7216b668a85\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"979240e2-7cf0-4dfa-b786-47eb9785f72e\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"cab0504f-dc29-4ab8-bd34-405c72eca15b\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"e926b8b1-7257-411d-ac2a-772e446cd552\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"968d9ee9-6173-4dd3-9634-40d5dd3644b6\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"88c58ca9-0ebe-455a-9310-bdccf682583b\",\"type\":\"LinearScale\"}},\"id\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"ceb9cd93-151f-4949-b94c-d290264f976c\",\"type\":\"Circle\"},{\"attributes\":{\"callback\":null},\"id\":\"cab0504f-dc29-4ab8-bd34-405c72eca15b\",\"type\":\"DataRange1d\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"372d6118-8cfb-485d-9a34-4e06d7991db1\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1858f774-8baa-402f-8ca2-5d83abade2a4\",\"type\":\"BasicTicker\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"76b4e194-0553-4bc3-a217-3f83479a1e82\",\"type\":\"PanTool\"},{\"id\":\"fa03b895-ae69-48b9-bfa6-90719a9d1f57\",\"type\":\"WheelZoomTool\"},{\"id\":\"fdca9fb6-f147-40e4-b69c-129888c4b55c\",\"type\":\"BoxZoomTool\"},{\"id\":\"3ed3ba6f-f691-4a76-8f73-a9d556ad46a3\",\"type\":\"SaveTool\"},{\"id\":\"230d6afe-6298-4e6f-87fd-77058551a3fc\",\"type\":\"ResetTool\"},{\"id\":\"f3adaa2f-552a-4871-886c-8264e7617ed0\",\"type\":\"HelpTool\"}]},\"id\":\"979240e2-7cf0-4dfa-b786-47eb9785f72e\",\"type\":\"Toolbar\"}],\"root_ids\":[\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.7\"}};\n",
" var render_items = [{\"docid\":\"322bbb04-2d1a-4266-8784-97ed855868ce\",\"elementid\":\"b2c03490-0d58-4c20-a238-0d730bfdbac7\",\"modelid\":\"73db6b19-2600-4d1c-b946-f8ab3c0e763b\"}];\n",
" \n",
" Bokeh.embed.embed_items(docs_json, render_items);\n",
" };\n",
" if (document.readyState != \"loading\") fn();\n",
" else document.addEventListener(\"DOMContentLoaded\", fn);\n",
" })();\n",
" },\n",
" function(Bokeh) {\n",
" }\n",
" ];\n",
" \n",
" function run_inline_js() {\n",
" \n",
" if ((root.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"b2c03490-0d58-4c20-a238-0d730bfdbac7\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
" \n",
" }\n",
" \n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
" }(window));\n",
"</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# create a new plot with default tools, using figure\n",
"p = figure()\n",
"\n",
"# Βάζουμε κύκλους σε συγκεκριμένα σημεία \n",
"p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5],)\n",
"\n",
"show(p) # showtime!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Για αρχή παρατηρήστε ότι μπορείτε να αλληλεπιδράσετε με αυτό το plot. \n",
"\n",
"Ας κάνουμε τους κύκλους να έχουν διαφορετικό χρώμα και μέγεθος:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" <div class=\"bk-root\">\n",
" <div class=\"bk-plotdiv\" id=\"96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf\"></div>\n",
" </div>\n",
"<script type=\"text/javascript\">\n",
" \n",
" (function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
" \n",
" var force = false;\n",
" \n",
" if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
" \n",
" \n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 0;\n",
" root._bokeh_failed_load = false;\n",
" }\n",
" \n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
" \n",
" function display_loaded() {\n",
" if (root.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
" \n",
" \n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" }\n",
" finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
" \n",
" function load_libs(js_urls, callback) {\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid '96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
" \n",
" var js_urls = [];\n",
" \n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" (function() {\n",
" var fn = function() {\n",
" var docs_json = {\"f3982369-9e0d-4124-83f0-19f02b743adf\":{\"roots\":{\"references\":[{\"attributes\":{},\"id\":\"80816be4-b65e-4abb-af59-4dff3980c9d6\",\"type\":\"HelpTool\"},{\"attributes\":{\"callback\":null},\"id\":\"3e0b007f-bed1-49b4-9e6a-17c81ed3f0fb\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"288166cc-007e-4219-8738-56b0bac51ab5\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"callback\":null},\"id\":\"e00d4c8b-3f3e-4bc9-9d2f-a3050c609385\",\"type\":\"DataRange1d\"},{\"attributes\":{\"plot\":{\"id\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bb75f0eb-3169-4b51-a5a5-2d456a8dd423\",\"type\":\"BasicTicker\"}},\"id\":\"800330ba-b00b-4cb0-aaef-ad6e7767a976\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"c67d48e5-0b6f-4ea3-b798-918d3a153e3a\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"74acdf5b-8c5c-4bdd-8b2d-55df2c54a84f\",\"type\":\"LinearScale\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"8f857510-c400-4a4c-9d54-db590964f293\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"e2a53941-6c14-46b0-b585-4c353d09af2d\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"6874ce2d-d613-4250-8e51-b5b30aa68753\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"2df67bf1-9783-40c0-bbab-7d3c9863da75\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"3a945062-c82f-498a-8eb1-1d39fe04f7c3\",\"type\":\"LinearScale\"},{\"attributes\":{\"formatter\":{\"id\":\"6874ce2d-d613-4250-8e51-b5b30aa68753\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bb75f0eb-3169-4b51-a5a5-2d456a8dd423\",\"type\":\"BasicTicker\"}},\"id\":\"e7039a0a-3c05-4ff6-a2f7-9a9cd46575b6\",\"type\":\"LinearAxis\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"14953095-c16e-4b38-97a2-16db7d8c29a4\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"formatter\":{\"id\":\"c67d48e5-0b6f-4ea3-b798-918d3a153e3a\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"ca4392dc-7e66-43c4-a7c6-85b7dce62312\",\"type\":\"BasicTicker\"}},\"id\":\"0602cf3e-f619-4537-82d2-70b7523791f8\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"ca4392dc-7e66-43c4-a7c6-85b7dce62312\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"bb75f0eb-3169-4b51-a5a5-2d456a8dd423\",\"type\":\"BasicTicker\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"ca4392dc-7e66-43c4-a7c6-85b7dce62312\",\"type\":\"BasicTicker\"}},\"id\":\"81329ddb-031c-4817-be7f-72be60681cd7\",\"type\":\"Grid\"},{\"attributes\":{\"data_source\":{\"id\":\"aadf099f-9d34-4a82-8ffa-84820ecd3983\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"4b2c42b0-8ffe-4127-be16-18845f57de60\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"4a151417-0887-48c7-85cf-95b4b2a54821\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"10c8aaaa-81a8-4d1a-b945-8e0f65f19d40\",\"type\":\"CDSView\"}},\"id\":\"d07af1f4-6cb3-42ee-b145-ec5816692e10\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"field\":\"size\",\"units\":\"screen\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4a151417-0887-48c7-85cf-95b4b2a54821\",\"type\":\"Circle\"},{\"attributes\":{\"source\":{\"id\":\"aadf099f-9d34-4a82-8ffa-84820ecd3983\",\"type\":\"ColumnDataSource\"}},\"id\":\"10c8aaaa-81a8-4d1a-b945-8e0f65f19d40\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"f22c0f22-9554-40ca-8d8f-2408e5281fe5\",\"type\":\"PanTool\"},{\"attributes\":{\"overlay\":{\"id\":\"14953095-c16e-4b38-97a2-16db7d8c29a4\",\"type\":\"BoxAnnotation\"}},\"id\":\"13a080bb-539e-4f33-a2bb-e1fc2f8f4305\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"f22c0f22-9554-40ca-8d8f-2408e5281fe5\",\"type\":\"PanTool\"},{\"id\":\"288166cc-007e-4219-8738-56b0bac51ab5\",\"type\":\"WheelZoomTool\"},{\"id\":\"13a080bb-539e-4f33-a2bb-e1fc2f8f4305\",\"type\":\"BoxZoomTool\"},{\"id\":\"e2a53941-6c14-46b0-b585-4c353d09af2d\",\"type\":\"SaveTool\"},{\"id\":\"2df67bf1-9783-40c0-bbab-7d3c9863da75\",\"type\":\"ResetTool\"},{\"id\":\"80816be4-b65e-4abb-af59-4dff3980c9d6\",\"type\":\"HelpTool\"}]},\"id\":\"2f611611-abac-4503-9a4b-9719d2e24da7\",\"type\":\"Toolbar\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"size\",\"x\",\"y\",\"fill_color\"],\"data\":{\"fill_color\":[\"orange\",\"orange\",\"orange\",\"orange\",\"pink\"],\"size\":[10,11,12,13,14],\"x\":[1,2,3,4,5],\"y\":[6,7,2,4,5]}},\"id\":\"aadf099f-9d34-4a82-8ffa-84820ecd3983\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"below\":[{\"id\":\"e7039a0a-3c05-4ff6-a2f7-9a9cd46575b6\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"0602cf3e-f619-4537-82d2-70b7523791f8\",\"type\":\"LinearAxis\"}],\"plot_height\":400,\"plot_width\":400,\"renderers\":[{\"id\":\"e7039a0a-3c05-4ff6-a2f7-9a9cd46575b6\",\"type\":\"LinearAxis\"},{\"id\":\"800330ba-b00b-4cb0-aaef-ad6e7767a976\",\"type\":\"Grid\"},{\"id\":\"0602cf3e-f619-4537-82d2-70b7523791f8\",\"type\":\"LinearAxis\"},{\"id\":\"81329ddb-031c-4817-be7f-72be60681cd7\",\"type\":\"Grid\"},{\"id\":\"14953095-c16e-4b38-97a2-16db7d8c29a4\",\"type\":\"BoxAnnotation\"},{\"id\":\"d07af1f4-6cb3-42ee-b145-ec5816692e10\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"8f857510-c400-4a4c-9d54-db590964f293\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"2f611611-abac-4503-9a4b-9719d2e24da7\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"3e0b007f-bed1-49b4-9e6a-17c81ed3f0fb\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"3a945062-c82f-498a-8eb1-1d39fe04f7c3\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"e00d4c8b-3f3e-4bc9-9d2f-a3050c609385\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"74acdf5b-8c5c-4bdd-8b2d-55df2c54a84f\",\"type\":\"LinearScale\"}},\"id\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"field\":\"fill_color\"},\"line_color\":{\"value\":\"navy\"},\"size\":{\"field\":\"size\",\"units\":\"screen\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"4b2c42b0-8ffe-4127-be16-18845f57de60\",\"type\":\"Circle\"}],\"root_ids\":[\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.7\"}};\n",
" var render_items = [{\"docid\":\"f3982369-9e0d-4124-83f0-19f02b743adf\",\"elementid\":\"96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf\",\"modelid\":\"aacf87d8-6e25-42bf-b9e7-5aaed1817c96\"}];\n",
" \n",
" Bokeh.embed.embed_items(docs_json, render_items);\n",
" };\n",
" if (document.readyState != \"loading\") fn();\n",
" else document.addEventListener(\"DOMContentLoaded\", fn);\n",
" })();\n",
" },\n",
" function(Bokeh) {\n",
" }\n",
" ];\n",
" \n",
" function run_inline_js() {\n",
" \n",
" if ((root.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"96da9ff7-4a5a-4c13-b6d0-7aab5b3543cf\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
" \n",
" }\n",
" \n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
" }(window));\n",
"</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# create a new plot with default tools, using figure\n",
"p = figure(plot_width=400, plot_height=400)\n",
"\n",
"# add a circle renderer with a size, color, and alpha\n",
"p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], \n",
" size=[10,11,12,13,14],\n",
" line_color=\"navy\", \n",
" fill_color=[\"orange\"]*4+['pink'], fill_alpha=0.5)\n",
"\n",
"show(p) # show the results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Η πιο σημαντική ίσως ιδιότητα της bokeh είναι ότι σας επιτρέπει να σώσεται τα plots σε μορφή [html](https://en.wikipedia.org/wiki/HTML). Με αυτόν τον τρόπο μπορείτε όχι μόνο να \"στείλετε\" ένα plot κάπου αλλού, αλλά και να το \"ανεβάσετε\" στο Internet! "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from bokeh.io import save"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/alexandroskanterakis/anaconda3/envs/arkalos/lib/python3.6/site-packages/bokeh/io.py:527: UserWarning: save() called but no resources were supplied and output_file(...) was never called, defaulting to resources.CDN\n",
" warnings.warn(\"save() called but no resources were supplied and output_file(...) was never called, defaulting to resources.CDN\")\n",
"/Users/alexandroskanterakis/anaconda3/envs/arkalos/lib/python3.6/site-packages/bokeh/io.py:537: UserWarning: save() called but no title was supplied and output_file(...) was never called, using default title 'Bokeh Plot'\n",
" warnings.warn(\"save() called but no title was supplied and output_file(...) was never called, using default title 'Bokeh Plot'\")\n"
]
},
{
"data": {
"text/plain": [
"'/Users/alexandroskanterakis/Downloads/graph.html'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"save(p, 'graph.html')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Δοκιμάστε τώρα να ανοίξετε το αρχείο ```graph.html``` από τον browser σας (File -> Open..)\n",
"\n",
"Η bokeh είναι πολύ πλούσια βιβλιοθήκη. Σε αυτό link: [http://nbviewer.jupyter.org/github/bokeh/bokeh-notebooks/blob/master/tutorial/00%20-%20Introduction%20and%20Setup.ipynb](http://nbviewer.jupyter.org/github/bokeh/bokeh-notebooks/blob/master/tutorial/00%20-%20Introduction%20and%20Setup.ipynb) υπάρχει ένας πολύ αναλυτικός οδηγός με τις περισσότερες λειτουργίες της."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Με τις παρακάτω εντολές εγκαθιστά διάφορα δοκιμαστικά datasets:"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating /Users/alexandroskanterakis/.bokeh directory\n",
"Creating /Users/alexandroskanterakis/.bokeh/data directory\n",
"Using data directory: /Users/alexandroskanterakis/.bokeh/data\n",
"Downloading: CGM.csv (1589982 bytes)\n",
" 1589982 [100.00%]\n",
"Downloading: US_Counties.zip (3182088 bytes)\n",
" 3182088 [100.00%]\n",
"Unpacking: US_Counties.csv\n",
"Downloading: us_cities.json (713565 bytes)\n",
" 713565 [100.00%]\n",
"Downloading: unemployment09.csv (253301 bytes)\n",
" 253301 [100.00%]\n",
"Downloading: AAPL.csv (166698 bytes)\n",
" 166698 [100.00%]\n",
"Downloading: FB.csv (9706 bytes)\n",
" 9706 [100.00%]\n",
"Downloading: GOOG.csv (113894 bytes)\n",
" 113894 [100.00%]\n",
"Downloading: IBM.csv (165625 bytes)\n",
" 165625 [100.00%]\n",
"Downloading: MSFT.csv (161614 bytes)\n",
" 161614 [100.00%]\n",
"Downloading: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip (5148539 bytes)\n",
" 5148539 [100.00%]\n",
"Unpacking: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.csv\n",
"Downloading: gapminder_fertility.csv (64346 bytes)\n",
" 64346 [100.00%]\n",
"Downloading: gapminder_population.csv (94509 bytes)\n",
" 94509 [100.00%]\n",
"Downloading: gapminder_life_expectancy.csv (73243 bytes)\n",
" 73243 [100.00%]\n",
"Downloading: gapminder_regions.csv (7781 bytes)\n",
" 7781 [100.00%]\n",
"Downloading: world_cities.zip (646858 bytes)\n",
" 646858 [100.00%]\n",
"Unpacking: world_cities.csv\n",
"Downloading: airports.json (6373 bytes)\n",
" 6373 [100.00%]\n",
"Downloading: movies.db.zip (5067833 bytes)\n",
" 5067833 [100.00%]\n",
"Unpacking: movies.db\n"
]
}
],
"source": [
"import bokeh\n",
"bokeh.sampledata.download()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Με τη παρακάτω εντολή εγκαθιστούμε τη [pandas](https://pandas.pydata.org/) την οποία θα χρειαστούμε για το επόμενο παράδειγμα. Σε επόμενη διάλεξη θα κάνουμε μία αναλυτική παρουσιάση της pandas. "
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fetching package metadata .........\n",
"Solving package specifications: .\n",
"\n",
"# All requested packages already installed.\n",
"# packages in environment at /Users/alexandroskanterakis/anaconda3/envs/arkalos:\n",
"#\n",
"pandas 0.20.3 py36_0 \n"
]
}
],
"source": [
"!conda install -y pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Μία από τις πιο βασικές ιδιότητες της bokeh είναι ότι μπορούμε να προσθέσουμε [tooltips](https://en.wikipedia.org/wiki/Tooltip). Τα tooltips είναι διάφορες (μετα)πληροφορίες που εμφανίζονται όταν \"περνάμε\" το ποντίκι πάνω από ένα σημείο. "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" <div class=\"bk-root\">\n",
" <div class=\"bk-plotdiv\" id=\"776159e5-a9a9-4ea8-8bab-829995359fae\"></div>\n",
" </div>\n",
"<script type=\"text/javascript\">\n",
" \n",
" (function(root) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
" \n",
" var force = false;\n",
" \n",
" if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
" root._bokeh_onload_callbacks = [];\n",
" root._bokeh_is_loading = undefined;\n",
" }\n",
" \n",
" \n",
" \n",
" if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
" root._bokeh_timeout = Date.now() + 0;\n",
" root._bokeh_failed_load = false;\n",
" }\n",
" \n",
" var NB_LOAD_WARNING = {'data': {'text/html':\n",
" \"<div style='background-color: #fdd'>\\n\"+\n",
" \"<p>\\n\"+\n",
" \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
" \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
" \"</p>\\n\"+\n",
" \"<ul>\\n\"+\n",
" \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
" \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
" \"</ul>\\n\"+\n",
" \"<code>\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"</code>\\n\"+\n",
" \"</div>\"}};\n",
" \n",
" function display_loaded() {\n",
" if (root.Bokeh !== undefined) {\n",
" var el = document.getElementById(\"776159e5-a9a9-4ea8-8bab-829995359fae\");\n",
" if (el != null) {\n",
" el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
" }\n",
" } else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(display_loaded, 100)\n",
" }\n",
" }\n",
" \n",
" \n",
" function run_callbacks() {\n",
" try {\n",
" root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" }\n",
" finally {\n",
" delete root._bokeh_onload_callbacks\n",
" }\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
" \n",
" function load_libs(js_urls, callback) {\n",
" root._bokeh_onload_callbacks.push(callback);\n",
" if (root._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" root._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" root._bokeh_is_loading--;\n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"776159e5-a9a9-4ea8-8bab-829995359fae\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid '776159e5-a9a9-4ea8-8bab-829995359fae' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
" \n",
" var js_urls = [];\n",
" \n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" (function() {\n",
" var fn = function() {\n",
" var docs_json = {\"03e91df5-1b5a-4928-8dce-cb3abc0f6a42\":{\"roots\":{\"references\":[{\"attributes\":{\"formatter\":{\"id\":\"85ec7c17-d929-4ed7-96f0-a454de879bb5\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"24131c10-7cc4-47f4-8133-271bfb0ad7e5\",\"type\":\"BasicTicker\"}},\"id\":\"18b9bfc6-40ce-4844-95d0-01719c480135\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"334f6c65-bc75-4a05-b448-30f3ca9ee250\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null},\"id\":\"3cb398f2-f147-4b48-aaa4-89edf2386ee1\",\"type\":\"DataRange1d\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"38c2317c-f592-487d-bc43-1651ec8325ff\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"78180e3d-46b8-48a4-9501-bcdf4daa60e1\",\"type\":\"BasicTicker\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"index\",\"$index\"],[\"(x,y)\",\"($x, $y)\"],[\"desc\",\"@desc\"],[\"Mitsos\",\"@example\"]]},\"id\":\"0198a41b-991d-4cc8-a6d0-41bbc1e328cf\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"85ec7c17-d929-4ed7-96f0-a454de879bb5\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"plot\":{\"id\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"24131c10-7cc4-47f4-8133-271bfb0ad7e5\",\"type\":\"BasicTicker\"}},\"id\":\"11ce8f11-f2f0-4489-924b-0c4b71f043b2\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"ec7d003f-e78c-4ca0-9395-7b452b1f45b0\",\"type\":\"LinearScale\"},{\"attributes\":{\"source\":{\"id\":\"c305a138-edaa-4e0d-8e2f-5d6203f77518\",\"type\":\"ColumnDataSource\"}},\"id\":\"96df6028-597e-4bfc-b4b9-add17d0b63e4\",\"type\":\"CDSView\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"78180e3d-46b8-48a4-9501-bcdf4daa60e1\",\"type\":\"BasicTicker\"}},\"id\":\"8bcc4d47-fd72-46c2-8dbb-99562c615cb1\",\"type\":\"Grid\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"0198a41b-991d-4cc8-a6d0-41bbc1e328cf\",\"type\":\"HoverTool\"}]},\"id\":\"fc9ef640-932d-427b-ac7e-972691b7a2df\",\"type\":\"Toolbar\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\",\"desc\",\"example\"],\"data\":{\"desc\":[\"A\",\"b\",\"C\",\"d\",\"E\"],\"example\":[\"aa\",\"bb\",\"cc\",\"dd\",\"ee\"],\"x\":[1,2,3,4,5],\"y\":[2,5,8,2,7]}},\"id\":\"c305a138-edaa-4e0d-8e2f-5d6203f77518\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"plot\":null,\"text\":\"Mouse over the dots\"},\"id\":\"10485c72-af63-4ef8-aa83-2458ef47d83c\",\"type\":\"Title\"},{\"attributes\":{\"below\":[{\"id\":\"18b9bfc6-40ce-4844-95d0-01719c480135\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"643635a7-1b5b-4e22-9793-c92524737696\",\"type\":\"LinearAxis\"}],\"plot_height\":300,\"plot_width\":300,\"renderers\":[{\"id\":\"18b9bfc6-40ce-4844-95d0-01719c480135\",\"type\":\"LinearAxis\"},{\"id\":\"11ce8f11-f2f0-4489-924b-0c4b71f043b2\",\"type\":\"Grid\"},{\"id\":\"643635a7-1b5b-4e22-9793-c92524737696\",\"type\":\"LinearAxis\"},{\"id\":\"8bcc4d47-fd72-46c2-8dbb-99562c615cb1\",\"type\":\"Grid\"},{\"id\":\"97a8b8b4-ea38-4e25-a25f-50855c27c92f\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"10485c72-af63-4ef8-aa83-2458ef47d83c\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"fc9ef640-932d-427b-ac7e-972691b7a2df\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"3cb398f2-f147-4b48-aaa4-89edf2386ee1\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"a2ee4750-4e37-41fd-8562-a19ce20131b5\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"dcd6177c-c8a2-4f0c-8c4a-6d3740dd7b71\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"ec7d003f-e78c-4ca0-9395-7b452b1f45b0\",\"type\":\"LinearScale\"}},\"id\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"formatter\":{\"id\":\"334f6c65-bc75-4a05-b448-30f3ca9ee250\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"78180e3d-46b8-48a4-9501-bcdf4daa60e1\",\"type\":\"BasicTicker\"}},\"id\":\"643635a7-1b5b-4e22-9793-c92524737696\",\"type\":\"LinearAxis\"},{\"attributes\":{\"callback\":null},\"id\":\"dcd6177c-c8a2-4f0c-8c4a-6d3740dd7b71\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"24131c10-7cc4-47f4-8133-271bfb0ad7e5\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"a2ee4750-4e37-41fd-8562-a19ce20131b5\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":20},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"cf2620b5-9ce7-42d2-9d80-0cff1e7ab9d2\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"c305a138-edaa-4e0d-8e2f-5d6203f77518\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"cf2620b5-9ce7-42d2-9d80-0cff1e7ab9d2\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"38c2317c-f592-487d-bc43-1651ec8325ff\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"96df6028-597e-4bfc-b4b9-add17d0b63e4\",\"type\":\"CDSView\"}},\"id\":\"97a8b8b4-ea38-4e25-a25f-50855c27c92f\",\"type\":\"GlyphRenderer\"}],\"root_ids\":[\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.7\"}};\n",
" var render_items = [{\"docid\":\"03e91df5-1b5a-4928-8dce-cb3abc0f6a42\",\"elementid\":\"776159e5-a9a9-4ea8-8bab-829995359fae\",\"modelid\":\"d04f3ac0-23d2-4461-b96e-f7f2fbad2d07\"}];\n",
" \n",
" Bokeh.embed.embed_items(docs_json, render_items);\n",
" };\n",
" if (document.readyState != \"loading\") fn();\n",
" else document.addEventListener(\"DOMContentLoaded\", fn);\n",
" })();\n",
" },\n",
" function(Bokeh) {\n",
" }\n",
" ];\n",
" \n",
" function run_inline_js() {\n",
" \n",
" if ((root.Bokeh !== undefined) || (force === true)) {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i].call(root, root.Bokeh);\n",
" }if (force === true) {\n",
" display_loaded();\n",
" }} else if (Date.now() < root._bokeh_timeout) {\n",
" setTimeout(run_inline_js, 100);\n",
" } else if (!root._bokeh_failed_load) {\n",
" console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
" root._bokeh_failed_load = true;\n",
" } else if (force !== true) {\n",
" var cell = $(document.getElementById(\"776159e5-a9a9-4ea8-8bab-829995359fae\")).parents('.cell').data().cell;\n",
" cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
" }\n",
" \n",
" }\n",
" \n",
" if (root._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
" }(window));\n",
"</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\n",
"from bokeh.models import TapTool, CustomJS, ColumnDataSource\n",
"from bokeh.models import HoverTool\n",
"\n",
"from bokeh.io import output_file\n",
"output_file('plot.html')\n",
"\n",
"source = ColumnDataSource(\n",
" data=dict(\n",
" x=[1, 2, 3, 4, 5],\n",
" y=[2, 5, 8, 2, 7],\n",
" desc=['A', 'b', 'C', 'd', 'E'],\n",
" example=['aa', 'bb', 'cc', 'dd', 'ee'],\n",
" )\n",
" )\n",
"\n",
"hover = HoverTool(\n",
" tooltips=[\n",
" (\"index\", \"$index\"),\n",
" (\"(x,y)\", \"($x, $y)\"),\n",
" (\"desc\", \"@desc\"),\n",
" (\"Mitsos\", \"@example\"),\n",
" ]\n",
" )\n",
"\n",
"p = figure(plot_width=300, plot_height=300, tools=[hover], title=\"Mouse over the dots\")\n",
"\n",
"p.circle('x', 'y', size=20, source=source)\n",
"\n",
"show(p)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ασκήσεις \n",
"\n",
"Αυτή η σειρά ασκήσεων έχει μόνο μία άσκηση. Θα πρέπει να κάνετε ένα plot το οποίο θα το σώσετε σε μορφή html. Στη συνέχεια και σε συνεργασία με τους διδάσκοντες στο μάθημα BIO102 θα πρέπει να \"ανεβάσετε\" το plot στη προσωπική σας σελίδα.\n",
"\n",
"Στείλτε το plot:\n",
"* Σε φορμάτ html στο [kantale@ics.forth.gr](mailto:kantale@ics.forth.gr) ή\n",
"* Ανεβάστε το στη προσωπική σας σελίδα και στείλτε μου το link\n",
"\n",
"### Περιγραφή:\n",
"\n",
"Σε αυτή τη σελίδα: [https://www.ebi.ac.uk/gwas/docs/file-downloads](https://www.ebi.ac.uk/gwas/docs/file-downloads) υπάρχει κατάλογος με τα πεισσότερα [GWAS](https://en.wikipedia.org/wiki/Genome-wide_association_study) πειράματα που έχουν γίνει. Μπορούμε να το κατεβάσουμε με τη παρακάτω εντολή:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2017-12-18 20:04:08-- https://www.ebi.ac.uk/gwas/api/search/downloads/full\n",
"Resolving www.ebi.ac.uk... 193.62.193.80\n",
"Connecting to www.ebi.ac.uk|193.62.193.80|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: unspecified [text/tsv]\n",
"Saving to: ‘gwas_catalogue.tsv’\n",
"\n",
"gwas_catalogue.tsv [ <=> ] 34.17M 1.03MB/s in 33s \n",
"\n",
"2017-12-18 20:04:42 (1.03 MB/s) - ‘gwas_catalogue.tsv’ saved [35832863]\n",
"\n"
]
}
],
"source": [
"!wget -O gwas_catalogue.tsv https://www.ebi.ac.uk/gwas/api/search/downloads/full"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ας διαβάσουμε τη πρώτη γραμμή του αρχείου:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'DATE ADDED TO CATALOG\\tPUBMEDID\\tFIRST AUTHOR\\tDATE\\tJOURNAL\\tLINK\\tSTUDY\\tDISEASE/TRAIT\\tINITIAL SAMPLE SIZE\\tREPLICATION SAMPLE SIZE\\tREGION\\tCHR_ID\\tCHR_POS\\tREPORTED GENE(S)\\tMAPPED_GENE\\tUPSTREAM_GENE_ID\\tDOWNSTREAM_GENE_ID\\tSNP_GENE_IDS\\tUPSTREAM_GENE_DISTANCE\\tDOWNSTREAM_GENE_DISTANCE\\tSTRONGEST SNP-RISK ALLELE\\tSNPS\\tMERGED\\tSNP_ID_CURRENT\\tCONTEXT\\tINTERGENIC\\tRISK ALLELE FREQUENCY\\tP-VALUE\\tPVALUE_MLOG\\tP-VALUE (TEXT)\\tOR or BETA\\t95% CI (TEXT)\\tPLATFORM [SNPS PASSING QC]\\tCNV\\n'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"file = open('gwas_catalogue.tsv')\n",
"line_1 = file.readline()\n",
"line_1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Το αρχείο είναι tab delimited. Ας το κάνουμε split:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['DATE ADDED TO CATALOG',\n",
" 'PUBMEDID',\n",
" 'FIRST AUTHOR',\n",
" 'DATE',\n",
" 'JOURNAL',\n",
" 'LINK',\n",
" 'STUDY',\n",
" 'DISEASE/TRAIT',\n",
" 'INITIAL SAMPLE SIZE',\n",
" 'REPLICATION SAMPLE SIZE',\n",
" 'REGION',\n",
" 'CHR_ID',\n",
" 'CHR_POS',\n",
" 'REPORTED GENE(S)',\n",
" 'MAPPED_GENE',\n",
" 'UPSTREAM_GENE_ID',\n",
" 'DOWNSTREAM_GENE_ID',\n",
" 'SNP_GENE_IDS',\n",
" 'UPSTREAM_GENE_DISTANCE',\n",
" 'DOWNSTREAM_GENE_DISTANCE',\n",
" 'STRONGEST SNP-RISK ALLELE',\n",
" 'SNPS',\n",
" 'MERGED',\n",
" 'SNP_ID_CURRENT',\n",
" 'CONTEXT',\n",
" 'INTERGENIC',\n",
" 'RISK ALLELE FREQUENCY',\n",
" 'P-VALUE',\n",
" 'PVALUE_MLOG',\n",
" 'P-VALUE (TEXT)',\n",
" 'OR or BETA',\n",
" '95% CI (TEXT)',\n",
" 'PLATFORM [SNPS PASSING QC]',\n",
" 'CNV\\n']"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"line_1_splitted = line_1.split('\\t')\n",
"line_1_splitted"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Αυτό το αρχείο λοιπόν περιέχει όλα τα SNPs για τα οποία έχει βρεθεί κάποια στατιστική συσχέτιση με κάποιον φαινότυπο. Οι στήλες που μας ενδιαφέρουν είναι:\n",
"* \"DATE\" : είναι η ημερομηνία που δημοσιεύθηκε η συσχέτιση\n",
"* \"P-VALUE\" : Το p-value της συσχέτισης \n",
"* \"INITIAL SAMPLE SIZE\": το πλήθος των samples που είχε η μελέτη όταν βρέθηκε η συσχέτιση\n",
"* \"STUDY\": Ο τίτλος του paper που δημοσιεύθηκε η συσχέτιση.\n",
"\n",
"Το plot σας θα έχει στον χ-άξονα την ημερομηνία που έγινε η δημοσίευση (DATA). Στον χ-άξονα, οι ημερομηνίες πρέπει να είναι ταξινομημένες από τη πιο παλιά (αριστερά) στη πιο πρόσφατη (δεξιά). Θα πρέπει δηλαδή να ταξινομήσετε τα δεδομένα με βάση την ημερομηνία. Ο y άξονας θα έχει τον αρνητικό λογάριθμο του p-value. Επειδή το p-value συνήθως είναι πολύ μικρό, χρησιμοποιούμε τον αρνητικό λογάριθμο για να τον αναπαραστήσουμε. Για παράδειγμα:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.3979400086720375"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
"p_value = 4E-7 # 4*10^(-7)\n",
"\n",
"def negative_log(p):\n",
" return -math.log10(p)\n",
"\n",
"negative_log(p_value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Κάθε SNP θα αναπαριστάται με έναν κύκλο με μέγεθος ανάλογα με το πλήθος των samples που συμμετείχαν στη μελέτη (πεδίο ```INITIAL_SAMPLE_SIZE```). \n",
"\n",
"Όταν ο χρήστης περνάει με το ποντίκι πάνω από κάποιον κύκλο θα πρέπει να του εμφανίζεται ο τίτλος του paper όπου έγινε η δημοσίευση.\n",
"\n",
"Σημείωση: Αν τα δεδομένα είναι πάρα πολλά και το plot είναι πολύ πυκνό τότε μπορείτε να βάλετε κάποιο φίλτρο π.χ. Μόνο τα SNPs που δημοσιεύθηκαν από το 2013 και μετά ή/και αυτά που είναι στο χρωμόσωμα 1.\n",
"\n",
"Προσπαθήστε το plot να είναι ταυτόχρονα χρηστικό και αισθητικά ωραίο!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@kantale
Copy link
Copy Markdown
Author

kantale commented Jan 29, 2018

Όσοι κάνετε την άσκηση με pandas (που είναι μια καλή ιδέα) η παρακάτω εντολή μετατρέπει το column DATE από string σε Date format

data['DATE'] = pd.to_datetime(data['DATE'], format='%Y-%m-%d')

@kantale
Copy link
Copy Markdown
Author

kantale commented Feb 3, 2018

Παρατηρούμε ότι το INITIAL SAMPLE SIZE περιέχει text από το οποίο πρέπει να "εξάγουμε" κάποιο νούμερο. Μπορείτε να χρησιμοποιήσετε αυτή τη μέθοδο για να το κάνετε αυτό:

import re
import pandas as pd

def convert(x):
    # Get the first number
    s = re.search(r'[\d,\.]', x)
    if not s:
        print ('Warning. Could not find a number at the field:', x)
        return 0
    
    #Remove commas
    found = s.group(0).replace(',', '').replace('.', '')
    if not found:
        print ('Warning: Could not parse:', x)
        return 0
    return int(found)

df = pd.read_csv('gwas_catalogue.tsv', sep='\t', converters = {
    'INITIAL SAMPLE SIZE': convert})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment