This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Tuesday, June 16, 2015

Python Legend, put legend outside of plot display

Often when you legend is too large, it is convenient to put it off to the side. Here's how...

from matplotlib import pyplot as plt

plt.plot(a,b, label="legend label")
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5),prop={'size':10})

An example here:

2 comments:

  1. It works... but if the label is too long, it gets cut. How do you increase the size of the region outside the frame, to make sure that does not happen?

    ReplyDelete
  2. Hmmm, I haven't run into that problem before. You may need to make the figure bigger, or use plt.subplots_adjust() to add space to one side.
    http://matplotlib.org/examples/pylab_examples/subplots_adjust.html

    ReplyDelete

Note: Only a member of this blog may post a comment.