title: Dynamically add subplots in matplotlib with more than one column
body: <p>How can I dynamically add a new plot to bunch of subplots if I'm using more than one column to display my subplots? <a href="https://stackoverflow.com/questions/12319796/dynamically-add-create-subplots-in-matplotlib">This</a> answers this question for one column, but I cant seem to modify the answers there to make it dynamically add to a subplot with <code>x</code> columns</p>
  
  <p>I modified <a href="https://stackoverflow.com/questions/12319796/dynamically-add-create-subplots-in-matplotlib">Sadarthrion's answer</a> and attempted the following. Here, for sake of an example, I made <code>number_of_subplots=11</code> and <code>num_cols = 3</code>. </p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  def plotSubplots(number_of_subplots,num_cols):
      # Start with one
      fig = plt.figure()
      ax = fig.add_subplot(111)
      ax.plot([1,2,3])
  
      for j in range(number_of_subplots):
          if j &gt; 0: 
              # Now later you get a new subplot; change the geometry of the existing
              n = len(fig.axes)
              for i in range(n):
                  fig.axes[i].change_geometry(n+1, num_cols, i+1)
  
              # Add the new
              ax = fig.add_subplot(n+1, 1, n+1)
              ax.plot([4,5,6])
  
              plt.show() 
  
     plotSubplots(11,3) 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/AGK3w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AGK3w.png" alt="enter image description here"></a></p>
  
  <p>As you can see this isn't giving me what I want. The first plot takes up all the columns and the additional plots are smaller than they should be</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><code>('2.7.6 | 64-bit | (default, Sep 15 2014, 17:36:35) [MSC v.1500 64 bit (AMD64)]'</code></p>
  
  <p>Also I have matplotlib version 1.4.3:</p>
  
  <pre><code>import matplotlib as mpl
  print mpl.__version__
  1.4.3
  </code></pre>
  
  <p>I tried Paul's answer below and get the following error message:</p>
  
  <pre><code>import math
  
  import matplotlib.pyplot as plt
  from matplotlib import gridspec
  
  def do_plot(ax):
      ax.plot([1,2,3], [4,5,6], 'k.')
  
  
  N = 11
  cols = 3
  rows = math.ceil(N / cols)
  
  gs = gridspec.GridSpec(rows, cols)
  fig = plt.figure()
  for n in range(N):
      ax = fig.add_subplot(gs[n])
      do_plot(ax)
  
  fig.tight_layout() 
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-1-f74203b1c1bf&gt; in &lt;module&gt;()
       15 fig = plt.figure()
       16 for n in range(N):
  ---&gt; 17     ax = fig.add_subplot(gs[n])
       18     do_plot(ax)
       19 
  
  C:\Users\user\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\figure.pyc in add_subplot(self, *args, **kwargs)
      962                     self._axstack.remove(ax)
      963 
  --&gt; 964             a = subplot_class_factory(projection_class)(self, *args, **kwargs)
      965 
      966         self._axstack.add(key, a)
  
  C:\Users\user\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_subplots.pyc in __init__(self, fig, *args, **kwargs)
       73             raise ValueError('Illegal argument(s) to subplot: %s' % (args,))
       74 
  ---&gt; 75         self.update_params()
       76 
       77         # _axes_class is set in the subplot_class_factory
  
  C:\Users\user\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_subplots.pyc in update_params(self)
      113         self.figbox, self.rowNum, self.colNum, self.numRows, self.numCols =     114             self.get_subplotspec().get_position(self.figure,
  --&gt; 115                                                 return_all=True)
      116 
      117     def is_first_col(self):
  
  C:\Users\user\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\gridspec.pyc in get_position(self, fig, return_all)
      423 
      424         figBottoms, figTops, figLefts, figRights = --&gt; 425                     gridspec.get_grid_positions(fig)
      426 
      427 
  
  C:\Users\user\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\gridspec.pyc in get_grid_positions(self, fig)
      103             cellHeights = [netHeight*r/tr for r in self._row_height_ratios]
      104         else:
  --&gt; 105             cellHeights = [cellH] * nrows
      106 
      107         sepHeights = [0] + ([sepH] * (nrows-1))
  
  TypeError: can't multiply sequence by non-int of type 'float' 
  </code></pre>