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 [31]:
x=np.random.randn(22)
y=np.random.randn(22)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter')
plt.scatter(x,y,s=22.0,c='g',marker='x',alpha=.7,linewidths=2.2)
#plt.xlim(0,1)
#plt.ylim()


Out[31]:
<matplotlib.collections.PathCollection at 0x7f7f11cdf1d0>

Histogram

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

  • Generate random 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 [34]:
data=np.random.randn(22)
plt.hist(data)


Out[34]:
(array([ 1.,  0.,  2.,  1.,  3.,  1.,  3.,  4.,  4.,  3.]),
 array([-3.06904602, -2.59684524, -2.12464446, -1.65244368, -1.1802429 ,
        -0.70804212, -0.23584134,  0.23635944,  0.70856022,  1.18076101,
         1.65296179]),
 <a list of 10 Patch objects>)