CS 124
Fall 2023

Problem Set 7

due by 10 p.m. on Saturday, September 23, 2023

Preliminaries

In your work on this assignment, make sure to abide by the collaboration policies of the course.

If you have questions while working on this assignment, please post them on Piazza or come to TA Help Hours.

Make sure to submit your work on Gradescope.

Problem 1: Plotting Data in Jupyter Notebook with matplotlib

100 points; pair-optional or group-of-3-optional

Create a new Jupyter Notebook file, save it using the the name ps7pr1.ipynb

This assignment has minimal requirements - creativity and exploration is encouraged.

Create a Jupyter Notebook that will plot COVID cases in Massachusetts. Using a combination of Markdown cells and code cells to explain the data and plot it using matplotlib.

Feel free to include images/pictures, but note that we will not see them on Gradescope when you upload the file. (You can optionally upload a PDF of the notebook as well if you want us to see your images).

  1. Download some data from the MA COVID Dashboard

    https://www.mass.gov/info-details/covid-19-response-reporting

    Select Overview Trends or COVID-19 Cases on the left navigation bar.
    Then highlight all of the data in one of the plots and select Download on the lower right corner. In the pop-up, choose Data. Once the new window opens, choose Download all rows as a text file. This should save the file on your computer with .csv extension. For ease of use, you may choose to rename the file data.csv.

    If you have trouble, you can use this file we downloaded for you: data.csv

    Move this file into the same folder as your Jupyter Notebook files.

  2. Read the data into a Python list.

    This code will read data.csv into a Python nested list

    import csv
    with open('data.csv', newline='') as f:
        f.readline()  # read and discard the first line of the file (data headers)
        reader = csv.reader(f)
        data = list(reader)
    
    print(data)   # print the data
    

    The above code probably doesn’t make much sense yet. That’s ok. The main thing to know is that it read the file into a python list. It should have printed the list out so that you can see that it is actually a nested list.

  3. Set up your x_vals and y_vals variables

    We suggest ignoring the date labels for now. Your x_vals list should therefore be a sequence of numbers that correspond to the data points. Use the range() function to generate a list of integers from 0 to the len(data)

    The y_vals should contain only the second entry in each sublist (the entry with index [1]). The values are strings, so they should be turned into numbers with the float() function. Use list comprehension to do this.

    The above steps should be two short lines of code:

    x_vals = ...
    y_vals = ...
    
  4. Plot the data using matplotlib

    Follow the examples in the pre-class preparation to plot the data. Add labels and titles to your plots.

  5. Be creative and explore further

    Once you have a reasonable plot, you can be done.

    If you want to explore further consider trying to use the data labels in index [0] using ax.set_xticks() and ax.set_xticklabels() functions, along with a rotation. See this answer on Stack Overflow for some hints. Search and Google around for other ideas.

    Be creative and explore other ways of plotting this data or any other data you find.

Requirements:

Important guidelines

  • You must format your Jupyter Notebook nicely. Some of the points will be awarded for clean and neat style.

  • Please Run All Cells before submitting your notebook file on Gradescope.

  • This submission will be manually graded, so you may use a certain amount of creativity as long as the required elements are easily found.

Saving a PDF version of your Jupyter Notebook

When you upload your .ipynb file to Gradescope, the images of the plots will not be included. We need you to also submit a PDF version of your notebook.

One suggested way to do this is:

  1. Run All from the Cell menu.
  2. Use the web browser’s print function.
  3. In the print menu, select Save as PDF as the destination rather than an actual printer. This will appear in different places for different web browsers.
  4. Save the resulting PDF file in a location where you can find it for uploading.
  5. Open the PDF file and verify that it looks correct.

If the above does not work well, this alternative may work better:

  1. Run All from the Cell menu.
  2. Go to the File menu, and select File -> Download As... -> HTML (.html)
  3. This will download a file with the same name as the notebook, except ending in .html
  4. Find this HTML file and open it with your web browser.
  5. Verify that the web browser displays the notebook with graphics correctly.
  6. Use the web browser’s print function.
  7. In the print menu, select Save as PDF as the destination rather than an actual printer. This will appear in different places for different web browsers.
  8. Save the resulting PDF file in a location where you can find it for uploading.
  9. Open the PDF file and verify that it looks correct.

Submitting Your Work

You should use Gradesope to submit the following files:

Warnings

  • Make sure to use these exact file names, or Gradescope will not accept your files. If Gradescope reports that a file does not have the correct name, you should rename the file using the name listed in the assignment page.

  • You must Run All cells before submitting.