A document from MCS 275 Spring 2021, instructor David Dumas. You can also get the notebook file.

Quiz 11 Solutions

MCS 275 Spring 2021 - David Dumas

Instructions:

Deadline

This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, April 6, 2021.

Topic

This quiz covers matplotlib, as discussed in Lectures 28-29.

Resources you are allowed to consult

Quizzes are INDIVIDUAL, closed book (except where noted below), and only allow access to specified resources. For this quiz you can access:

Point distribution

There are two problems on this quiz, numbered 2 and 3. The point breakdown is:

Points Item
3 autograder
4 problem 2
4 problem 3
11 total

No problem 1 (as usual)

Problem 2: Unclear implications

Here is a table showing yearly values for two quantities from 1996 to 2008.

year math PhDs uranium
1996 1122 1060
1997 1123 1054
1998 1177 1052
1999 1083 933
2000 1050 877
2001 1010 890
2002 919 856
2003 993 730
2004 1076 923
2005 1205 1035
2006 1325 1240
2007 1393 1300
2008 1399 1310

In this table,

  • The column labeled "math PhDs" shows the total number of mathematics doctoral degrees awarded in the US
  • The column labeled "uranium" shows the total amount of uranium stored at US nuclear power plants (in units of millions of ounces)

Use matplotlib to plot the variation of both quantities on the same set of axes (with the x axis showing the year). Each quantity should be shown as a line with dots marking the individual data points. PhDs and stored uranium should be shown in different colors. Include a legend.

The code in the cell below can be used to save some typing---it has the two rightmost columns of the table above converted to list literals.

Upload two files for this problem:

  • quiz11prob2.py: The script you wrote to make the plot
  • quiz11prob2.png or quiz11prob2.jpg: An image file that shows the plot produced by your script, which can be saved by matplotlib or captured as a screenshot.

Note: The image and script you submit must match, i.e. the image must be one that was created with the exact script you submit.

Data source for this problem: US NSF and US DOE via tylervigen.com

In [ ]:
# annual data for 1996 to 2008
math_phds_awarded  = [1122, 1123, 1177, 1083, 1050, 1010, 919, 993, 1076, 1205, 1325, 1393, 1399]
stored_uranium_Moz = [1060, 1054, 1052, 933, 877, 890, 856, 730, 923, 1035, 1240, 1300, 1310]
In [3]:
# MCS 275 Quiz 11 Problem 2
# J Vaccaro
# I completed this work myself, in accordance with the syllabus
"""Plot and save data comparing the number of math phds to the amount of stored uranium"""

import matplotlib.pyplot as plt
import numpy as np

# Create the data arrays/lists
math_phds_awarded  = [1122, 1123, 1177, 1083, 1050, 1010, 919, 993, 1076, 1205, 1325, 1393, 1399]
stored_uranium_Moz = [1060, 1054, 1052, 933, 877, 890, 856, 730, 923, 1035, 1240, 1300, 1310]
years = np.arange(1996,2009)

# Plot the datasets on the same plot
plt.plot(years,math_phds_awarded,'r-o',label="Math PhDs Awarded") # Plots in red with lines and dots
plt.plot(years,stored_uranium_Moz,'b-o',label="Stored Uranium (Moz)") # Plots in blue with lines and dots

# Add title, legend, xlabel
plt.xlabel("Years")
plt.title("Uncertain implications")
plt.legend()

# Show and save the image
plt.show()
plt.savefig("quiz11prob2.png")
2021-04-07T23:26:38.339903 image/svg+xml Matplotlib v3.4.0, https://matplotlib.org/
<Figure size 432x288 with 0 Axes>

Problem 3: Zebra

Use matplotlib to make a density plot of the region in the xy plane where $0 \leq x \leq 5$ and $0 \leq y \leq 5$, where each pixel is colored according to the value of the function $$ f(x,y) = \sin( \sin( \sin( \sin( \cdots \sin(xy - x^2) \cdots )))) $$ where there are a total of 100 nested copies of the $\sin$ function. Use a 50x50 grid of points to make the plot.

(Hint: in discussion last week you used matplotlib to make a density plot of escape rates for quadratic polynomials of a complex variable in order to visualize Julia sets. That code might be a good starting place for your work here, but this question doesn't use complex numbers at all.)

Upload two files for this problem:

  • quiz11prob3.py: The script you wrote to make the plot
  • quiz11prob3.png or quiz11prob3.jpg: An image file that shows the plot produced by your script, which can be saved by matplotlib or captured as a screenshot.

Note: The image and script you submit must match, i.e. the image must be one that was created with the exact script you submit.

In [7]:
# MCS 275 Quiz 11 Problem 3
# J. Vaccaro
# I completed this work myself, and used worksheet 11 solutions as reference.
"""Plot the zebra function on the domain x=[0,5] and y=[0,5] and save as a png"""

import matplotlib.pyplot as plt
import numpy as np

# Create the numpy arrays with resolution 50
x = np.linspace(0,5,50)
y = np.linspace(0,5,50)
xx,yy = np.meshgrid(x,y)
zz = xx*yy - xx*xx

# Create the zebra array zz
for _ in range(100):
    zz = np.sin(zz)

# Create the figure/image with a colorbar
plt.figure(figsize=(8,8))
plt.imshow(zz,extent=(0,5,0,5),origin="lower",cmap="inferno")
plt.colorbar()

# Save the image
plt.savefig("quiz11prob3.png")