Basics Refresher

This exercise centres around the loading and plotting data from a file while using Numpy functions. You have been given two dimensional data within three csv files and are required to plot the average, maximum and minimum of the columns of that data.

Looping over the filenames given:

  1. Read the file-name using the Numpy loadtxt function (use help if you've forgotten how this works!)
  2. Find the mean, max and minimum of each column in the data using numpy functions (use axis=0)
  3. Plot the mean minimum and maximum of each column with respect to the row number
    • A neat way to do this could be to use a 1 by 3 layout of subplots, i.e. fig.add_subplot(1, 3, i), $i \in [1, 2, 3]$ for plotting the mean, max and min
    • Try adding a meaningful label to the y axes

In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
filename = ['data/inflammation-01.csv','data/inflammation-02.csv' ,'data/inflammation-03.csv']

In [3]:
for file in filename:    
    data = np.loadtxt(file)
    means = np.mean(data,axis=0)
    maxs = np.max(data,axis = 0)
    mins = np.min(data,axis = 0)

    fig = plt.figure(figsize=(10.0, 3.0))

    ax1 = fig.add_subplot(1, 3, 1)
    ax2 = fig.add_subplot(1, 3, 2)
    ax3 = fig.add_subplot(1, 3, 3)

    ax1.set_ylabel('average')
    ax1.plot(means)

    ax2.set_ylabel('max')
    ax2.plot(maxs)

    ax3.set_ylabel('min')
    ax3.plot(mins)

    fig.tight_layout()        # Slightly better spacing around subplots
    plt.show()



In [ ]: