In [2]:
import numpy
import matplotlib.pyplot
%matplotlib inline
In [3]:
data =numpy.loadtxt (fname = 'data/weather-01.csv', delimiter=',')
In [4]:
#create a wide figure to hold the subplots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
#create placeholders for plots
subplot1= fig.add_subplot(1,3,1
)
subplot2= fig.add_subplot(1,3,2)
subplot3= fig.add_subplot(1,3,3)
subplot1.set_ylabel('average')
subplot1.plot(numpy.mean(data, axis=0))
subplot2.set_ylabel('min')
subplot2.plot(numpy.min(data, axis=0))
subplot3.set_ylabel('max')
subplot3.plot(numpy.max(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
In [ ]:
In [5]:
#repeat same operations again and again
In [6]:
word= 'notebook'
print (word [4])
In [7]:
print (word [0])
In [8]:
#a loop
for char in word:
print (char)
In [9]:
# get a list of all the filenames from disk
import glob
In [10]:
print (glob.glob('data/weather*.csv'))
In [11]:
#produce series of plots for each datafile
In [12]:
filename= sorted (glob.glob('data/weather*.csv'))
filename= filename [0:3] #takes 01, 02, 03 files from data/weather only
for f in filename:
print (f)
data= numpy.loadtxt (fname=f, delimiter= ',')
#create a wide figure to hold the subplots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
#create placeholders for plots
subplot1= fig.add_subplot(1,3,1
)
subplot2= fig.add_subplot(1,3,2)
subplot3= fig.add_subplot(1,3,3)
subplot1.set_ylabel('average')
subplot1.plot(numpy.mean(data, axis=0))
subplot2.set_ylabel('min')
subplot2.plot(numpy.min(data, axis=0))
subplot3.set_ylabel('max')
subplot3.plot(numpy.max(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
In [13]:
num =37
if num>100:
print ('greater')
else:
print ('not greater')
print ('Done')
In [14]:
num =-3
if num>0:
print (num, "is positive")
elif num==0:
print (num, "is zero")
else:
print (num, "is negative")
In [15]:
filename= sorted (glob.glob('data/weather*.csv'))
#filename= filename [0:3] #takes 01, 02, 03 files from data/weather only
for f in filename:
print (f)
data= numpy.loadtxt (fname=f, delimiter= ',')
if numpy.max (data, axis=0)[0] ==0 and numpy.max (data, axis=0)[20]==20:
print ("suspicious looking maxima") #if the first max value is 0 and the 20th one is 20c
elif numpy.sum (numpy.min(data, axis=0))==0:
print ("minima add up to zero")
else:
print ("data looks ok")
#create a wide figure to hold the subplots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
#create placeholders for plots
subplot1= fig.add_subplot(1,3,1
)
subplot2= fig.add_subplot(1,3,2)
subplot3= fig.add_subplot(1,3,3)
subplot1.set_ylabel('average')
subplot1.plot(numpy.mean(data, axis=0))
subplot2.set_ylabel('min')
subplot2.plot(numpy.min(data, axis=0))
subplot3.set_ylabel('max')
subplot3.plot(numpy.max(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
FUNCTIONS
In [16]:
def fahr_to_kelvin (temp):
return((temp-32)*(5/9)+ 273.15)
In [17]:
print ('freezing point of water', fahr_to_kelvin(32))
In [18]:
print ('boiling point of water', fahr_to_kelvin(212))
In [19]:
# as you go through your work you can create functions and put them in your own library
In [20]:
def analyse (filename):
data= numpy.loadtxt (fname=filename, delimiter= ',')
#create a wide figure to hold the subplots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
#create placeholders for plots
subplot1= fig.add_subplot(1,3,1
)
subplot2= fig.add_subplot(1,3,2)
subplot3= fig.add_subplot(1,3,3)
subplot1.set_ylabel('average')
subplot1.plot(numpy.mean(data, axis=0))
subplot2.set_ylabel('min')
subplot2.plot(numpy.min(data, axis=0))
subplot3.set_ylabel('max')
subplot3.plot(numpy.max(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
In [26]:
def detect_problems (filename):
"""Some of our temperature files have problems, check for these
This function reads a file (filename arguent) and reports on
odd looking maxima and minima that add up to zero. this seems
to happen when the sensors break.
The function does not return any data.
"""
data= numpy.loadtxt (fname=filename, delimiter= ',')
if numpy.max (data, axis=0)[0] ==0 and numpy.max (data, axis=0)[20]==20:
print ("suspicious looking maxima") #if the first max value is 0 and the 20th one is 20c
elif numpy.sum (numpy.min(data, axis=0))==0:
print ("minima add up to zero")
else:
print ("data looks ok")
In [22]:
for f in filename [0:5]:
print (f)
analyse (f)
detect_problems (f)
In [23]:
help (numpy.loadtxt
)
In [27]:
help (detect_problems)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: