Here is what you need to do:
import math import matplotlib.pylab as plt nrows = int(math.ceil(len(subsl) / 2.)) fig, axs = plt.subplots(nrows, 2) ylim = 100000, 600000 for ax, subsm in zip(axs.flat, subsl): H7, subsm = sumsubdesc2(table, subsm) H7.plot(ax=ax, title='Rolling 4q mean %s' % subsm) ax.set_ylim(ylim)
This will work even if axs.size > len(subsl)
, since StopIteration
occurs when the shortest iterative ends. Note that axs.flat
is an iterator compared to the axs
array sorted by line by line.
To hide the last chart that is not displayed, do the following:
axs.flat[-1].set_visible(False)
In general, for axs.size - len(subsl)
additional graphs at the end of the grid, do:
for ax in axs.flat[axs.size - 1:len(subsl) - 1:-1]: ax.set_visible(False)
This snippet looks a little rude, so I will explain:
The axs
array has axs
elements. The index of the last element of the flattened version of axs
is axs.size - 1
. subsl
has len(subsl)
elements, and the same reasoning applies to the index of the last element. But we need to return from the last axs
element to the last element drawn, so we need to take a -1 step.
source share