Introduction to NumPy

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

Load in the data as in previous exercises, except this time we want the "temperature" array:

In [2]:
with np.load("weather_data.npz") as weather:
    temperature = weather["temperature"]

The ground-level slice is at index 0, so we can grab it and use the np.mean function to average the values in it:

In [3]:
np.mean(temperature[0])
Out[3]:
280.29968572319876

This number is so high due to it being in Kelvin. Convert it to Celcius with:

In [4]:
temperature -= 273.15

Now, the mean will be in the correct units:

In [5]:
np.mean(temperature[0])
Out[5]:
7.149685723198808

Now, the entire ground-level slice looks like:

In [6]:
fig, ax = plt.subplots()
im = ax.imshow(temperature[0])

fig.colorbar(im)
Out[6]:
<matplotlib.colorbar.Colorbar at 0x7f78fb74fa60>

Including all the data from the higher atmosphere both smooths out the data, as well as bringing its average down.

We do this by giving the mean function the complete 3D array, but tell it to average along the zeroth axis (altitude):

In [7]:
average_column = np.mean(temperature, axis=0)

fig, ax = plt.subplots()
im = ax.imshow(average_column)

fig.colorbar(im)
Out[7]:
<matplotlib.colorbar.Colorbar at 0x7f78f96c6fe0>

If we instead grab just the ground-level layer and average along the latitude axis, we see that it gets warmer the further South you go:

In [8]:
# Higher values of the index are more South

average_by_latitude = np.mean(temperature[0], axis=1)

fig, ax = plt.subplots()
im = ax.plot(average_by_latitude)