title: Matplotlib automatic legend outside plot
body: <p>I am trying to use the keyword <code>bbox_to_anchor()</code> in a matplotlib plot in Python.</p>
  
  <p>Here is  a very basic plot that I have produced <a href="http://matplotlib.org/users/legend_guide.html#plotting-guide-legend" rel="noreferrer">based on this example</a>. :</p>
  
  <pre><code>import matplotlib.pyplot as plt
  x = [1,2,3]
  plt.subplot(211)
  plt.plot(x, label="test1")
  plt.plot([3,2,1], label="test2")
  plt.legend(bbox_to_anchor=(0, -0.15, 1, 0), loc=2, ncol=2, mode="expand", borderaxespad=0)
  plt.show()
  </code></pre>
  
  <p>I am trying to automatically place the legend outside the plot using <code>bbox_to_anchor()</code>. In this example, <code>bbox_to_anchor()</code> has 4 arguments listed.</p>
  
  <p>In this particular example (above), the legend is placed below the plot so the number -0.15 needs to be manually entered each time a plot is changed (font size, axis title removed, etc.).
  <strong>Is it possible to automatically calculate these 4 numbers for the following scenarios?</strong>:</p>
  
  <ol>
  <li>legend below plot</li>
  <li>legend above plot</li>
  <li>legend to right of plot</li>
  </ol>
  
  <p><strong>If not, is it possible to make good guesses about these numbers, in Python?</strong></p>
  
  <p>Also, in the example code above I have set the last 2 numbers in <code>bbox_to_anchor()</code> to 1 and 0 since I do not understand what they are or how they work. <strong>What do the last 2 numbers in <code>bbox_to_anchor()</code> mean?</strong></p>