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).
-
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.
-
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.
-
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 thelen(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 thefloat()
function. Use list comprehension to do this.The above steps should be two short lines of code:
x_vals = ... y_vals = ...
-
Plot the data using matplotlib
Follow the examples in the pre-class preparation to plot the data. Add labels and titles to your plots.
-
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]
usingax.set_xticks()
andax.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:
-
Your notebook must have at least one plot.
-
Begin with an introduction to the topic, and then provide a narrative of what the notebook explains.
-
It’s ok and encouraged to Google for how to do any part of this assignment. E.g. how to use LaTeX to create a certain symbol.
-
You must cite any sources you access, and give credit to any class work you have done previously and are duplicating here.
-
Your Notebook and code are forms of communication. Take care in your work so that the work communicates effectively.
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:
- Run All from the Cell menu.
- Use the web browser’s print function.
- 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.
- Save the resulting PDF file in a location where you can find it for uploading.
- Open the PDF file and verify that it looks correct.
If the above does not work well, this alternative may work better:
- Run All from the Cell menu.
- Go to the File menu, and select File -> Download As... -> HTML (.html)
- This will download a file with the same name as the notebook, except ending in
.html
- Find this HTML file and open it with your web browser.
- Verify that the web browser displays the notebook with graphics correctly.
- Use the web browser’s print function.
- 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.
- Save the resulting PDF file in a location where you can find it for uploading.
- Open the PDF file and verify that it looks correct.
Submitting Your Work
You should use Gradesope to submit the following files:
- your
ps7pr1.ipynb
file containing your solution for Problem 1 - your
ps7pr1.pdf
file containing a PDF version of Problem 1
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.