PYT-DS: More Plotting in Matplotlib

Lets keep revealing the mysteries of plotting with matplotlib.

I'm still following VanderPlas closely as I think his materials are top notch. They're also freely available as open source products. Avail yourselves! Take advantage of what has been accomplished to make your lives easier!

Links:


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white') # didn't have to import seaborn
import numpy as np

In [ ]:
fig, ax_array = plt.subplots(2, 3, sharex='col', sharey='row') # ta da!

The axes show up in an array this time. We cycle through them...


In [ ]:
# axes are in a two-dimensional array, indexed by [row, col]
for i in range(2):
    for j in range(3):
        ax_array[i, j].text(0.5, 0.5, str((i, j)),
                      fontsize=18, ha='center')
fig

In [ ]:
grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)

In [ ]:
plt.subplot(grid[0, 0])
plt.subplot(grid[0, 1:])
plt.subplot(grid[1, :2])
plt.subplot(grid[1, 2]);

There's lots going on in this VanderPlas plot below, a practical application of Gridspec.

Gridspec gets the ball rollowing, at which point we create three subplots and name the axes.

Notice the shared axes.

numpy does the heavy lifting, in terms of providing 3000 points with a given mean and covariance. You might want to play around with these settings. Indeed, lets turn that into a....

LAB:

What does the cov argument do? Try making some other changes, in terms of colors, marker size, transparency... Get familiar with the bells and whistles, starting with something that already works!


In [ ]:
# Create some normally distributed data
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 3000).T

# Set up the axes with gridspec
fig = plt.figure(figsize=(6, 6))
grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)
main_ax = fig.add_subplot(grid[:-1, 1:])
y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)
x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)

# scatter points on the main axes
main_ax.plot(x, y, 'ok', markersize=3, alpha=0.2)

# histogram on the attached axes
x_hist.hist(x, 40, histtype='stepfilled',
            orientation='vertical', color='gray')
x_hist.invert_yaxis()

y_hist.hist(y, 40, histtype='stepfilled',
            orientation='horizontal', color='gray')
y_hist.invert_xaxis()