Global Surface Temperature

This example uses historical data since 1880 on average global surface temperature changes from NASA's GISS Surface Temperature Analysis (GISTEMP) (original file) That site has lots of other data sets in csv format, too.

Click the "play" icon or press shift+enter to execute each cell.


In [1]:
# import the software packages needed
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
inline_rc = dict(mpl.rcParams)

Importing a data set

This next cell may take a little while to run if it's grabbing a pretty big data set. The cell label to the left will look like "In [*]" while it's still thinking and "In [2]" when it's finished.


In [2]:
# Combined land and ocean temperature averages (LOTI: Land Ocean Temperature Index)
data1 = pd.read_csv('http://github.com/adamlamee/CODINGinK12-data/raw/master/LOTI.csv', header=1).replace(to_replace="***", value=np.NaN)
data_LOTI = data1.apply(lambda x: pd.to_numeric(x, errors='ignore'))

# Only land temperature averages
data2 = pd.read_csv('http://github.com/adamlamee/CODINGinK12-data/raw/master/LAND.csv', header=1).replace(to_replace="***", value=np.NaN)
data_LAND = data2.apply(lambda x: pd.to_numeric(x, errors='ignore'))

We can view the first few rows of the file we just imported.


In [3]:
# The .head(n) command displays the first n rows of the file.
data_LAND.head(5)


Out[3]:
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec JanDec DecNov DJF MAM JJA SON
0 1880 -0.84 -0.41 -0.48 -0.67 -0.37 -0.50 -0.49 0.06 -0.51 -0.69 -0.53 -0.55 -0.50 NaN NaN -0.51 -0.31 -0.58
1 1881 -0.80 -0.63 -0.37 -0.28 -0.05 -1.15 -0.56 -0.28 -0.34 -0.47 -0.54 -0.13 -0.47 -0.50 -0.66 -0.23 -0.66 -0.45
2 1882 0.09 -0.14 -0.10 -0.62 -0.40 -1.05 -0.74 -0.14 -0.10 -0.35 -0.42 -0.70 -0.39 -0.34 -0.06 -0.37 -0.64 -0.29
3 1883 -0.71 -0.97 -0.48 -0.34 -0.38 0.41 -0.05 -0.20 -0.49 -0.60 -0.71 -0.43 -0.41 -0.43 -0.79 -0.40 0.05 -0.60
4 1884 -0.64 -0.37 -0.42 -0.96 -1.22 -0.86 -0.88 0.11 -0.41 -0.81 -0.85 -1.03 -0.70 -0.65 -0.48 -0.87 -0.54 -0.69

Plotting the data


In [4]:
x1 = data_LOTI.Year
y1 = data_LOTI.JanDec

# plt.plot() makes a line graph, by default
fig = plt.figure(figsize=(10, 5))
plt.plot(x1, y1)
plt.title('Average land an docean temperature readings')
plt.xlabel('Year')
plt.ylabel('Percent temp change')


Out[4]:
<matplotlib.text.Text at 0x7fe0a5cf6400>

In [5]:
x2 = data_LAND.Year
y2 = data_LAND.JanDec

# plt.plot() makes a line graph, by default
fig = plt.figure(figsize=(10, 5))
plt.plot(x2, y2)
plt.title('Land temperature readings')
plt.xlabel('Year')
plt.ylabel('Percent temp change')


Out[5]:
<matplotlib.text.Text at 0x7fe0a5634cf8>

In [6]:
# Wow, this needs a title and axis labels!
fig = plt.figure(figsize=(10, 5))
plt.plot(x1, y1, label="Land and Ocean")
plt.plot(x2, y2, label="Land only")
plt.legend()
plt.show()


Edit and re-plot

If you like Randall Monroe's webcomic XKCD as much as I do, you can make your plots look like his hand-drawn ones. Thanks to Jake VanderPlas for sorting that out.


In [7]:
plt.xkcd()
fig = plt.figure(figsize=(10, 5))
plt.plot(x1, y1)


Out[7]:
[<matplotlib.lines.Line2D at 0x7fe0a5b8f588>]
/home/main/anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [8]:
# to make normal plots again
mpl.rcParams.update(inline_rc)

Try editing some code and re-running the cell to see the effects. For more information on formatting the markdown text in a cell like this one, go to Help > Markdown > Basic Writing and Formatting Text.

To save your work: go to File > Save and Checkpoint. That only saves your edits as long as you're working inthis notebook. All is lost after 10 minutes of inactivity.

To really save your work: go to File > Download as > iPython notebook (or save as pdf if you just want to show someone a snapshot of what your code and output look like.

References


In [ ]: