In [1]:
import numpy
import matplotlib.pyplot
%matplotlib inline
In [2]:
data = numpy.loadtxt (fname='Data/weather-01.csv', delimiter = ',')
In [3]:
# create a wide figure to hold the sublots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
# create placeholder 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('max')
subplot2.plot(numpy.max(data, axis=0))
subplot3.set_ylabel('min')
subplot3.plot(numpy.min(data, axis=0))
fig.tight_layout()
# how far away the figures are- tight_layout is further apart than default
matplotlib.pyplot.show()
#adding a label to each subplot and setting the data to go in each subplot
#will create three graphs side by side
In [4]:
word = 'notebook'
print (word[4])
In [5]:
# for loops, char is counter variable
for char in word:
print (char)
In [6]:
import glob
# global file system library has only one function in it
In [7]:
print (glob.glob('data/weather*.csv'))
# all files start with weather and end with .csv * means there can be anything in between
In [8]:
# new variable
filenames = sorted(glob.glob('data/weather*.csv'))
#overwrite 'filenames' with a different value to create a smaller slice
#filenames = filenames[0:3]
#can add a '#' before code to not use it without deleting it
for f in filenames:
print (f)
#loop across individual file names in 'filenames' and indent lines below so that it is part of the loop
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')
elif numpy.sum(numpy.min(data, axis=0)) ==0:
print ('minimum add to zero')
else:
print ('data looks ok')
#if the first data point is o and the 20th one is 20 or if all the data is 0, hen there is something wrong otherwise carry on
#shows message over graph
# create a wide figure to hold the sublots
fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))
# create placeholder 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('max')
subplot2.plot(numpy.max(data, axis=0))
subplot3.set_ylabel('min')
subplot3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
In [9]:
num = 107
if num > 100:
print ('Greater')
else:
print ("Not Greater")
print ('Done')
In [10]:
num = -3
if num >0:
print (num, "is positive")
elif num == 0:
print (num, "is zero")
else:
print (num, "is negative")
#single = is to assign a value to the variable, double == is for matamaticaly equal, =? is not equal to
#elif is short for else if, do another if test if the first one fails
In [11]:
# functions start with def ie define
def fahr_to_kelvin(temp):
return((temp-32)*(5/9)+273.15)
#doesn't do anything but this function is there to be used whenever
In [12]:
print ('freezing point of water:', fahr_to_kelvin(32))
In [13]:
print ('boiling point of water:', fahr_to_kelvin(212))
In [14]:
def fahr_to_cel(temp):
return((temp-32)*(5/9))
In [15]:
print ('cross over point:', fahr_to_cel(-40))
In [16]:
def analyse(filename):
""" Calculate the mean max and min and create three graphs within one plot
"""
data = numpy.loadtxt(fname=filename, delimiter=',')
# create a wide figure to hold the sublots
fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
# create placeholder 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('max')
subplot2.plot(numpy.max(data, axis=0))
subplot3.set_ylabel('min')
subplot3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
In [17]:
def detect_problems(filename):
"""Reports on odd looking maxima and minima. The function does not return data it adds
a label to the graph
"""
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')
elif numpy.sum(numpy.min(data, axis=0)) ==0:
print('minimum add to zero')
else:
print('data looks ok')
#the string is a comment on the function to describe what it does. triple quote strings cover multiple lines
In [18]:
for f in filenames [0:5]:
print (f)
analyse (f)
detect_problems (f)
In [19]:
help(numpy.loadtxt)
In [20]:
help(detect_problems)
In [21]:
# we want a description about what this function does, it is useful to have this to remember in the future
In [22]:
help(analyse)
In [ ]: