Visualization 1: Matplotlib Basics Exercises


In [31]:
%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 [15]:
datax = np.random.randn(10,5)
datay = np.random.randn(10,5)

In [29]:
fig = plt.figure(figsize=(8,5))
plt.scatter(datax, datay, marker = '.', alpha = 1, color = 'r')
plt.tick_params(top=False, right=False)
plt.title('Random Number Scatter Plot')
plt.xlabel('x')
plt.ylabel('y')


Out[29]:
<matplotlib.text.Text at 0x7f61c8e85d50>

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 [32]:
data = np.random.randn(10,5)

In [71]:
fig = plt.figure(figsize=(8,5))
plt.hist(data, bins = 5, histtype = 'bar', align = 'mid', range = (-3,2))
plt.tick_params(top=False, right=False)
plt.title('Random Number Histogram')
plt.xlabel('x')
plt.ylabel('y')
plt.xticks([-3,-2.5,-2,-1.5,-1,-.5,0,.5,1,1.5,2])


Out[71]:
([<matplotlib.axis.XTick at 0x7f61c5261750>,
  <matplotlib.axis.XTick at 0x7f61c52618d0>,
  <matplotlib.axis.XTick at 0x7f61c51cb910>,
  <matplotlib.axis.XTick at 0x7f61c515d6d0>,
  <matplotlib.axis.XTick at 0x7f61c515ddd0>,
  <matplotlib.axis.XTick at 0x7f61c50e9510>,
  <matplotlib.axis.XTick at 0x7f61c50e9c10>,
  <matplotlib.axis.XTick at 0x7f61c50f3350>,
  <matplotlib.axis.XTick at 0x7f61c50f3a50>,
  <matplotlib.axis.XTick at 0x7f61c50fd190>,
  <matplotlib.axis.XTick at 0x7f61c50fd890>],
 <a list of 11 Text xticklabel objects>)

In [ ]: