Import all necessary libraries:
Import the climate data files using pandas:
Clip the data to the year 2018:
Resample temperature values to monthly averages:
Create the plotting function:
def create_climate_diagram(df, temp_col, prec_col, title, filename, temp_min=-15, temp_max=20, prec_min=0, prec_max=370):
"""
Draw a climate diagram.
Parameters
----------
df : pd.DataFrame
Dataframe with values to plot from
temp_col : str
Name of temperature column
prec_col : str
Name of precipitation column
title : String
The title for the figure
filename : String
The name of the output figure
temp_min : Number
The minimum temperature value to display
temp_max : Number
The maximum temperature value to display
prec_min : Number
The minimum precipitation value to display
prec_max : Number
The maximum precipitation value to display
Returns
-------
The figure
"""
fig = plt.figure(figsize=(10,8))
plt.rcParams["font.size"] = 16
ax2 = fig.add_subplot(111)
ax1 = ax2.twinx()
ax2.bar(df.index.strftime("%b"), df.loc[:, prec_col].values, width=0.8, color="b")
ax1.plot(df.loc[:, temp_col].values, c="r")
ax2.set_ylabel("Precipitation (mm)")
ax1.set_ylabel("Temperature (°C)")
ax1.set_ylim((temp_min,temp_max))
ax2.set_ylim((prec_min,prec_max))
plt.title(title)
plt.savefig(filename)
return fig