There are a few ways to make small multiples using pandas/matplotlib.
We’ve been using plt.subplots so far to yell at matplotlib, “hey, prepare a
graph!”. Then when we use df.plot we pass ax to put all of our data into
that one particular graph.
<matplotlib.text.Text at 0x10b0d04a8>
Passing ax around
If we use .plot twice but give them both the same ax, the elements will be
plotted on the same graph.
<matplotlib.text.Text at 0x10d3d8e48>
Having multiple ax
We can receive multiple ax elements from .subplots. Below we’re using
nrows= and ncols to ask for two rows of graphics, each row having one
column.
Note: The next one is nicer than this one because it shares x and y axes.
See how it looks like they’re both making a lot of money in the end?
Unfortunately that’s not true. If you look at the y-axis labels, you’ll see
Iran peaks at around a GDP of $13k Bhutan only gets up to about $6k. In order
to make the x and y axes match up, you need to pass sharex and sharey to
your plt.subplots.
Expanding with nrows and ncols
You could do this with a million different graphics!
Simplifying
That’s a little too complicated for my tastes, though. How are you going to get
all of those into a loop? Short answer: you aren’t. Let’s try it a different
way.
Instead of getting all of the subplots at once, we’ll get them one at a time by
using plt.subplot, the singular version of plt.subplots.
<matplotlib.text.Text at 0x10d567f60>
<matplotlib.text.Text at 0x10d2edf60>
<matplotlib.text.Text at 0x10dde6588>
Make it a loop
188
So we need 188 different graphs. If we put 15 columns on each row, that’s 12.53
rows - round that up to 13.