Visualization 1: Matplotlib Basics Exercises


In [30]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from __future__ import print_function
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets


:0: FutureWarning: IPython widgets are experimental and may change in the future.

Scatter plots

Learn how to use Matplotlib's plt.scatter function to make a 2d scatter plot.

  • Generate random data using np.random.randn.
  • Style the markers (color, size, shape, alpha) appropriately.
  • Include an x and y label and title.

In [21]:
randx = np.random.randn(500)
randy = np.random.randn(500)
plt.scatter(randx, randy, color = "g", marker = "x")
plt.xlabel("Random X")
plt.ylabel("Random Y")
plt.title("Random Data!!!!!")
plt.box(False)
plt.grid(True)


Histogram

Learn how to use Matplotlib's plt.hist function to make a 1d histogram.

  • Generate randpom data using np.random.randn.
  • Figure out how to set the number of histogram bins and other style options.
  • Include an x and y label and title.

In [41]:
data = np.random.randn(500000)
def plothist(bins, numdata):
    plt.hist(np.random.randn(numdata), bins=bins, color = "k", ec = "w")
interact(plothist, bins=widgets.IntSlider(min=1,max=100,step=1,value=10), numdata=\
         widgets.IntSlider(min=10,max=10000,step=10,value=10));
plt.xlabel("Random Variable X")
plt.ylabel("Counts")
plt.title("Distribution of a random variable in abjustable bins.")



In [ ]: