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 [2]:
fig, ax = plt.subplots(figsize=(9,6))
ax.scatter(np.random.randn(20), np.random.randn(20))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.xlabel('Random y');
plt.ylabel('Random x');
plt.title('Plot of a Random (x,y)');


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 [8]:
fig, ax = plt.subplots(figsize=(9,6));
ax.hist(np.random.randn(9001),bins=20,align='mid',histtype='stepfilled');
plt.xlabel('Random Number');
plt.ylabel('Occurences');
plt.title('Histogram for a set of random numbers');
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')


I worked with Hunter Thomas for this exercise