Mini-Lab: Introduction to Plotting

 


Introduction

The purpose of this mini-lab is to practice plottng functions and data sets and to make histograms using the matplotlib module in Python.



Exercises

  1. Create a new file for the statements and functions you will write in this mini-lab. Save this new file with a name representative of this mini-lab.

  2. Begin working through the Pyplot tutorial up to (but not including) the section "Controlling line properties".

  3. Create a new function, 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).

  4. Copy one high temperature array and one low temperature array from your work in Lab 3: Using Arrays.

  5. Create a new function, 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?)

  6. Skip to the section titled "Working with Text" in the Beginner's Guide to Pyplot. After reading this section, update the graph of your cosine functions to include an appropriate title and labels for the axes.

  7. Be sure to read the subsection "Using mathematical expressions in text" . This could be very useful to incorporate mathematical expressions in your text, such as in titles or axes of graphs.

  8. Run the histogram example (in the "Working with Text"). Add comments to your code to explain what the code is doing, and why the histogram looks the way it does.

  9. Copy the following histogram example into your file.
        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:

    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.

  10. Create a new function, 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.

Addition to Programming Project #1

Add a function to your 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.

Submit