Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of ways to do what you want. To add to what @inalis and @Navi already said, you can use the <code>bbox_to_anchor</code> keyword argument to place the legend partially outside the axes and/or decrease the font size. </p> <p>Before you consider decreasing the font size (which can make things awfully hard to read), try playing around with placing the legend in different places:</p> <p>So, let's start with a generic example:</p> <pre><code>import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in xrange(5): ax.plot(x, i * x, label='$y = %ix$' % i) ax.legend() plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/LQ8xk.png" alt="alt text"></p> <p>If we do the same thing, but use the <code>bbox_to_anchor</code> keyword argument we can shift the legend slightly outside the axes boundaries:</p> <pre><code>import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in xrange(5): ax.plot(x, i * x, label='$y = %ix$' % i) ax.legend(bbox_to_anchor=(1.1, 1.05)) plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/OtE5U.png" alt="alt text"></p> <p>Similarly, you can make the legend more horizontal and/or put it at the top of the figure (I'm also turning on rounded corners and a simple drop shadow):</p> <pre><code>import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in xrange(5): line, = ax.plot(x, i * x, label='$y = %ix$'%i) ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3, fancybox=True, shadow=True) plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/zgtBl.png" alt="alt text"></p> <p>Alternatively, you can shrink the current plot's width, and put the legend entirely outside the axis of the figure:</p> <pre><code>import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in xrange(5): ax.plot(x, i * x, label='$y = %ix$'%i) # Shrink current axis by 20% box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/v34g8.png" alt="alt text"></p> <p>And in a similar manner, you can shrink the plot vertically, and put the a horizontal legend at the bottom:</p> <pre><code>import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in xrange(5): line, = ax.plot(x, i * x, label='$y = %ix$'%i) # Shrink current axis's height by 10% on the bottom box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) # Put a legend below current axis ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=5) plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/cXcYa.png" alt="alt text"></p> <p>Have a look at the <a href="http://matplotlib.org/users/legend_guide.html#legend-location" rel="noreferrer">matplotlib legend guide</a>. You might also take a look at <a href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.figlegend" rel="noreferrer"><code>plt.figlegend()</code></a>. Hope that helps a bit, anyway!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload