Solutions

Another function

The following code produces the plot:

In [6]:
x = np.linspace(-5,5,1000)
y_tanh = np.tanh(x)
plt.plot(x, y_tanh, label='tanh(x)')
# Set title and legend, then show plot
plt.title('The hyperbolic tangent function')
plt.legend()
plt.xlabel('x')
plt.ylabel('f(x)')
plt.show()

Summer climate

The following commented code addresses all the points of the exercise

In [10]:
# Import the data 
csv_file = './data/cetml1659on.dat'
df = pd.read_csv(csv_file, # file name
                 skiprows=6,  # skip header
                 sep='\s+',  # whitespace separated
                 na_values=['-99.9', '-99.99']  # NaNs
                )
# Plot the January and June values
df['JAN'].plot()
df['JUN'].plot()
# Add a title and axes labels
plt.title('Summer and Winter Climate Plots')
plt.xlabel('Year')
plt.ylabel('Temperature ($^\circ$C)')
# Add a legend
plt.legend()
plt.show()

Other graphs

1.Code for a bar chart of average temperature per century:

In [16]:
# Import the data file
csv_file = './data/cetml1659on.dat'
df = pd.read_csv(csv_file, # file name
                 skiprows=6,  # skip header
                 sep='\s+',  # whitespace separated
                 na_values=['-99.9', '-99.99']  # NaNs
                )
# Add century column
years = Series(df.index, index=df.index).apply(str)
century = years.apply(lambda x: x[:2]+'00')
df['century'] = century
# Group data by century
by_century = df.groupby('century')
agg = by_century.aggregate(np.mean)
# Plot bar chart
agg.YEAR.plot.bar()
# Label axes
plt.title('Average temperature per century')
plt.xlabel('Century')
plt.ylabel('Temperature ($^\circ$C)')
plt.show()

2.Code for a histogram of the average annual temperature:

In [17]:
# Assume we have already imported the data as df
# Look at the year column of our data frame and plot histogram
df['YEAR'].hist()
# Label axes
plt.title('Histogram of average annual temperatures')
plt.xlabel('Temperature ($^\circ$C)')
plt.ylabel('Frequency')
plt.show()

3.Code to plot a scatter diagram of each year's February temperature plotted against that year's January temperature:

In [18]:
# Assume we have already imported the data as df
# Plot the data frame's February values against its January values
df.plot.scatter(x='JAN', y='FEB')
# Label axes
plt.title('Scatter diagram of February temperature against January temperature')
plt.xlabel(r'Temperature in January($^\circ$C)')
plt.ylabel(r'Temperature in February($^\circ$C)')
plt.show()