Visualization 1: Matplotlib Basics Exercises


In [2]:
%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 [8]:
data = np.random.randn(2, 100)
plt.scatter(data[0], data[1])
plt.xlabel("Random Number 1", fontsize=12, color="#666666")
plt.ylabel("Random Number 2", fontsize=12, color="#666666")
ax = plt.gca()
ax.spines['right'].set_color("None")
ax.spines['top'].set_color("None")
ax.spines['bottom'].set_color("#666666")
ax.spines['left'].set_color("#666666")
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.title("2D Random Number Scatter Plot", color="#383838")


Out[8]:
<matplotlib.text.Text at 0x7f2028504290>

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 [9]:
data = np.random.randn(50)
plt.hist(data, 20, facecolor='green', )
plt.xlabel("Random Number", fontsize=12, color="#666666")
plt.ylabel("Probability", fontsize=12, color="#666666")
ax = plt.gca()
ax.spines['right'].set_color("None")
ax.spines['top'].set_color("None")
ax.spines['bottom'].set_color("#666666")
ax.spines['left'].set_color("#666666")
ax.tick_params(axis='x', colors='#666666')
ax.tick_params(axis='y', colors='#666666')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
plt.title("Random Number Histogram", color="#383838")


Out[9]:
<matplotlib.text.Text at 0x7f20284352d0>

In [ ]:
# used "Joe Kington"'s (from stackoverflow) method for setting axis tick and label colors
# used "timday"'s (from stackoverflow) method for hiding the top and right axis and ticks