In-Class Activity: Introduction to Plotting

 


Introduction

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



Exercises

  1. Create a new notebook for the statements and functions you will write in this activity. Save this new notebook with a name representative of this activity.

  2. (From the reading/class preparation before this activity) Work through the Pyplot tutorial up to (but not including) the section "Controlling line properties". The section "Plotting with keyword strings" is optional. Your code for these exercises may be all in one Code cell or separate code cells.

  3. In a new Code cell, define a function that will compute the number of A's, B's, C's, D's, and F's from a list of numerical grades. This function should take a list of grades as a parameter and should return a list containing the counts for each of the letter grades. For example, if a list of grades was
    [73.2, 84.7, 89.5, 92.1, 90.7, 77.6, 95.8, 82.1, 70.4, 69.6, 57.2, 78.9, 91.5, 85.3, 74.3]
    the list of letter grade counts that would get returned is
    [4, 4, 5, 1, 1]
    because there are 4 A's, 4 B's, 5 C's, 1 D, and 1 F.
    Test your function using this example.

  4. Copy your code from the previous in-class activity that read in a set of assignment grades for a class and stored them in 5 separate lists into a new Code cell.

  5. Following the example in the "Plotting with categorical variables" section of the tutorial, plot a bar graph showing the number of A's, B's, C's, etc, of at least one of your assignment grades lists.

  6. Now create another bar graph of the same data, but order the bars from number of F's to number of A's, going left to right, instead of A's to F's, left to right.

  7. In a new Code cell, copy and run the histogram example (in the section "Working with Text"). Add comments to your code to explain what the code is doing, and why the histogram looks the way it does. To get your code to run, you will need to add the line import numpy as np at the beginning of the Code cell.

  8. For the list of grades given above (the list is named grades in the code below), the following code will plot a histogram of those grades:
                    # Creating histogram
                    fig, ax = plt.subplots(figsize =(10, 7))
                    ax.hist(grades, bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
                     
                     # Show plot
                     plt.show()
    Copy this code into a new Code cell and run it. Think about whether it looks the way you thought it would/should.

  9. Create a histogram for the same list of assignment grades that you used to create a bar graph. How does this compare to the bar graph?

  10. What is the difference between a bar graph and a histogram? Which is a better format (or is there a case to be made for the use of both?) to visually display your grade data? You are expected to do some online research to help answer these questions. Write your answers to these questions in a new Text cell at the end of this activity.

Submit