The following commented code addresses all the points of the exercise
# Import the data
csv_file = '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()
and produces the following plot:
1.Code for a bar chart of average temperature per century:
# Import the data file
csv_file = '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()
which produces the plot:
2.Code for a histogram of the average annual temperature:
# 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()
which produces the plot:
3.Code to plot a scatter diagram of each year's February temperature plotted against that year's January temperature:
# 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()
which produces the plot: