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 [61]:
np.random.seed(33)
plt.figure(figsize=(6,6))
x = np.random.randn(50)
y = np.random.randn(50) + 2 * x
plt.scatter(x,y, c='r', marker='o', alpha=.3, s = 100)
plt.xlabel("Random x value")
plt.ylabel("Random y value")
plt.title("Random Scatter Plot")


Out[61]:
<matplotlib.text.Text at 0x7f550e95a250>

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 [59]:
np.random.seed(43)
plt.figure(figsize=(8,4))
x = np.log(np.random.rand(500))
plt.hist(x, alpha = .3, color = 'yellow', normed=True)
plt.xlabel("Log of Random Uniform")
plt.ylabel("Density")
plt.title("Example histogram")


Out[59]:
<matplotlib.text.Text at 0x7f550f021a90>

In [ ]: