Skip to content

Commit d347fdb

Browse files
Merge pull request #27 from TeachBooks/draft_book
Draft book
2 parents 0c0d771 + c6fc37e commit d347fdb

File tree

5 files changed

+58
-53
lines changed

5 files changed

+58
-53
lines changed

book/_toc.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ parts:
5252
# - file: notebook_5_figures/PythonNotebook5_matplotlib.ipynb
5353
- file: exercise_notebooks/exercise_notebook_5_figures.ipynb
5454

55-
# - file: notebook_6_numpy/PythonNotebook6_first_page.ipynb
56-
# sections:
57-
# - file: notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb
58-
# - file: notebook_6_numpy/PythonNotebook6_loading_data.ipynb
59-
# - file: exercise_notebooks/exercise_notebook_6_numpy.ipynb
55+
- file: notebook_6_numpy/PythonNotebook6_first_page.ipynb
56+
sections:
57+
- file: notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb
58+
- file: notebook_6_numpy/PythonNotebook6_loading_data.ipynb
59+
- file: exercise_notebooks/exercise_notebook_6_numpy.ipynb
6060

6161
# - file: notebook_7_files/PythonNotebook7_first_page.ipynb
6262
# sections:

book/exercise_notebooks/exercise_notebook_6_numpy.ipynb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@
5555
"\n",
5656
"You can do this by shifting the x-coordinates of your bars, and setting the bar width using the `width=` argument to the bar graph function.\n",
5757
"\n",
58-
"* copy your plotting code from the last notebook. \n",
59-
"* make a numpy array of the month list.\n",
60-
"* now you can easily add or subtract a number from the whole array.\n",
61-
"* make a plot where the bars for the different regions don't overlap.\n",
58+
"Let's see how it works...\n",
59+
"\n",
60+
"* first copy your plotting code from the last exercise notebook. \n",
61+
"* use the `width=` argument of the <code>plt.bar</code> function to change the width of the bars.\n",
62+
"* now make a numpy array of the month list.\n",
63+
"* now you can easily add or subtract a single number from this array, and in this way you can shift the x-values for the different countries a little to the left or right when plotting the bars for that country.\n",
64+
"\n",
65+
"Try out: make a plot where the bars for the different regions don't overlap.\n",
6266
"\n",
6367
"</div></div>"
6468
]
@@ -78,18 +82,17 @@
7882
"source": [
7983
"<div class=\"alert alert-block alert-info\"><b>Exercise 6.3</b><br><br><div style=\"text-align: justify\">\n",
8084
"\n",
81-
"* Create a 4x2 matrix `m` filled with different values \n",
85+
"In linear algebra you will learn about matrices, which are basically 2D arrays (a vector being the 1D equivalent): an $m\\times n$ matrix has $m$ rows and $n$ columns.\n",
8286
"\n",
83-
"4x2 means four rows and two columns.\n",
84-
"You can use `np.array()`, and pass it nested lists,\n",
85-
"or you can use `np.zeros((rows, columns))` and fill in the values afterwards.\n",
86-
"Here `rows` and `columns` are the number of rows and columns you want. Note the double parentheses. The reason is that `(rows, columns)` is a tuple,\n",
87-
"and the `np.zeros()` function takes this tuple as it's argument.\n",
87+
"* Create a 4x2 matrix `M` filled with different values you may choose yourself\n",
8888
"\n",
89+
"You can use `np.array()`, and pass it nested lists (lists within a list),\n",
90+
"or you can use `M = np.zeros((rows, columns))` and fill in the values afterwards by assigning values to the individual elements.\n",
91+
"Here `rows` and `columns` are the number of rows and columns you want. Note the double parentheses. The reason is that `(rows, columns)` is a tuple, and the `np.zeros()` function takes this tuple as it's argument to create a matrix filled with zeros of the required dimensions.\n",
8992
"\n",
90-
"* Print the shape of your matrix.\n",
93+
"* Check the shape of your matrix, using `M.shape`\n",
9194
"\n",
92-
"* Try the `transpose()` function on your array (hint: `m.transpose()`). What did it do?\n",
95+
"* Try the transpose() function on your array (hint: `M.transpose()`). What did it do?\n",
9396
"\n",
9497
"</div></div>"
9598
]
@@ -142,18 +145,18 @@
142145
"source": [
143146
"<div class=\"alert alert-block alert-info\"><b>Exercise 6.5 Plot the RICO input data</b><br><br><div style=\"text-align: justify\">\n",
144147
"\n",
145-
"* split the matrix into separate variables for each column \n",
148+
"* load the data\n",
149+
"* create a separate array (with their own name) for each column \n",
146150
"* use subplots to plot several quantities side by side:\n",
147151
" ```\n",
148152
" fig, axes = plt.subplots(nrows=1, ncols=5, sharey=True, figsize=(13,4))\n",
149153
" ```\n",
154+
"* `figsize=(13,4)` to set the figure size in inches (width, height) at some assumed dots-per-inch value. It's included here to make the plot a bit wider, so that the labels don't overlap. Change the values a bit to see what happens.\n",
150155
"* to plot in the first subplot, use `axes[0].plot(...)`\n",
151156
"* to set x and y labels, use `axes[0].set_ylabel(...)`\n",
152-
"* use the y axis of the plot for the hight above ground\n",
153-
" \n",
154-
" \n",
155-
"* `sharey=True` makes the plots share the y axis\n",
156-
"* `figsize=(13,4)` sets the figure size in inches (width, height) at some assumed dots-per-inch value. It's included here to make the plot a bit wider, so that the labels don't overlap. \n",
157+
"* use the y-axis of the plot for the height above ground, in this way it is the same for all subplots \n",
158+
"* ... and that's why we used `sharey=True` we tell matplotlib that the y-axis is to be shared\n",
159+
"\n",
157160
"\n",
158161
"</div></div>"
159162
]
@@ -170,7 +173,7 @@
170173
],
171174
"metadata": {
172175
"kernelspec": {
173-
"display_name": "Python 3 (ipykernel)",
176+
"display_name": "base",
174177
"language": "python",
175178
"name": "python3"
176179
},
@@ -184,7 +187,7 @@
184187
"name": "python",
185188
"nbconvert_exporter": "python",
186189
"pygments_lexer": "ipython3",
187-
"version": "3.10.0"
190+
"version": "3.13.2"
188191
},
189192
"latex_envs": {
190193
"LaTeX_envs_menu_present": true,
@@ -203,11 +206,6 @@
203206
"latex_user_defs": false,
204207
"report_style_numbering": false,
205208
"user_envs_cfg": false
206-
},
207-
"vscode": {
208-
"interpreter": {
209-
"hash": "1fe2f2b718b1108b9c4176932db8a0ead471245140baaa21ea96a4066683e6b2"
210-
}
211209
}
212210
},
213211
"nbformat": 4,
-85 Bytes
Binary file not shown.

book/notebook_6_numpy/PythonNotebook6_loading_data.ipynb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@
3434
{
3535
"cell_type": "code",
3636
"execution_count": null,
37-
"metadata": {
38-
"tags": [
39-
"theme-remove-input-init"
40-
]
41-
},
37+
"metadata": {},
4238
"outputs": [],
4339
"source": [
4440
"import numpy as np\n",
4541
"from pathlib import Path\n",
4642
"\n",
47-
"rico_dataset_path = Path.cwd().parent / \"data\" / \"rico.txt\"\n",
43+
"rico_dataset_path = Path.cwd().parent / \"data/rico.txt\"\n",
4844
"\n",
4945
"rico = np.loadtxt(rico_dataset_path)"
5046
]
@@ -53,6 +49,8 @@
5349
"cell_type": "markdown",
5450
"metadata": {},
5551
"source": [
52+
"Note that the file in this case is save in a folder <code>data</code> within the parent directory.\n",
53+
"\n",
5654
"What did we just load?"
5755
]
5856
},
@@ -62,8 +60,8 @@
6260
"metadata": {},
6361
"outputs": [],
6462
"source": [
65-
"print(type(rico))\n",
66-
"print(rico.shape)"
63+
"print('The type of rico is:', type(rico))\n",
64+
"print('The number of rows and columns is equal to:',rico.shape)"
6765
]
6866
},
6967
{
@@ -95,11 +93,11 @@
9593
"* look at the top of the file. You will see two lines starting with `#` followed by lines of numbers.\n",
9694
"\n",
9795
"* lines starting with `#` are considered as comments, and are ignored by Numpy.\n",
98-
"Those lines describe the columns of the file.\n",
96+
"Those lines describe what is in the columns of the file.\n",
9997
"\n",
10098
"* the following lines with numbers are loaded into the numpy array. You can compare the first few rows from the file with the array content you printed above.\n",
10199
"\n",
102-
"As you probably know, numbers like `2.0000e+01` are written in scientific notation, this one means `2 * 10**1 = 20`\n"
100+
"As you probably know, numbers like `2.0000e+01` are written in scientific notation, this one means $2 \\times 10^1 = 20$\n"
103101
]
104102
},
105103
{
@@ -142,9 +140,9 @@
142140
"source": [
143141
"## 6.2.1 Surface and color plots\n",
144142
"\n",
145-
"Next you will learn some more plotting with matplotlib, namely to plot a function of two variables, f(x,y). Either as a 2D image with colors showing the value, or as a surface in 3D where z = f(x,y). \n",
143+
"Next you will learn some more plotting with matplotlib, namely to plot a function of two variables, $f(x,y)$. Either as a 2D image with colors showing the value, or as a surface in 3D where $z = f(x,y)$. \n",
146144
"\n",
147-
"First try the folloring example showing `np.meshgrid()`. Meshgrid is used to create 2D arrays X and Y, where X contains an x-coordinate and Y contains an y-coordinate."
145+
"First try the folloring example showing `np.meshgrid()`. Meshgrid is used to create 2D arrays <code>X</code> and <code>Y</code>, with x- and y-coordinates, respectively."
148146
]
149147
},
150148
{
@@ -252,7 +250,7 @@
252250
"cell_type": "markdown",
253251
"metadata": {},
254252
"source": [
255-
"#### After this Notebook you should be able to:\n",
253+
"#### After this Chapter you should be able to:\n",
256254
"\n",
257255
"- know the basics of how to use `numpy`\n",
258256
"- be able to import and perform calculations with datasets\n",
@@ -263,7 +261,7 @@
263261
],
264262
"metadata": {
265263
"kernelspec": {
266-
"display_name": "Python 3 (ipykernel)",
264+
"display_name": "base",
267265
"language": "python",
268266
"name": "python3"
269267
},
@@ -277,7 +275,7 @@
277275
"name": "python",
278276
"nbconvert_exporter": "python",
279277
"pygments_lexer": "ipython3",
280-
"version": "3.10.0"
278+
"version": "3.13.2"
281279
},
282280
"latex_envs": {
283281
"LaTeX_envs_menu_present": true,

book/notebook_6_numpy/PythonNotebook6_vectors_and_plotting_with_numpy.ipynb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"source": [
4343
"Now functions in numpy are accessible as `np.array()` for example.\n",
4444
"\n",
45-
"The part `as np` is not necessary, but commonly done for slightly shorter code than typing `numpy.array()` every time (just like we imported `matplotlib.pyplot` as `plt` last week).\n"
45+
"The part `as np` is not necessary, but commonly done for slightly shorter code than typing `numpy.array()` every time (just like we imported `matplotlib.pyplot` as `plt` before).\n"
4646
]
4747
},
4848
{
@@ -219,7 +219,12 @@
219219
"```{admonition} Attention\n",
220220
":class: danger\n",
221221
"\n",
222-
"Choose `N` carefully. `np.arange(0, 1, 0.1)` yields an array with 10 numbers: `[0, 0.1, 0.2, ..., 0.9]`. `np.linspace(0, 1, 10)` also yields an array with 10 numbers, but different ones: `[0, 0.1111111, 0.22222222, ..., 0.88888889, 1]`. This is the difference between including and excluding the end point. \n",
222+
"Note the differences! \n",
223+
"\n",
224+
"With `np.arange(0, 1, 0.1)` you create an array starting at 0 with step size 0.1 (defined by the last number); the end point is not included: in this case the array has 10 elements `[0, 0.1, 0.2, ..., 0.9]`. \n",
225+
"\n",
226+
"With `np.linspace(0, 1, 10)` you create an array with 10 elements (defined by the last number), but since in this case the end point is included, it yields `[0, 0.1111111, 0.22222222, ..., 0.88888889, 1]`. \n",
227+
"\n",
223228
"+++\n",
224229
"```"
225230
]
@@ -245,7 +250,7 @@
245250
"cell_type": "markdown",
246251
"metadata": {},
247252
"source": [
248-
"Note we used `np.cos()`, which is similar to `math.cos()` but can work on numpy arrays.\n",
253+
"Note we used `np.cos()`, which is similar to `math.cos()` but allows to use a numpy array as input, while the `math` equivalent can only work with single variables (scalars).\n",
249254
"\n",
250255
"```{admonition} Attention\n",
251256
":class: danger\n",
@@ -287,7 +292,9 @@
287292
"outputs": [],
288293
"source": [
289294
"import matplotlib.pyplot as plt\n",
290-
"plt.plot(x, y1)"
295+
"plt.plot(x, y1,'.')\n",
296+
"plt.xlabel('x')\n",
297+
"plt.ylabel('cos(x)') "
291298
]
292299
},
293300
{
@@ -305,7 +312,9 @@
305312
"metadata": {},
306313
"outputs": [],
307314
"source": [
308-
"plt.plot(x, y1)\n",
315+
"plt.plot(x, y1,'.-')\n",
316+
"plt.xlabel('x')\n",
317+
"plt.ylabel('cos(x)') \n",
309318
"plt.savefig('cosplot.png')"
310319
]
311320
},
@@ -315,7 +324,7 @@
315324
"source": [
316325
"Matplotlib will now save your plot in the file you specified. The file will be placed in the same folder / directory as your notebook. You can also specify the directory that you would want the image to be saved: check out the documentation of the `savefig` function [here](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html). \n",
317326
"\n",
318-
"*Note that in this interactive environment, saving the file won't work---this is when you're working locally.*\n",
327+
"*Note that in this interactive online environment, saving the file won't work.*\n",
319328
"\n",
320329
"Matplotlib can handle several formats: .png (an image), .pdf (vector graphics, small files and often looks good), .svg (also vector graphics, can be edited in for example Inkscape)\n",
321330
"\n",
@@ -325,7 +334,7 @@
325334
],
326335
"metadata": {
327336
"kernelspec": {
328-
"display_name": "Python 3 (ipykernel)",
337+
"display_name": "base",
329338
"language": "python",
330339
"name": "python3"
331340
},
@@ -339,7 +348,7 @@
339348
"name": "python",
340349
"nbconvert_exporter": "python",
341350
"pygments_lexer": "ipython3",
342-
"version": "3.10.0"
351+
"version": "3.13.2"
343352
},
344353
"latex_envs": {
345354
"LaTeX_envs_menu_present": true,

0 commit comments

Comments
 (0)