Matplotlib Legend Position

Matplotlib Legend Position

In Matplotlib, the legend is used to label different plotted data. It provides information about the data being visualized. The position of the legend in a plot is important for effective data communication. In this article, we will explore different ways to adjust the position of the legend in a Matplotlib plot.

Default Legend Position

When you plot a graph in Matplotlib, the legend is placed by default in the best location to avoid overlapping with the plot itself. Let’s create a simple plot to see the default legend position.

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend()
plt.show()

Output:

Matplotlib Legend Position

In the example above, the legend is automatically placed in the upper right corner of the plot. Matplotlib tries to find the best position for the legend to avoid obstructing the data.

Changing Legend Position

If you want to manually adjust the position of the legend in a plot, you can do so by using the loc parameter in the plt.legend() function. The loc parameter takes a string or an integer value to specify the desired legend position.

Here are some common values for the loc parameter and their corresponding legend positions:

  • ‘best’ (0)
  • ‘upper right’ (1)
  • ‘upper left’ (2)
  • ‘lower left’ (3)
  • ‘lower right’ (4)
  • ‘right’ (5)
  • ‘center left’ (6)
  • ‘center right’ (7)
  • ‘lower center’ (8)
  • ‘upper center’ (9)
  • ‘center’ (10)

Let’s see examples of how to use these values to change the legend position:

Example 1: Legend in the upper left corner

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper left')
plt.show()

Output:

Matplotlib Legend Position

Example 2: Legend in the lower right corner

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='lower right')
plt.show()

Output:

Matplotlib Legend Position

Example 3: Legend in the center

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='center')
plt.show()

Output:

Matplotlib Legend Position

Customizing Legend Box

Apart from changing the position of the legend, you can also customize the appearance of the legend box. You can adjust attributes such as border color, background color, font size, and shadow.

Example 4: Customize border color and background color of the legend box

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper right', edgecolor='black', facecolor='lightgrey')
plt.show()

Output:

Matplotlib Legend Position

Example 5: Change font size of the legend text

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper right', fontsize='large')
plt.show()

Output:

Matplotlib Legend Position

Example 6: Add shadow to the legend box

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper right', shadow=True)
plt.show()

Output:

Matplotlib Legend Position

Legend Outside the Plot Area

Sometimes you may want to place the legend outside the plot area to prevent it from overlapping with the data. You can achieve this by using the bbox_to_anchor parameter along with the loc parameter.

Example 7: Place legend outside plot area to the right

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper left', bbox_to_anchor=(1.05, 1))
plt.show()

Output:

Matplotlib Legend Position

Example 8: Place legend outside plot area to the bottom

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper left', bbox_to_anchor=(0, -0.2))
plt.show()

Output:

Matplotlib Legend Position

Legend Title

You can also add a title to the legend to provide additional context about the data being visualized. This can be done using the title parameter in the plt.legend() function.

Example 9: Add a title to the legend

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(title='Functions')
plt.show()

Output:

Matplotlib Legend Position

Custom Legend Labels

If you want to customize the labels in the legend, you can do so by passing a list of strings to the plt.legend() function.

Example 10: Custom legend labels

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1)
plt.plot(x, y2)
plt.legend(['Quadratic', 'Cubic'])
plt.show()

Output:

Matplotlib Legend Position

Multiple Legends

In some cases, you may need to display multiple legends in a single plot to label different aspects of the data. This can be achieved by calling the plt.legend() function multiple times.

Example 11: Multiple legends

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1, label='y = x^2')
plt.plot(x, y2, label='y = x^3')
plt.legend(loc='upper left')
plt.legend(['Quadratic', 'Cubic'], loc='lower right')
plt.show()

Output:

Matplotlib Legend Position

Removing the Legend

If you no longer need the legend in your plot, you can remove it by calling the plt.legend() function with no arguments.

Example 12: Remove the legend

import matplotlib.pyplot as plt

x = range(10)
y1 = [i**2 for i in x]
y2 = [i**3 for i in x]

plt.plot(x, y1)
plt.plot(x, y2)
plt.legend()
plt.show()

Matplotlib Legend Position Conclusion

In this article, we have explored various ways to adjust the position of the legend in a Matplotlib plot. By changing the legend position, customizing the legend box, and adding titles to the legend, you can effectively communicate the data in your visualizations. Experiment with different legend settings to find the most suitable configuration for your plots.

Like(0)