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')
The data file statesSummary.csv is from the CDC. Before starting the program, open up the csv file and see what it looks like.
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[0]
stateValues = []
for w in words[1:]:
stateValues.append(int(w))
color = np.random.rand(10)
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()