Exercise 1


In [ ]:
# First of all, lets read some data from a CSV file. 
#"Figure 1. Average Global Sea Surface Temperature, 1880-2015",,,
#Source: EPA's Climate Change Indicators in the United States: www.epa.gov/climate-indicators,,,"Data source: NOAA, 2016",,,
# Units: temperature anomaly (°F),,,
# https://www.epa.gov/sites/production/files/2016-08/sea-surface-temp_fig-1.csv 

# Import our libraries
import csv
import matplotlib.pyplot as plt
import os
import numpy as np
%matplotlib inline

In [ ]:
file_name = os.path.join(os.path.pardir, 'data', 'sea-surface-temp_fig-1.csv')

In [ ]:
# Header is 6 lines and we are going to read in the data
skip_rows = 6

# Read 'file_name' and call it 'data' 
# This is a simple .csv file. Go into the folder 'Data' and open it in Excel if you don't believe us! 
data = np.genfromtxt(file_name, delimiter=',', skip_header=skip_rows, names=True,
                     dtype=None)

In [ ]:
# Name the data
years = data['Year']
temperature = ((data['Annual_anomaly'] - 32)*5)/9

In [ ]:
### Time to create our plot and give it a red colour because it is alarming!
fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(years, temperature, c='red')

### How about adding some labels? 
ax.set_xlabel('Year')
ax.set_ylabel('Temperature ($^\circ C$)')
ax.set_title('Annual Anomaly')

In [ ]:
# fig.savefig('awesome_figure.png', dpi=300, bbox_to_inches='tight')

Exercise 2


In [ ]:
file_name = os.path.join(os.path.pardir, 'data', 'Shelf_sea_nuts.csv')
# Header is 6 lines and we are going to read in the data
skip_rows = 1

In [ ]:
# Read 'file_name' and call it 'data' 
# This is a simple .csv file. Go into the folder 'Data' and open it in Excel if you don't believe us! 
data = np.genfromtxt(file_name, delimiter=',', names=True, dtype=None)
print(data.dtype) # Hint #

In [ ]:
# Name the data based on the column names
ICES_year = data['Year']
ICES_temperature = data['TEMP']
ICES_Oxygen = data['O2']
ICES_Nitrate = data['NO3']

In [ ]:
fig = plt.figure(figsize=(8, 15))
ax = fig.add_subplot(311)
ax.scatter(ICES_year, ICES_temperature, color='red', alpha=0.1) 
ax.set_xlabel('Year')
ax.set_ylabel('Temperarure (oC)')
ax.set_title('Temperature on the EU Shelf')

ax = fig.add_subplot(312)
ax.scatter(ICES_year, ICES_Nitrate, color='green', alpha=0.1) 
ax.set_xlabel('Year')
ax.set_ylabel('Nitrate (umol/l)')
ax.set_title('Nitrate on the EU Shelf')

ax = fig.add_subplot(313)
ax.scatter(ICES_year, ICES_Oxygen, color='blue', alpha=0.1) 
ax.set_xlabel('Year')
ax.set_ylabel('Oxygen (umol/l)')
ax.set_title('Oxygen on the EU Shelf')

Solution for plotting function


In [ ]:
def plot_function(X_data, Y_data, x_label, y_label, title):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(X_data, Y_data, color='blue', alpha=0.1) 
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_title(title)

In [ ]:
plot_function(ICES_year, ICES_Oxygen, "Year", "O2", "Decadal O2")

In [ ]: