MatplotlibExercises


Visualization 1: Matplotlib Basics Exercises


In [48]:
%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 [65]:
x = np.random.randn(100)
y = np.random.randn(100)
plt.scatter(x,y, s = 20, c = 'b')
plt.xlabel('Random Number 2')
plt.ylabel('Random Number')
plt.title('Random 2d Scatter Plot')
axis = plt.gca()
axis.spines['top'].set_visible(False)
axis.spines['right'].set_visible(False)
axis.get_xaxis().tick_bottom()
axis.get_yaxis().tick_left()
plt.tight_layout()


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 [66]:
x = np.random.randn(10)

plt.hist(x,4)
plt.xlabel('X value')
plt.ylabel('Y value')
plt.title('Random Histogram Bins')


Out[66]:
<matplotlib.text.Text at 0x7f31cc0844d0>

In [ ]: