Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Position legend optimally in Matplotlib

📅 2012-Oct-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

By default, Matplotlib places the legend at the top-right corner of the plot. This might block some of the lines or bars of the plot.

To make Matplotlib automatically choose an optimal position for the legend such that it does not block anything in the plot, do this:

matplotlib.pyplot.legend( ..., loc="best" )

Tried with: Matplotlib 1.2, Python 3.2.3 and Windows 7 x64