Skip to content

Commit 0f038a1

Browse files
committed
updated gitignore and added empty notebook 1
1 parent 0526ee3 commit 0f038a1

File tree

3 files changed

+148
-19
lines changed

3 files changed

+148
-19
lines changed

.DS_Store

2 KB
Binary file not shown.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ ENV/
8787
.spyderproject
8888

8989
# Rope project settings
90-
.ropeproject
90+
.ropeproject
91+
92+
.DS_Store

notebook1_basics_plotting/py_exploratory_comp_1.ipynb

Lines changed: 145 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"metadata": {},
3636
"source": [
3737
"Note that the extra spaces are added to make the code more readable. \n",
38-
"`2 * 3` works just as well as `2*3`. But is it highly recommended to use the additional spaces (in fact, it is considered good style to do so).\n",
38+
"`2 * 3` works just as well as `2*3`. And it is considered good style. Use the extra spaces in all your Notebooks.\n",
39+
"\n",
3940
"When you are programming, you want to store your values in variables"
4041
]
4142
},
@@ -106,8 +107,8 @@
106107
"cell_type": "markdown",
107108
"metadata": {},
108109
"source": [
109-
"### <a name=\"ex1\"></a> Exercise 1, First Python code\n",
110-
"Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2$ using $a=1$, $b=1$, $c=-6$."
110+
"### <a name=\"ex1a\"></a> Exercise 1a, First Python code\n",
111+
"Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen."
111112
]
112113
},
113114
{
@@ -121,7 +122,7 @@
121122
"cell_type": "markdown",
122123
"metadata": {},
123124
"source": [
124-
"<a href=\"#ex1answer\">Answer to Exercise 1</a>"
125+
"<a href=\"#ex1aanswer\">Answer to Exercise 1a</a>"
125126
]
126127
},
127128
{
@@ -145,15 +146,67 @@
145146
"cell_type": "markdown",
146147
"metadata": {},
147148
"source": [
148-
"(Note for Python 2 users: `1/3` gives zero in Python 2, as the division of two integers returns an integer. Use `1.0/3` instead."
149+
"(Note for Python 2 users: `1/3` gives zero in Python 2, as the division of two integers returned an integer in Python 2). The above print statement looks pretty ugly with 16 values of 3 in a row. A better and more readable way to print both text and the value of variable to the screen is to use what are called f-strings. f-strings allow you to insert the value of a variable anywhere in the text by surrounding it with braces `{}`. The entire text string needs to be between quotes and be preceded by the letter `f`"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"a = 1\n",
159+
"b = 3\n",
160+
"c = a / b\n",
161+
"print(f'{a} divided by {b} gives {c}')"
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"The complete syntax between braces is `{variable:width.precision}`. When `width` and `precision` are not specified, Python will use all digits and figure out the width for you. If you want a floating point number with 3 decimals, you specify the number of digits (`3`) followed by the letter `f` for floating point (you can still let Python figure out the width by not specifying it). If you prefer exponent (scientific) notation, replace the `f` by an `e`. The text after the `#` is a comment in the code. Any text on the line after the `#` is ignored by Python. "
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"metadata": {},
175+
"outputs": [],
176+
"source": [
177+
"print(f'{a} divided by {b} gives {c:.3f}') # three decimal places\n",
178+
"print(f'{a} divided by {b} gives {c:10.3f}') # width 10 and three decimal places\n",
179+
"print(f'{a} divided by {b} gives {c:.3e}') # three decimal places scientific notation"
180+
]
181+
},
182+
{
183+
"cell_type": "markdown",
184+
"metadata": {},
185+
"source": [
186+
"### <a name=\"ex1b\"></a> Exercise 1b, First Python code using f-strings\n",
187+
"Compute the value of the polynomial $y=ax^2+bx+c$ at $x=-2$, $x=0$, and $x=2.1$ using $a=1$, $b=1$, $c=-6$ and print the results to the screen using f-strings and 2 decimal places."
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"metadata": {},
194+
"outputs": [],
195+
"source": []
196+
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {},
200+
"source": [
201+
"<a href=\"#ex1banswer\">Answer to Exercise 1b</a>"
149202
]
150203
},
151204
{
152205
"cell_type": "markdown",
153206
"metadata": {},
154207
"source": [
155208
"### More on variables\n",
156-
"Once you have created a variable in a Python session, it will remain in memory, so you can use it in other cells as well. For example, the variable `a`, which was defined earlier in this Notebook, still exist. It will be `6` unless you changed it in Exercise 1. "
209+
"Once you have created a variable in a Python session, it will remain in memory, so you can use it in other cells as well. For example, the variables `a` and `b`, which were defined two code cells above in this Notebook, still exist. "
157210
]
158211
},
159212
{
@@ -162,7 +215,8 @@
162215
"metadata": {},
163216
"outputs": [],
164217
"source": [
165-
"print('a:', a)"
218+
"print(f'the value of a is: {a}')\n",
219+
"print(f'the value of b is: {b}')"
166220
]
167221
},
168222
{
@@ -212,7 +266,9 @@
212266
"cell_type": "markdown",
213267
"metadata": {},
214268
"source": [
215-
"Let's try to plot $y$ vs $x$ for $x$ going from $-4$ to $+4$ for the polynomial in the exercise above. To do that, we need to evaluate $y$ at a bunch of points. A sequence of values of the same type is called an array (for example an array of integers or floats). Array functionality is available in the package `numpy`. Let's import `numpy` and call it `np`, so that any function in the `numpy` package may be called as `np.function`. "
269+
"Let's try to plot $y$ vs $x$ for $x$ going from $-4$ to $+4$ for the polynomial\n",
270+
"$y=ax^2+bx+c$ with $a=1$, $b=1$, $c=-6$.\n",
271+
"To do that, we need to evaluate $y$ at a bunch of points. A sequence of values of the same type is called an array (for example an array of integers or floats). Array functionality is available in the package `numpy`. Let's import `numpy` and call it `np`, so that any function in the `numpy` package may be called as `np.function`. "
216272
]
217273
},
218274
{
@@ -272,7 +328,7 @@
272328
"cell_type": "markdown",
273329
"metadata": {},
274330
"source": [
275-
"Note that *one hundred* `y` values are computed in the simple line `y = a * x ** 2 + b * x + c`. The text after the `#` is a comment in the code. Any text on the line after the `#` is ignored by Python. Python treats arrays in the same fashion as it treats regular variables when you perform mathematical operations. The math is simply applied to every value in the array (and it runs much faster than when you would do every calculation separately). \n",
331+
"Note that *one hundred* `y` values are computed in the simple line `y = a * x ** 2 + b * x + c`. Python treats arrays in the same fashion as it treats regular variables when you perform mathematical operations. The math is simply applied to every value in the array (and it runs much faster than when you would do every calculation separately). \n",
276332
"\n",
277333
"You may wonder what the statement `[<matplotlib.lines.Line2D at 0x30990b0>]` is (the numbers on your machine may look different). This is actually a handle to the line that is created with the last command in the code block (in this case `plt.plot(x, y)`). Remember: the result of the last line in a code cell is printed to the screen, unless it is stored in a variable. You can tell the Notebook not to print this to the screen by putting a semicolon after the last command in the code block (so type `plot(x, y);`). We will learn later on that it may also be useful to store this handle in a variable."
278334
]
@@ -281,7 +337,7 @@
281337
"cell_type": "markdown",
282338
"metadata": {},
283339
"source": [
284-
"The `plot` function can take many arguments. Looking at the help box of the `plot` function (by typing `plt.plot(` and then shift-tab) gives you a lot of help. Typing `plt.plot?` gives a new scrollable subwindow at the bottom of the notebook, showing the documentation on `plot`. Click the x in the upper right hand corner to close the subwindow again."
340+
"The `plot` function can take many arguments. Looking at the help box of the `plot` function, by typing `plt.plot(` and then shift-tab, gives you a lot of help. Typing `plt.plot?` gives a new scrollable subwindow at the bottom of the notebook, showing the documentation on `plot`. Click the x in the upper right hand corner to close the subwindow again."
285341
]
286342
},
287343
{
@@ -379,7 +435,7 @@
379435
"metadata": {},
380436
"source": [
381437
"### <a name=\"ex4\"></a> Exercise 4, Subplots and fancy tick markers\n",
382-
"Load the average monthly air temperature and seawater temperature for Holland. Create one plot with two graphs above each other using the subplot command (use `plt.subplot?`). On the top graph, plot the air and sea temperature. Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than 0,1,2,etc. Use `plt.xticks?` to find out how. In the bottom graph, plot the difference between the air and seawater temperature. Add legends, axes labels, the whole shebang."
438+
"Load the average monthly air temperature and seawater temperature for Holland. Create one plot with two graphs above each other using the subplot command (use `plt.subplot?` to find out how). On the top graph, plot the air and sea temperature. Label the ticks on the horizontal axis as 'jan', 'feb', 'mar', etc., rather than 0,1,2,etc. Use `plt.xticks?` to find out how. In the bottom graph, plot the difference between the air and seawater temperature. Add legends, axes labels, the whole shebang."
383439
]
384440
},
385441
{
@@ -401,7 +457,48 @@
401457
"metadata": {},
402458
"source": [
403459
"### Colors\n",
404-
"There are five different ways to specify colors in matplotlib plotting; you may read about it [here](http://matplotlib.org/examples/pylab_examples/color_demo.html). A useful way is to use the html color names. The html codes may be found, for example, [here](http://en.wikipedia.org/wiki/Web_colors). But the coolest way is probably to use the xkcd names, which need to be prefaced by the `xkcd:`. The xkcd list of color names is given by [xkcd](https://xkcd.com/color/rgb/) and includes favorites such as 'baby puke green' and a number of brown colors vary from `poo` to `poop brown` and `baby poop brown`. Try it out:"
460+
"If you don't specify a color for a plotting statement, `matplotlib` will use its default colors. The first three default colors are special shades of blue, orange and green. The names of the default colors are a capital `C` followed by the number, starting with number `0`. For example"
461+
]
462+
},
463+
{
464+
"cell_type": "code",
465+
"execution_count": null,
466+
"metadata": {},
467+
"outputs": [],
468+
"source": [
469+
"plt.plot([0, 1], [0, 1], 'C0')\n",
470+
"plt.plot([0, 1], [1, 2], 'C1')\n",
471+
"plt.plot([0, 1], [2, 3], 'C2')\n",
472+
"plt.legend(['default blue', 'default orange', 'default green']);"
473+
]
474+
},
475+
{
476+
"cell_type": "markdown",
477+
"metadata": {},
478+
"source": [
479+
"There are five different ways to specify your own colors in matplotlib plotting; you may read about them [here](http://matplotlib.org/examples/pylab_examples/color_demo.html). A useful way is to use the html color names. The html codes may be found, for example, [here](http://en.wikipedia.org/wiki/Web_colors). "
480+
]
481+
},
482+
{
483+
"cell_type": "code",
484+
"execution_count": null,
485+
"metadata": {},
486+
"outputs": [],
487+
"source": [
488+
"color1 = 'fuchsia'\n",
489+
"color2 = 'lime'\n",
490+
"color3 = 'DodgerBlue'\n",
491+
"plt.plot([0, 1], [0, 1], color1)\n",
492+
"plt.plot([0, 1], [1, 2], color2)\n",
493+
"plt.plot([0, 1], [2, 3], color3)\n",
494+
"plt.legend([color1, color2, color3]);"
495+
]
496+
},
497+
{
498+
"cell_type": "markdown",
499+
"metadata": {},
500+
"source": [
501+
"The coolest (and nerdiest) way is probably to use the xkcd names, which need to be prefaced by the `xkcd:`. The xkcd list of color names is given by [xkcd](https://xkcd.com/color/rgb/) and includes favorites such as 'baby puke green' and a number of brown colors vary from `poo` to `poop brown` and `baby poop brown`. Try it out:"
405502
]
406503
},
407504
{
@@ -410,7 +507,8 @@
410507
"metadata": {},
411508
"outputs": [],
412509
"source": [
413-
"plt.plot([1, 2, 3], [4, 5, 2], 'xkcd:baby puke green');"
510+
"plt.plot([1, 2, 3], [4, 5, 2], 'xkcd:baby puke green');\n",
511+
"plt.title('xkcd color baby puke green');"
414512
]
415513
},
416514
{
@@ -448,7 +546,7 @@
448546
"metadata": {},
449547
"source": [
450548
"### <a name=\"ex6\"></a> Exercise 6, Fill between\n",
451-
"Load the air and sea temperature, as used in Exercise 4, but this time make one plot of temperature vs the number of the month and use the `plt.fill_between` command to fill the space between the curve and the $x$-axis. Specify the `alpha` keyword, which defines the transparancy. Some experimentation will give you a good value for alpha (stay between 0 and 1). Note that you need to specify the color using the `color` keyword argument."
549+
"Load the air and sea temperature, as used in Exercise 4, but this time make one plot of temperature vs the number of the month and use the `plt.fill_between` command to fill the space between the curve and the horizontal axis. Specify the `alpha` keyword, which defines the transparancy. Some experimentation will give you a good value for alpha (stay between 0 and 1). Note that you need to specify the color using the `color` keyword argument."
452550
]
453551
},
454552
{
@@ -476,7 +574,7 @@
476574
"cell_type": "markdown",
477575
"metadata": {},
478576
"source": [
479-
"<a name=\"ex1answer\">Answer to Exercise 1</a>"
577+
"<a name=\"ex1aanswer\">Answer to Exercise 1</a>"
480578
]
481579
},
482580
{
@@ -494,7 +592,7 @@
494592
"x = 0 \n",
495593
"y = a * x ** 2 + b * x + c\n",
496594
"print('y evaluated at x = 0 is', y)\n",
497-
"x = 2\n",
595+
"x = 2.1\n",
498596
"y = a * x ** 2 + b * x + c\n",
499597
"print('y evaluated at x = 2 is', y)"
500598
]
@@ -503,7 +601,36 @@
503601
"cell_type": "markdown",
504602
"metadata": {},
505603
"source": [
506-
"<a href=\"#ex1\">Back to Exercise 1</a>\n",
604+
"<a href=\"#ex1a\">Back to Exercise 1a</a>\n",
605+
"\n",
606+
"<a name=\"ex1banswer\">Answer to Exercise 1b</a>"
607+
]
608+
},
609+
{
610+
"cell_type": "code",
611+
"execution_count": null,
612+
"metadata": {},
613+
"outputs": [],
614+
"source": [
615+
"a = 1\n",
616+
"b = 1\n",
617+
"c = -6\n",
618+
"x = -2\n",
619+
"y = a * x ** 2 + b * x + c\n",
620+
"print(f'y evaluated at x = {x} is {y}')\n",
621+
"x = 0 \n",
622+
"y = a * x ** 2 + b * x + c\n",
623+
"print(f'y evaluated at x = {x} is {y}')\n",
624+
"x = 2.1\n",
625+
"y = a * x ** 2 + b * x + c\n",
626+
"print(f'y evaluated at x = {x} is {y:.2f}')"
627+
]
628+
},
629+
{
630+
"cell_type": "markdown",
631+
"metadata": {},
632+
"source": [
633+
"<a href=\"#ex1b\">Back to Exercise 1b</a>\n",
507634
"\n",
508635
"<a name=\"ex2answer\">Answer to Exercise 2</a>"
509636
]
@@ -655,7 +782,7 @@
655782
"name": "python",
656783
"nbconvert_exporter": "python",
657784
"pygments_lexer": "ipython3",
658-
"version": "3.6.5"
785+
"version": "3.7.0"
659786
},
660787
"varInspector": {
661788
"cols": {

0 commit comments

Comments
 (0)