The purpose of this mini-lab is to practice plottng functions and
data sets and to make histograms using the matplotlib
module in Python.
graphCosines
, which
graphs the functions cos(x)
,
cos(2x)
, 2cos(x)
, and
cos(x/2)
on the same axes. You should use a range
of x-values from -4*pi
to
4*pi
, with steps of size 0.1. You can use the arange(start, stop, step)
function to generate the range and create the array x. To use the predefined
value of pi
, you will need to import the
math
module, and then access pi
as
math.pi
. You may access the cosine function
by using numpy, as in np.cos(x)
.
plotTemps
, that
takes 2 arrays of temperatures as parameters and plots them
on the same axis. Test your function by calling it with the
2 arrays you created in the previous exercise.
(Think about: Do the highs
and lows follow any type of pattern?)
def gauss(): x = [random.gauss(3,1) for _ in range(400)] #x = [random.uniform(1,10) for _ in range(400)] y = [random.gauss(4,2) for _ in range(400)] bins = np.linspace(-4, 10, 20) plt.hist(x, bins, alpha=0.5, facecolor='r') plt.hist(y, bins, alpha=0.5, facecolor='y')Write your answers to the following questions as comments in your code:
random.gauss
function work?
linspace
function works?)
plt.hist([x,y],bins)
The link http://matplotlib.org/stable/api/_as_gen_/matplotlib.pyplot.hist.html
contains more details on the set of parameters that can be used with the
hist
function. (You will need to scroll about halfway down
the page.) You might also find this
link from a question posted on Stack Overflow to be a helpful
discussion of plotting two histograms at the same time.
drawHist
that draws a
histogram from one of your temperature arrays. Decide if you want to
see how many times each degree between the low temp and the high temp
was reached, or if you'd like to see something more like how often the
temps were in the 20s, the 30s, the 40s, etc. Experiment with different
bins.
Population.py
file that will plot
the counts of the prey and predator populations for each generation on
the same axes. Add a statement near the end of your
predator_prey
function that calls this function. This may
be useful to more clearly identify any patterns that occur with the
predator and prey, and help you with the Part II of the project. This addition does not need to be submitted as part of Part I.