In [3]:
# !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 [18]:
import numpy
In [19]:
!head land_global_temperature_annomaly-1880-2015.csv
In [21]:
T_Lan = numpy.loadtxt(fname='land_global_temperature_annomaly-1880-2015.csv',delimiter=',',skiprows=4)
In [22]:
print(T_Lan)
In [23]:
T_Lan.shape
Out[23]:
In [26]:
from matplotlib import pyplot
%matplotlib inline
In [27]:
T_Lan[0]
Out[27]:
In [34]:
T_Lan[:,1]
Out[34]:
In [38]:
# You can delet the uglu line by adding ';' at the end of the plot command
pyplot.plot(T_Lan[:,1]);
In [40]:
pyplot.plot(T_Lan[:,0],T_Lan[:,1]);
In [41]:
pyplot.plot(T_Lan[0:30,0],T_Lan[0:30,1]);
In [45]:
date = numpy.arange('1880','2016',dtype=('datetime64[M]'))
In [46]:
print(date)
In [53]:
dummy = numpy.linspace(1,1632,1632)
print(dummy)
In [60]:
# too many labels in the x axis.
#pyplot.xticks(dummy,date rotation=75);
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=75);
pyplot.plot(dummy,T_Lan[:,1]);
In [63]:
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
In [105]:
pyplot.figure(figsize=(10,6))
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=40)
pyplot.plot(dummy, T_Lan[:,1], color='#2380a3' , linestyle='-', linewidth =1)
pyplot.title('Land global temperature anomalies.\n')
pyplot.xlabel('Year-month')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.grid()
# CHECK why it is croping the plot.
# pyplot.savefig('temp_anomalies.png')
In [107]:
pyplot.figure(figsize=(10,5))
pyplot.hist(T_Lan[:,1])
pyplot.xlabel('Land temperature anomaly [°C]')
pyplot.ylabel('Frequency');
In [143]:
pyplot.figure(figsize=(10,5))
pyplot.hist(T_Lan[:,1], bins=30, normed = True, color='g', alpha=0.6)
pyplot.xlabel('Land temperature anomaly [°C]')
pyplot.ylabel('Normalized Frequency');
In [138]:
mean_T = numpy.mean(T_Lan[:,1])
median_T = numpy.median(T_Lan[:,1])
print('The mean value is {:.5}\nThe median value is {:.5}'.format(mean_T,median_T))
In [137]:
variance_T = numpy.var(T_Lan[:,1])
sigma_T = numpy.sqrt(variance_T)
print('The variance is {:.5}'.format(variance_T))
print('The standard deviation is {:.5}'.format(sigma_T))
In [139]:
from scipy import stats
In [148]:
bins = numpy.linspace(min(T_Lan[:,1]),max(T_Lan[:,1]),40)
pyplot.figure(figsize=(10,5))
pyplot.hist(T_Lan[:,1], bins, normed = True, color='g', alpha=0.6)
#Plot the probability densitty function
pyplot.plot(bins, stats.norm.pdf(bins,mean_T,sigma_T), color='#ff5733', ls='-',lw=2)
pyplot.xlabel('Land temperature anomaly [°C]')
pyplot.ylabel('Normalized Frequency');
In [161]:
pyplot.figure(figsize=(15,5))
#First Plot
pyplot.subplot(121)
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=40)
pyplot.plot(dummy, T_Lan[:,1], color='#2380a3' , linestyle='-', linewidth =1,alpha=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_Lan[:,1]),max(T_Lan[:,1]),40)
pyplot.hist(T_Lan[:,1], bins, normed = True, color='#2380a3', alpha=0.6,histtype='bar') #histtype: bar & stepfilled
#Plot the probability densitty 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('Normalized Frequency');
In [162]:
def smooth_data(N,data):
"""
Returns smoothed data using a sliding_moving average
Arguments:
----------
N (int): Amount of data values that will be averaged
data (array): Array of data we want to smooth.
Returns:
--------
smooth (array): array with smothed data.
"""
window = numpy.ones(N)/N
smooth = numpy.convolve(data,window,'same')
return smooth
In [164]:
# To check what a function does apply "?<function name>"
?smooth_data
In [169]:
smooth = smooth_data(12,T_Lan[:,1])
In [170]:
print(smooth)
In [189]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=40)
pyplot.plot(dummy, smooth, color='r' , linestyle='-', linewidth =2,alpha=1)
pyplot.title('Land global temperature anomalies averaged.\n')
pyplot.xlabel('Year-month')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.grid()
In [192]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=40)
pyplot.plot(dummy, T_Lan[:,1], color='g' , linestyle='-', linewidth =1, alpha=0.8)
pyplot.plot(dummy, smooth, color='r' , linestyle='-', linewidth =2,alpha=1)
pyplot.title('Land global temperature anomalies.\n(Data (g) and Smooth Data(r))\n')
pyplot.xlabel('Year-month')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.grid()
In [194]:
m,b = numpy.polyfit(dummy,T_Lan[:,1],1)
print (m,b)
In [196]:
f_linear = numpy.poly1d((m,b))
print(f_linear)
In [201]:
pyplot.figure(figsize=(10,5))
pyplot.xticks(dummy[::12*10],date[::12*10], rotation=40)
#Original Data series
pyplot.plot(dummy, T_Lan[:,1], color='g' , linestyle='-', linewidth =1, alpha=0.8, label='original data')
#Linear Regression
pyplot.plot(dummy,f_linear(dummy), color='k', ls='--', linewidth=2, label='linear regression')
#Averagedfunction
#pyplot.plot(dummy, smooth, color='r' , linestyle='-', linewidth =2,alpha=1)
pyplot.title('Land global temperature annomalies\n')
pyplot.xlabel('Year-month')
pyplot.ylabel('Land temperature anomaly [°C]')
pyplot.legend()
pyplot.grid()