Matplotlib Exercise 2

Imports


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

Exoplanet properties

Over the past few decades, astronomers have discovered thousands of extrasolar planets. The following paper describes the properties of some of these planets.

http://iopscience.iop.org/1402-4896/2008/T130/014001

Your job is to reproduce Figures 2 and 4 from this paper using an up-to-date dataset of extrasolar planets found on this GitHub repo:

https://github.com/OpenExoplanetCatalogue/open_exoplanet_catalogue

A text version of the dataset has already been put into this directory. The top of the file has documentation about each column of data:


In [2]:
!head -n 30 open_exoplanet_catalogue.txt































Use np.genfromtxt with a delimiter of ',' to read the data into a NumPy array called data:


In [3]:
data = np.genfromtxt("open_exoplanet_catalogue.txt", delimiter = ',')
data[0:20,2]
#raise NotImplementedError()


Out[3]:
array([  1.40000000e-03,              nan,              nan,
         1.41900591e-01,   9.75369917e-02,   2.20000000e-02,
         6.90000000e+00,   1.70000000e-02,   1.24000000e+00,
         1.95000000e-02,              nan,              nan,
         1.82000000e+00,   2.17000000e+00,   8.00000000e-02,
         5.20000000e-01,   1.99000000e+00,              nan,
                    nan,              nan])

In [4]:
assert data.shape==(1993,24)

Make a histogram of the distribution of planetary masses. This will reproduce Figure 2 in the original paper.

  • Customize your plot to follow Tufte's principles of visualizations.
  • Customize the box, grid, spines and ticks to match the requirements of this data.
  • Pick the number of bins for the histogram appropriately.

In [10]:
clean = np.array([x for x in data[:,2] if not math.isnan(x)])

plt.hist(clean, range = (0,14), bins = 50)
plt.xlabel("Planetary masses (Jupiter masses)")
plt.ylabel("Frequency (Number of Planets)")
plt.title("Histogram of Exoplanet Masses")

#raise NotImplementedError()


Out[10]:
<matplotlib.text.Text at 0x7f50df501b70>

In [6]:
assert True # leave for grading

Make a scatter plot of the orbital eccentricity (y) versus the semimajor axis. This will reproduce Figure 4 of the original paper. Use a log scale on the x axis.

  • Customize your plot to follow Tufte's principles of visualizations.
  • Customize the box, grid, spines and ticks to match the requirements of this data.

In [7]:
plt.scatter([math.log(x) for x in data[:,5]], data[:,6])
plt.xlabel("Semimajor Axis")
plt.ylabel("Orbital Eccentricity")
plt.title("Eccentricity vs. Semimajor Axis")
#raise NotImplementedError()


Out[7]:
<matplotlib.text.Text at 0x7f50df7d90f0>

In [8]:
assert True # leave for grading