diff --git a/content/code/python/initial-version-with-mean.py b/content/code/python/initial-version-with-mean.py deleted file mode 100644 index 558db6c..0000000 --- a/content/code/python/initial-version-with-mean.py +++ /dev/null @@ -1,47 +0,0 @@ -import pandas as pd -import matplotlib.pyplot as plt - - -# read data -data = pd.read_csv("weather_data.csv") - -# combine 'date' and 'time' into a single column 'recorded_at' as type datetime -data["recorded_at"] = pd.to_datetime(data["date"] + " " + data["time"]) - -# set 'recorded_at' as index for convenience -data = data.set_index("recorded_at") - -# keep only january data using datetime period indexing -january = data.loc["2024-01"] - -fig, ax = plt.subplots() - -# temperature time series -ax.plot( - january.index, - january["air_temperature_celsius"], - label="air temperature (C)", - color="red", -) - -values = january["air_temperature_celsius"].values -mean_temp = sum(values) / len(values) - -# mean temperature (as horizontal dashed line) -ax.axhline( - y=mean_temp, - label=f"mean air temperature (C): {mean_temp:.1f}", - color="red", - linestyle="--", -) - -ax.set_title("air temperature (C) at Helsinki airport") -ax.set_xlabel("date and time") -ax.set_ylabel("air temperature (C)") -ax.legend() -ax.grid(True) - -# format x-axis for better date display -fig.autofmt_xdate() - -fig.savefig("2024-01-temperature.png") diff --git a/content/code/python/initial-version-with-precipitation.py b/content/code/python/initial-version-with-precipitation.py deleted file mode 100644 index 97fad53..0000000 --- a/content/code/python/initial-version-with-precipitation.py +++ /dev/null @@ -1,68 +0,0 @@ -import pandas as pd -import matplotlib.pyplot as plt - - -# read data -data = pd.read_csv("weather_data.csv") - -# combine 'date' and 'time' into a single column 'recorded_at' as type datetime -data["recorded_at"] = pd.to_datetime(data["date"] + " " + data["time"]) - -# set 'recorded_at' as index for convenience -data = data.set_index("recorded_at") - -# keep only january data using datetime period indexing -january = data.loc["2024-01"] - -fig, ax = plt.subplots() - -# temperature time series -ax.plot( - january.index, - january["air_temperature_celsius"], - label="air temperature (C)", - color="red", -) - -values = january["air_temperature_celsius"].values -mean_temp = sum(values) / len(values) - -# mean temperature (as horizontal dashed line) -ax.axhline( - y=mean_temp, - label=f"mean air temperature (C): {mean_temp:.1f}", - color="red", - linestyle="--", -) - -ax.set_title("air temperature (C) at Helsinki airport") -ax.set_xlabel("date and time") -ax.set_ylabel("air temperature (C)") -ax.legend() -ax.grid(True) - -# format x-axis for better date display -fig.autofmt_xdate() - -fig.savefig("2024-01-temperature.png") - -fig, ax = plt.subplots() - -# precipitation time series -ax.plot( - january.index, - january["precipitation_mm"], - label="precipitation (mm)", - color="blue", -) - -ax.set_title("precipitation (mm) at Helsinki airport") -ax.set_xlabel("date and time") -ax.set_ylabel("precipitation (mm)") -ax.legend() -ax.grid(True) - -# format x-axis for better date display -fig.autofmt_xdate() - -fig.savefig("2024-01-precipitation.png") diff --git a/content/code/python/initial-version.py b/content/code/python/initial-version.py index 9f0f7c0..97fad53 100644 --- a/content/code/python/initial-version.py +++ b/content/code/python/initial-version.py @@ -24,6 +24,17 @@ color="red", ) +values = january["air_temperature_celsius"].values +mean_temp = sum(values) / len(values) + +# mean temperature (as horizontal dashed line) +ax.axhline( + y=mean_temp, + label=f"mean air temperature (C): {mean_temp:.1f}", + color="red", + linestyle="--", +) + ax.set_title("air temperature (C) at Helsinki airport") ax.set_xlabel("date and time") ax.set_ylabel("air temperature (C)") @@ -34,3 +45,24 @@ fig.autofmt_xdate() fig.savefig("2024-01-temperature.png") + +fig, ax = plt.subplots() + +# precipitation time series +ax.plot( + january.index, + january["precipitation_mm"], + label="precipitation (mm)", + color="blue", +) + +ax.set_title("precipitation (mm) at Helsinki airport") +ax.set_xlabel("date and time") +ax.set_ylabel("precipitation (mm)") +ax.legend() +ax.grid(True) + +# format x-axis for better date display +fig.autofmt_xdate() + +fig.savefig("2024-01-precipitation.png") diff --git a/content/solution.md b/content/solution.md index af10a11..4142ace 100644 --- a/content/solution.md +++ b/content/solution.md @@ -18,15 +18,13 @@ Learners can also explore some of these steps in one of the exercise sessions. ## Checklist - Start with notebook -- Add statistics (mean temperature) -- Add precipitation - Generalize from January to also February and March data - Abstract code into functions - Move from notebook to script - From functions with side-effects towards stateless functions - Initialize git - Add `requirements.txt` -- Add test +- Add test (optional) - Add command line interface - Show how a workflow solution could look - Split into multiple files/modules @@ -34,8 +32,10 @@ Learners can also explore some of these steps in one of the exercise sessions. ## Our initial version -We imagine that we assemble a working script/code -from various internet research/ AI chat +The initial version of our script for this exercise plots a series of +temperatures and precipitations for **January** as well as +the **mean temperature** averaged over the month. Suppose that we +assemble a working script from various internet research/AI chat recommendations and arrive at: :::::{tabs} @@ -55,47 +55,6 @@ recommendations and arrive at: - We test it out **in a notebook**. -## We add a dashed line representing the mean temperature - -This is still only the January data. - -:::::{tabs} - ::::{group-tab} Python - :::{literalinclude} code/python/initial-version-with-mean.py - :language: python - :emphasize-lines: 27-36 - ::: - :::: - - ::::{group-tab} R - Work in progress. You can - [help us](https://github.com/coderefinery/modular-type-along/issues/40) - by contributing or improving an R solution. - :::: -::::: - - -## We add another plot for the precipitation - -As a first go, we achieve this by copy pasting the existing code and adjusting -it for the precipitation column. - -:::::{tabs} - ::::{group-tab} Python - :::{literalinclude} code/python/initial-version-with-precipitation.py - :language: python - :emphasize-lines: 49-68 - ::: - :::: - - ::::{group-tab} R - Work in progress. You can - [help us](https://github.com/coderefinery/modular-type-along/issues/40) - by contributing or improving an R solution. - :::: -::::: - - ## Plotting also February and March data - Copy-pasting very similar code 6 times would be too complicated to maintain. diff --git a/content/starting-point.md b/content/starting-point.md index c7a8048..1310343 100644 --- a/content/starting-point.md +++ b/content/starting-point.md @@ -18,12 +18,14 @@ under the Creative Commons Attribution 4.0 International license (CC BY 4.0): ::: -## Our initial goal +## Our starting point + +Our starting point for this exercise is a script which plots a series of +temperatures and precipitations for **January** as well as +the **mean temperature** averaged over the month. Suppose that we +assemble a working script from various internet research/AI chat +recommendations and arrive at: -Our initial goal for this exercise is to plot a series of temperatures and -precipitations for **January** and to compute and plot the **mean temperature** -averaged over the month. We imagine that we assemble a working script from -various internet research/ AI chat recommendations and arrive at: :::::{tabs} ::::{group-tab} Python :::{literalinclude} code/python/initial-version.py @@ -44,7 +46,7 @@ languages in its place. For the Python experts: we will not see the most elegant Python. -## Further goals +## Goals - Once we get this working for **January**, our task changes to also plot the **February** and the **March** in two additional