Create subtitles in matplotlib in a loop?

I use this code that provides good graphics one by one (using IPython-notebook and Pandas)

for subsm in subsl: H7, subsm = sumsubdesc2(table, subsm) ax1=H7.plot() plt.title('Rolling 4q mean %s'%(subsm)) ax1.set_title('Rolling 4q mean %s'%(subsm)) ax1.set_ylim(100000,600000) 

I would like to get the β€œ2up” graphs next to the next for 3 rows (5 subtitles) that cannot figure out how to handle this, since all the examples of the subtitle seem to be designed to subtitle the ether with data or specific graphs and specific placement the grid.

So, I don’t know how to create the main plot, and then put a few graphs (in this case 5) with headings like two-up?

Edit the two lines of code since I left the function call; - (

+4
source share
2 answers

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.

+6
source

I'm not sure, but I think you are asking:

 # not tested import math import matplotlib.pylab as plt Nrows = math.ceil(len(subsl) / 2.) for i in range(len(subsl)): subsm = subsl[i] H7, subsm = sumsubdesc2(table, subsm) plt.subplot(Nrows, 2, i+1) # do some plotting plt.title('Rolling 4q mean %s'%(subsm)) 

I'm not sure what you mean by "headings like two."

+1
source

All Articles