y = log(x) or y = √ x ?
To test out this question, we will write a program that:
import math import matplotlib.pyplot as pltSince it's unwieldly to type "matplotlib.pyplot" before every function we'd like to use from that library, instead we'll use the common abbreviation of "plt". With this, we can plt.plot(), instead of matplotlib.pyplot.plot().
x = range(1,101)Remember: Python starts counting at 0 and goes up to, but not including the 101. So, this creates the list [1,2,...,100].
y1 =  for i in x: y = math.log(i) y1.append(y) y2 =  for i in x: y = math.sqrt(i) y2.append(y)We need two separate lists since we have two separate functions to graph.
plt.plot(x,y1,label='y1 = log(x)') plt.plot(x,y2,label='y2 = sqrt(x)') plt.legend()Creates the plot for safe keeping but does not display it until told to (see next lines).
plt.show()This line pops up the new graphics window to display the plots.
From your plots, which do you think grows faster: log(x) or sqrt(x)?
Using the Python program you wrote above, try the following:
Next, Let's focus on the question: "Has Lyme Disease Increased?" and examine data from the Center for Disease Control (CDC) to answer that question. Let's start with the tri-state area. Here are the years and occurrences:
years = [2003,2004,2005,2006,2007,2008,2009,2010,2011] ny = [5399,5100,5565,4460,4165,5741,4134,2385,3118] nj = [2887,2698,3363,2432,3134,3214,4598,3320,3398] ct = [1403,1348,1810,1788,3058,2738,2751,1964,2004]
To plot New York data as a `scatter plot' (dots at each (x,y) point), we add the commands:
import matplotlib.pyplot as plt #Library of plotting functions plt.scatter(years, ny) plt.show()
plt.plot(years, ny, label='NY')Add in labels for New Jersey and Connecticut data. You can then display a legend by adding the command:
plt.legend()Add axis labels and a title:
plt.title("Lyme Disease in NY, NJ, & CT") plt.xlabel('Years') plt.ylabel('Number of Cases')
import matplotlib.pyplot as plt import numpy as np
infile = open('statesSummary.csv','r')
yearLine = infile.readline() yearWords = yearLine.split(",") years =  for w in yearWords[1:]: years.append(int(w))
for i in range(5): line = infile.readline() words = line.split(",") stateName = words stateValues =  for w in words[1:]: stateValues.append(int(w)) color = np.random.rand(3) plt.scatter(years, stateValues, c=color, label=stateName)
plt.title("Cases of Lyme Disease") plt.xlabel('Years') plt.ylabel('Number of Cases') plt.legend(loc = 2, fontsize = 'x-small') plt.show()
Above, we use the general, file I/O of Python. Another option is the very useful package designed only for handling CSV files. Unsurprisingly, it's called csv and handles much of the parsing of lines that we did above. Included below is a simple example (csvEx.py):
#Simple use of csv module. #Assumes "in.csv" is in the same folder. #Katherine St. John #Spring 2016 import csv #Using the dictionary reader to access by column names f = open("in.csv") reader = csv.DictReader(f) m = [row['Homework'] for row in reader if int(row['Homework']) < 90] f.close() print m[-1] #Using the regular csv reader (ignoring first line with column names). #Note the use of 'with' for files: with open("in.csv") as f: reader = csv.reader(f) reader.next() #Ignore line with column names m = [row for row in reader if int(row) < 90] print m[-1]
You are welcome to use any approach to opening files in your homework and projects.