Skip to content

Commit 65d9e95

Browse files
committed
code bundle updated
1 parent d108f80 commit 65d9e95

14 files changed

+3821
-0
lines changed

Lesson05/.ipynb_checkpoints/Exercise38-checkpoint.ipynb

+319
Large diffs are not rendered by default.

Lesson05/.ipynb_checkpoints/Exercise39-checkpoint.ipynb

+403
Large diffs are not rendered by default.

Lesson05/.ipynb_checkpoints/Exercise40-checkpoint.ipynb

+422
Large diffs are not rendered by default.

Lesson05/.ipynb_checkpoints/Exercise41-checkpoint.ipynb

+131
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#Import pandas library and read DataFrame from DATA_PATH \n",
10+
"\n",
11+
"import pandas as pd \n",
12+
"%matplotlib inline\n",
13+
"import numpy as np \n",
14+
"\n",
15+
"from pathlib import Path\n",
16+
"from bokeh.plotting import figure, show, output_file\n",
17+
"from bokeh.plotting import figure, output_notebook, show, ColumnDataSource\n",
18+
"\n",
19+
"DATA_PATH = Path('../datasets/chap5_data/')"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"**set the output as notebook**"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"output_notebook()"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"**Read the data as Dataframe. Filter the rows according to countries(`UK` and `France`).\n",
43+
"Make it as `ColumnDataSource` so that bokeh can access it by columns names**"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"uk_eu_population = pd.read_csv(DATA_PATH / \"uk_europe_population_2005_2019.csv\")\n",
53+
"uk_population = uk_eu_population[uk_eu_population.country == 'UK']\n",
54+
"source_uk = ColumnDataSource(dict(year=uk_population.year, change=uk_population.change))\n",
55+
"france_population = uk_eu_population[uk_eu_population.country == 'France']\n",
56+
"source_france = ColumnDataSource(dict(year=france_population.year, change=france_population.change))"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"**plot both line on figure**"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"\n",
73+
"TOOLTIPS = [\n",
74+
" (\"population:\", \"@change\")\n",
75+
"]\n",
76+
"\n",
77+
"r = figure(title=\"Line Plot comparing Population Change\", plot_height=450, tooltips=TOOLTIPS)\n",
78+
"\n",
79+
"r.line(x=\"year\", y=\"change\", source=source_uk, color='#1F78B4', legend='UK', line_color=\"red\", line_width=3)\n",
80+
"r.line(x=\"year\", y=\"change\", source=source_france, legend='France', line_color=\"black\", line_width=2)\n",
81+
"\n",
82+
"r.grid.grid_line_alpha=0.3\n",
83+
"\n",
84+
"show(r)"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": []
93+
}
94+
],
95+
"metadata": {
96+
"kernelspec": {
97+
"display_name": "Python 3",
98+
"language": "python",
99+
"name": "python3"
100+
},
101+
"language_info": {
102+
"codemirror_mode": {
103+
"name": "ipython",
104+
"version": 3
105+
},
106+
"file_extension": ".py",
107+
"mimetype": "text/x-python",
108+
"name": "python",
109+
"nbconvert_exporter": "python",
110+
"pygments_lexer": "ipython3",
111+
"version": "3.7.3"
112+
}
113+
},
114+
"nbformat": 4,
115+
"nbformat_minor": 2
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#step-1 download the sample data from library,\n",
10+
"# import bokeh.sampledata\n",
11+
"# bokeh.sampledata.download()"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from pathlib import Path\n",
21+
"DATA_PATH = Path('../datasets/chap5_data/')"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"#step-2 import required libraries\n",
31+
"import pandas as pd\n",
32+
"from bokeh.plotting import figure, output_notebook, show, ColumnDataSource\n",
33+
"from bokeh.io import push_notebook, show, output_notebook\n",
34+
"from ipywidgets import interact\n",
35+
"output_notebook()"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"# Step-3 initalize the figure\n",
45+
"TOOLTIPS = [\n",
46+
" (\"date\", \"@date\"),\n",
47+
" (\"value\", \"@close\")\n",
48+
"]\n",
49+
"p = figure(title=\"Interactive plot to change line width and color\", plot_width=900,\n",
50+
" plot_height=400, x_axis_type=\"datetime\", tooltips=TOOLTIPS)\n"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
59+
"# step-4 helper function to return dataframes.\n",
60+
"def prepare_data():\n",
61+
" microsoft_stock = pd.read_csv(DATA_PATH / \"microsoft_stock_ex6.csv\")\n",
62+
" microsoft_stock[\"date\"] = pd.to_datetime(microsoft_stock[\"date\"])\n",
63+
" google_stock = pd.read_csv(DATA_PATH / \"google_stock_ex6.csv\")\n",
64+
" google_stock[\"date\"] = pd.to_datetime(google_stock[\"date\"])\n",
65+
" \n",
66+
" return microsoft_stock, google_stock\n"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"# step-5 call the helper function to get the dataframes\n",
76+
"microsoft_stock, google_stock = prepare_data()"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"# step-6 Add the lines for both dataframes\n",
86+
"microsoft_line=p.line(\"date\",\"close\", source=microsoft_stock, line_width=1.5, legend=\"microsoft_stock\")\n",
87+
"google_line = p.line(\"date\", \"close\", source=google_stock, line_width=1.5, legend=\"google_stock\")"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"#custom function define how to interact for user event.\n",
97+
"def update(color, width=1):\n",
98+
" google_line.glyph.line_color = color\n",
99+
" google_line.glyph.line_width = width\n",
100+
" push_notebook()"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"#step-7 plot the required libraries\n",
110+
"interact(update, color=[\"red\", \"blue\", \"gray\"], width=(1,5))\n",
111+
"show(p, notebook_handle=True)"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": []
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": null,
124+
"metadata": {},
125+
"outputs": [],
126+
"source": []
127+
}
128+
],
129+
"metadata": {
130+
"kernelspec": {
131+
"display_name": "Python 3",
132+
"language": "python",
133+
"name": "python3"
134+
},
135+
"language_info": {
136+
"codemirror_mode": {
137+
"name": "ipython",
138+
"version": 3
139+
},
140+
"file_extension": ".py",
141+
"mimetype": "text/x-python",
142+
"name": "python",
143+
"nbconvert_exporter": "python",
144+
"pygments_lexer": "ipython3",
145+
"version": "3.7.3"
146+
}
147+
},
148+
"nbformat": 4,
149+
"nbformat_minor": 2
150+
}

0 commit comments

Comments
 (0)