title: Control Matplotlib legend box width for wrapping
body: <p>I am trying to fix the width of the legend box in Matplotlib (Python 2.7). Here is a simple example of the code I am using:</p>
  
  <pre><code>import numpy as np
  import pylab as plt
  fig = plt.figure(figsize=(8.5, 8.5))
  ax = fig.add_axes([0.0882, 0.0588, 0.8882, 0.9118])
  
  x = np.array([0,1,2,33])
  y = np.array([0.1,2,4,8])
  z = np.array([0,1,3.7,7])
  t = np.array([0.5,1,12,41])
  v = np.array([0.9,7,24,54])
  a = np.array([0.2,11,17,61])
  q = np.array([0.4,17,15,80])
  r = np.array([0.9,3.7,18,44])
  s = np.array([0.2,10,19,31])
  
  y1 = y+1
  z1 = z+1
  t1 = t+1
  v1 = v+1
  a1 = a+1
  q1 = q+1
  r1 = r+1
  s1 = s+1
  
  ax.plot(x,y,label='y')
  ax.plot(x,z,label='z')
  ax.plot(x,t,label='t')
  ax.plot(x,v,label='v')
  ax.plot(x,a,label='a')
  ax.plot(x,y1,label='y1')
  ax.plot(x,z1,label='z1')
  ax.plot(x,t1,label='t1')
  ax.plot(x,v1,label='v1')
  ax.plot(x,a1,label='a1')
  ax.plot(x,q1,label='q1')
  ax.plot(x,r1,label='r1')
  ax.plot(x,s1,label='s1')
  
  lg = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=10)
  
  fig.savefig('A_test_position.png', facecolor='white', dpi = 300, bbox_inches='tight')
  </code></pre>
  
  <p>When I only keep the first 3 <code>ax.plot()</code> statements, I get <a href="https://i.stack.imgur.com/c1zRI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c1zRI.png" alt="this"></a>. However, when I include all the <code>ax.plot()</code> statements, the output becomes <a href="https://i.stack.imgur.com/KvEj7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KvEj7.png" alt="this"></a></p>
  
  <p><strong>Problem:</strong>
  I need to get the legend box to:</p>
  
  <ol>
  <li>start at 0 on the x-axis</li>
  <li>end at the maximum x-value on the x-axis (in this case, the max value would be 35)</li>
  <li>wrap to a new row after it reaches the maximum on the x-axis</li>
  </ol>
  
  <p>When there are a small number of entries (eg. first plot), then I want the legend to be centered. Currently my code is working correctly for this, so I do not need to change this behavior. However, I need to change the behavior when there are so many entries that the legend needs to wrap around to a new row (2nd plot).</p>
  
  <p>How can this be done in Matplotlib? Is there a way to specify the size of the legend box?</p>