In [2]:
!wget -O land_global_temperature_anomaly-1880-2015.csv https://www.ncdc.noaa.gov/cag/time-series/global/globe/land/all/1/1880-2015.csv
In [1]:
import numpy
In [2]:
!head land_global_temperature_anomaly-1880-2015.csv
In [3]:
numpy.loadtxt(fname='land_global_temperature_anomaly-1880-2015.csv',delimiter=',',skiprows=4)
Out[3]:
In [4]:
T_land = numpy.loadtxt(fname='land_global_temperature_anomaly-1880-2015.csv',delimiter=',',skiprows=4)
In [5]:
print(T_land)
In [6]:
numpy.shape(T_land)
Out[6]:
In [7]:
T_land.shape
Out[7]:
In [8]:
1632/2
Out[8]:
In [9]:
1632/12
Out[9]:
In [10]:
2015-1880
Out[10]:
In [11]:
from matplotlib import pyplot
%matplotlib inline
In [12]:
T_land[0]
Out[12]:
In [13]:
T_land[:,1]
Out[13]:
In [14]:
pyplot.plot(T_land[:,1])
Out[14]:
In [15]:
#Remember ti remove the ugly line at the beggining of the plot
#We us a ; at the end of the command
pyplot.plot(T_land[:,1]);
In [17]:
pyplot.plot(T_land[0:25,0], T_land[0:25,1]);
In [18]:
date = numpy.arange('1880','2016',dtype=('datetime64[M]'))
In [19]:
print(date)
In [20]:
len(date)
Out[20]:
In [21]:
dummy = numpy.linspace(1,1632, 1632)
In [22]:
print(dummy)
In [24]:
pyplot.xticks(dummy, date, rotation=75)
#pyplot.xticks(dummy[::12*10],date[::12*10], rotation=75)
pyplot.plot(dummy, T_land[:, 1]);
In [26]:
pyplot.xticks(dummy[1::12*10],date[1::12*10], rotation=75)
#it cuts january and start from february
pyplot.plot(dummy, T_land[:, 1]);
#start:stop:step; ::step
In [27]:
In [29]:
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
In [32]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10], date[::12*10], rotation=75)
pyplot.plot(dummy, T_land[:, 1], color='#ff2424', ls='-', lw=1);#linestyle='-' and linewidth=1
pyplot.title('Land global temperature anomalies. \n')
pyplot.xlabel('Year-Month')
pyplot.ylabel('Land temperature anomaly [ºC]')
pyplot.grid()
pyplot.savefig('temp_anomalies.pdf');
In [ ]:
In [36]:
pyplot.figure(figsize=(10,5))
pyplot.hist(T_land[:,1], bins=25, normed=True, color='b', alpha=0.5)
pyplot.xlabel('Land temperature anomaly [ºC]')
pyplot.ylabel('Frequency');
In [38]:
mean_T = numpy.mean(T_land[:,1])
median_T = numpy.median(T_land[:,1])
print('The mean value is {:.5} and the median is {:.5}'.format(mean_T, median_T))
In [39]:
variance_T = numpy.var(T_land[:,1])
sigma_T = numpy.sqrt(variance_T)
print('The variance is {:.5} and the standard deviation is {:.5}'.format(variance_T, sigma_T))
In [41]:
from scipy import stats
In [44]:
bins = numpy.linspace(min(T_land[:,1]), max(T_land[:,1]), 40)
pyplot.figure(figsize=(10,5))
pyplot.hist(T_land[:,1], bins=25, normed=True, color='b', alpha=0.5)
#Plot the probability density function
pyplot.plot(bins, stats.norm.pdf(bins, mean_T, sigma_T), color='#ff5733', ls='-', lw=2.5)
pyplot.xlabel('Land temperature anomaly [ºC]')
pyplot.ylabel('Frequency');
pyplot.grid();
In [ ]:
In [50]:
pyplot.figure(figsize=(12,4))
#first plot
pyplot.subplot(121) #creates a grid of 1 row, 2 columns and select the first plot.
pyplot.xticks(dummy[::12*10], date[::12*10], rotation=75)
pyplot.plot(dummy, T_land[:, 1], color='b', ls='-', lw=1);#linestyle='-' and linewidth=1
#pyplot.title('Land global temperature anomalies. \n')
pyplot.xlabel('Year-Month')
pyplot.ylabel('Land temperature anomaly [ºC]')
pyplot.grid()
#second plot
pyplot.subplot(122)
bins = numpy.linspace(min(T_land[:,1]), max(T_land[:,1]), 40)
#plot the histogram
pyplot.hist(T_land[:,1], bins=25, normed=True, color='b', alpha=0.5, histtype='stepfilled')
#Plot the probability density function
pyplot.plot(bins, stats.norm.pdf(bins, mean_T, sigma_T), color='#ff2525', ls='-', lw=2.5)
pyplot.xlabel('Land temperature anomaly [ºC]')
pyplot.ylabel('Frequency');
pyplot.grid();
In [51]:
def smooth_data(N, data):
"""
Returns smoothed data using a sliding_moving acarage.
Arguments:
----------
N(int) : amount of data values we want to average.
data (array) : array of data we want to smooth.
Returns:
--------
smooth (array): array with smoothed data.
"""
window = numpy.ones(N)/N
smooth = numpy.convolve(data, window, 'same')
return smooth
In [52]:
?smooth_data
In [53]:
smooth = smooth_data(12, T_land[:,1])
In [64]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10], date[::12*10], rotation=75)
pyplot.plot(dummy, T_land[:, 1], color='b', ls='-', lw=1, alpha=0.5)
pyplot.plot(dummy, smooth, color='#ff2525', ls='-', lw=2)
pyplot.xlabel('Year-Month')
pyplot.ylabel('Land temperature anomaly [ºC]')
pyplot.title('Smooth data')
pyplot.grid()
In [67]:
m, b = numpy.polyfit(dummy, T_land[:,1],1)
In [68]:
print(m, b)
In [82]:
f_linear = numpy.poly1d((m, b))
In [83]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10], date[::12*10], rotation=75)
pyplot.plot(dummy, T_land[:, 1], color='b', ls='-', lw=1, alpha=0.5)
pyplot.plot(dummy, f_linear(dummy), color='k', ls='--', lw=2, label='linear regression')
pyoplot
pyplot.xlabel('Year-Month')
pyplot.ylabel('Land temperature anomaly [ºC]')
pyplot.legend(loc = 'best', fontsize=15)
pyplot.grid();
In [ ]: