Visualization 1: Matplotlib Basics Exercises


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

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 [41]:
scatx=np.random.rand(50)
scaty=np.random.randn(50)
f= plt.figure(figsize=(9,6))
plt.scatter(scatx,scaty,c=u'k',marker=u'o',alpha=1)
plt.xlabel('X')
plt.ylabel('Y')
plt.title("Scatter Plot of A Set of Random Data")


Out[41]:
<matplotlib.text.Text at 0x7fc38338cb90>

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 [35]:
x= np.random.rand(50)
plt.hist(x)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("One Dimensional Histogram of a Random Set of Data")
plt.hist(x,bins=50, normed=True, facecolor="r",stacked=False)


Out[35]:
(array([ 2.01763289,  0.        ,  1.00881645,  1.00881645,  1.00881645,
         5.04408224,  1.00881645,  2.01763289,  4.03526579,  1.00881645,
         1.00881645,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         2.01763289,  1.00881645,  0.        ,  2.01763289,  0.        ,
         2.01763289,  1.00881645,  2.01763289,  0.        ,  1.00881645,
         0.        ,  1.00881645,  2.01763289,  1.00881645,  1.00881645,
         1.00881645,  0.        ,  0.        ,  3.02644934,  1.00881645,
         2.01763289,  0.        ,  1.00881645,  0.        ,  3.02644934,
         0.        ,  2.01763289,  0.        ,  0.        ,  2.01763289]),
 array([ 0.00724915,  0.02707436,  0.04689957,  0.06672478,  0.08654999,
         0.10637521,  0.12620042,  0.14602563,  0.16585084,  0.18567606,
         0.20550127,  0.22532648,  0.24515169,  0.2649769 ,  0.28480212,
         0.30462733,  0.32445254,  0.34427775,  0.36410296,  0.38392818,
         0.40375339,  0.4235786 ,  0.44340381,  0.46322902,  0.48305424,
         0.50287945,  0.52270466,  0.54252987,  0.56235508,  0.5821803 ,
         0.60200551,  0.62183072,  0.64165593,  0.66148114,  0.68130636,
         0.70113157,  0.72095678,  0.74078199,  0.7606072 ,  0.78043242,
         0.80025763,  0.82008284,  0.83990805,  0.85973327,  0.87955848,
         0.89938369,  0.9192089 ,  0.93903411,  0.95885933,  0.97868454,
         0.99850975]),
 <a list of 50 Patch objects>)

In [ ]:


In [ ]: