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.
I take it back
Maybe the best way to do this is actually to use subplots! With the sharex and
sharey it certainly seems more effective. We’ll just need a way to pull off
one subplot at a time, instead of doing a huge big ((ax1, ax...)) disaster.
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10fc3dda0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10ff06b70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10ff8e2e8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x10ffc6ba8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x110115b00>,
<matplotlib.axes._subplots.AxesSubplot object at 0x1101545f8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x11019c390>,
<matplotlib.axes._subplots.AxesSubplot object at 0x1101ac160>,
<matplotlib.axes._subplots.AxesSubplot object at 0x110228828>]], dtype=object)
Right now it’s 3 lists of 3 axes, which will be hard to loop over.
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x10fc3dda0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10ff06b70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10ff8e2e8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x10ffc6ba8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x110115b00>,
<matplotlib.axes._subplots.AxesSubplot object at 0x1101545f8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x11019c390>,
<matplotlib.axes._subplots.AxesSubplot object at 0x1101ac160>,
<matplotlib.axes._subplots.AxesSubplot object at 0x110228828>]], dtype=object)
Luckily we can easily convert it to just one long list using a weird list
comprehension
[<matplotlib.axes._subplots.AxesSubplot at 0x10fc3dda0>,
<matplotlib.axes._subplots.AxesSubplot at 0x10ff06b70>,
<matplotlib.axes._subplots.AxesSubplot at 0x10ff8e2e8>,
<matplotlib.axes._subplots.AxesSubplot at 0x10ffc6ba8>,
<matplotlib.axes._subplots.AxesSubplot at 0x110115b00>,
<matplotlib.axes._subplots.AxesSubplot at 0x1101545f8>,
<matplotlib.axes._subplots.AxesSubplot at 0x11019c390>,
<matplotlib.axes._subplots.AxesSubplot at 0x1101ac160>,
<matplotlib.axes._subplots.AxesSubplot at 0x110228828>]
And take parts off one at a time
9
<matplotlib.axes._subplots.AxesSubplot at 0x10fc3dda0>
8
<matplotlib.axes._subplots.AxesSubplot at 0x10ff06b70>
7
And we can just keep on doing down, plucking them off one at a time.
Putting it together
Let’s put them in order
Country
Austria 36731.628770
Australia 36064.737280
Belgium 32585.011970
Bahrain 24472.896240
Bahamas 21418.229810
Barbados 16233.413790
Argentina 15714.103180
Antigua and Barbuda 13722.986160
Belarus 13515.161030
Azerbaijan 9291.026270
Belize 7550.138241
Albania 6969.306283
Algeria 6419.127829
Bhutan 6130.862355
Angola 5838.155376
Armenia 5059.087964
Bolivia 2677.326347
Bangladesh 1792.550235
Benin 1464.138255
Afghanistan 1349.696941
Name: GDP_per_capita, dtype: float64
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ipykernel/__main__.py:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
if __name__ == '__main__':
Country
Continent
Year
GDP_per_capita
life_expectancy
Population
1952
Cape Verde
Africa
2012
3896.041139
74.771
505335
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ipykernel/__main__.py:3: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
app.launch_new_instance()
<matplotlib.text.Annotation at 0x111e4b240>
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ipykernel/__main__.py:38: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
Want to hear when I release new things? My infrequent and sporadic newsletter can help with that.