Graphics and Equation Typesetting Exercises

Some of these questions come from materials prepared by Matt Moelter and Jodi Christiansen for PHYS 202 at Cal Poly. Others are from the Computational Physics book by Mark Newman at University of Michigan.


Instructions: Create a new notebook called GraphicsExercises in your Graphics directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.

Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title Graphics Exercises. Then include the inline functions and 3D graphics libraries by adding a code cell that invokes the %pylab inline magic and imports the needed packages.



In [ ]:
%pylab inline
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm

Question 1

Have a look at the Matplotlib gallery, find a cool looking figure, use the %loadpy magic we used before to copy the code into the box below, and execute it. Note that some of the examples might require packages that are not installed on your machine (in particular those that make maps) - if this is the case, pick another example for the purposes of this exercise.

Type %loadpy and then the URL of the py file containing the code, and it will automatically copy it into a cell below. Run the cell with the code to see the figure.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 2

Download the linked file called sunspots.txt, which contains the observed number of sunspots on the Sun for each month since January 1749. The file contains two columns of numbers, the first being the month and the second being the sunspot number.

(See note below$^*$ for how to download it.)


(a) Write some code that reads in the data and makes a graph of sunspots as a function of time. Format the x-axis labels to show every 25th year starting with 1750 e.g. (1750, 1775, 1800, 1825, 1850, etc.), instead of sunspot number. Be sure to think about how to make this graph publication quality, i.e. it should have labels, a legend, and customized (i.e. NOT default) color and data representation choices.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now add code to calculate the running average of the data. Plot both the original data and the running average on the same graph, only over the range covered by the first 1000 data points.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 3

Download the linked file called stars.txt, which contains the temperatures and magnitudes of several thousand stars in the Milky Way near our solar system. You will use this data to construct a scatter plot of stellar magnitude (logarithm of brightness) vs. temperature (in Kelvin), called a Hertzsprung-Russell diagram, after the astronomers who first plotted such data this way. The diagram is one of the fundamental tools of stellar astrophysics. For historical reasons, H-R diagrams are normally plotted with both the magnitude and temperature axes decreasing to the right/up rather than increasing.

(a) Read in the data from stars.txt and plot the data as is, with magnitude and temperature increasing in the usual way. Add labels and format your graph as publication quality.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now use the xlim() and ylim() commands to flip your graph so that it is a correct H-R diagram. There are several regions of the diagram where stars seem to cluster. Have a look at the wikipedia entry on H-R diagrams to find out what each of these regions are called and add some annotations to your graph to identify them.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 4

Make a 3D plot of the function $G(x, y) = e^{(−x^2−y^2)}$ over the range -6 $< (x, y) <$ 6 using 51 bins in $x$ and 51 bins in $y$.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 5

Determine the electric potential for a quadrupole consisting of

$q_1$ = -1.0 nC at (-2 m, 0 m)
$q_2$ = -1.0 nC at (+2 m, 0 m)
$q_3$ = +1.0 nC at (0 m, -2 m)
$q_4$ = +1.0 nC at (0 m, +2 m)


(a) Make a surface plot on the range -4 $ <(x,y)< $ 4 using 101 bins in $x$ and 101 bins in $y$. In this case, do not let $k$ = $q$ = $d$ = 1. Use dimensionful quantities and compute the value of the potential in volts.

Recall $k$ = 8.9875517873681764 $\times$ 10$^9$ N$\cdot$ m$^2$/C$^2$.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now make a contour plot of the same data, with appropriate contour levels to see the shape of the potential.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 6

Find a juicy equation in a textbook from one of your classes and use $\LaTeX$ to typeset it in a markdown cell. Include an explanation of what the equation is for.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

$^*$To save plain text files from the internet, you must explicitly save the page source. How you do this depends on the browser you are using. Here are a few examples. If you don’t save the file as plain text, the python interpreter won’t be able to read your file and load the data.

  • Chrome: Click on the filename. It will open the file in another tab. In the File menu, click “Save Page As...”. Click on the arrow button next to the “Save As:” field to expand the pane. Uncheck the “Hide extension” checkbox and click “Save”.
  • Firefox: Click on the filename. It will open the file in another tab. In the File menu, click “Save Page As...”. Be sure the format is “Plain text” and click “Save”.
  • Safari: Click on the filename. It will open the file in another tab. In the File menu, click “Save As...”. Be sure the format says “Page Source” and uncheck the “Hide extension” checkbox and click “Save”.

All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.