matplotlib exercises


In [ ]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

Q1: planetary positions

The distances of the planets from the Sun (technically, their semi-major axes) are:


In [ ]:
a = np.array([0.39, 0.72, 1.00, 1.52, 5.20, 9.54, 19.22, 30.06, 39.48])

These are in units where the Earth-Sun distance is 1 (astronomical units).

The corresponding periods of their orbits (how long they take to go once around the Sun) are, in years


In [ ]:
P = np.array([0.24, 0.62, 1.00, 1.88, 11.86, 29.46, 84.01, 164.8, 248.09])

Finally, the names of the planets corresponding to these are:


In [ ]:
names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", 
         "Uranus", "Neptune", "Pluto"]

(technically, pluto isn't a planet anymore, but we still love it :)

  • Plot as points, the periods vs. distances for each planet on a log-log plot.

  • Write the name of the planet next to the point for that planet on the plot


In [ ]:

Q2: drawing a circle

For an angle $\theta$ in the range $\theta \in [0, 2\pi]$, the polar equations of a circle of radius $R$ are: $$ x = R\cos(\theta) $$ $$ y = R\sin(\theta) $$

We want to draw a circle.

  • Create an array to hold the theta values—the more we use, the smoother the circle will be
  • Create x and y arrays from theta for your choice of $R$
  • Plot y vs. x

Now, look up the matplotlib fill() function, and draw a circle filled in with a solid color.


In [ ]:

Q3: Circles, circles, circles...

Generalize your circle drawing commands to produce a function,

draw_circle(x0, y0, R, color)

that draws the circle. Here, (x0, y0) is the center of the circle, R is the radius, and color is the color of the circle.

Now randomly draw 10 circles at different locations, with random radii, and random colors on the same plot.


In [ ]:

Q4: Climate

Download the data file of global surface air temperature averages from here: https://raw.githubusercontent.com/sbu-python-summer/python-tutorial/master/day-4/nasa-giss.txt

(this data comes from: https://data.giss.nasa.gov/gistemp/graphs/)

There are 3 columns here: the year, the temperature change, and a smoothed representation of the temperature change.

  • Read in this data using np.loadtxt().
  • Plot as a line the smoothed representation of the temperature changes.
  • Plot as points the temperature change (no smoothing). Color the points blue if they are < 0 and color them red if they are >= 0

You might find the NumPy where() function useful.


In [ ]:

Q5: subplots

matplotlib has a number of ways to create multiple axes in a figure -- look at plt.subplot() (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot)

Create an x array using NumPy with a number of points, spanning from $[0, 2\pi]$.

Create 3 axes vertically, and do the following:

  • Define a new numpy array f initialized to a function of your choice.
  • Plot f in the top axes
  • Compute a numerical derivative of f, $$ f' = \frac{f_{i+1} - f_i}{\Delta x}$$ and plot this in the middle axes
  • Do this again, this time on $f'$ to compute the second derivative and plot that in the bottom axes

In [ ]:

Q6: frequent words plotting

In this exercise, we will read the file with the transcription of Star Trek TOS, Shore Leave and calculate the amount of time each word was found. We will then plot the 25 most frequent words and label the plot.

6.1 Read the file and create the dictionaty {'word':count}

  • Open the shore_leave.txt
  • Create the dictionary of the form {'word':count}, where count shows the amount of times the word was found in the text. Remember to get rid of the punctuation ("." and ",") and to ensure that all words are lowercase

In [ ]:
f = open("shore_leave.txt", "r")

for line in f:
    pass

2. Plot 25 most frequent words

Plot a labelled bar chart of the most frequent 25 words with their frequencies.


In [1]:
# your code here

In [ ]: