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 [28]:
z=np.random.randn(2,10)
x=z[0,:]
x
y=z[1.:]
y

plt.scatter(x, y, s=60)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y vs x')
plt.tight_layout()
plt.grid(True)
plt.yticks([-4,-2,0,2,4], [-4,-2,0,2,4])
plt.ylim(-5, 5)


Out[28]:
(-5, 5)

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

plt.hist(f, bins=3, color='r')
plt.xlabel('x')
plt.ylabel('y')
plt.tick_params(length=0)
plt.title('y vs x')


Out[48]:
<matplotlib.text.Text at 0x7f72c253a790>

In [ ]: