Matplotlib Legend Location

Matplotlib Legend Location

Matplotlib is a popular plotting library in Python that can be used to create a wide range of plots. One common feature in plots is the legend, which provides information about the data being displayed. In this article, we will explore how to customize the location of the legend in a matplotlib plot.

Basic Usage

The legend function in matplotlib is used to add a legend to a plot. By default, it tries to choose the best location for the legend. Here is an example of how to add a legend to a plot:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend()
plt.show()

Output:

Matplotlib Legend Location

This will create a simple plot with a legend at the best location.

Customizing Legend Location

If you want to customize the location of the legend, you can use the loc parameter in the legend function. The loc parameter takes a string or an integer that represents the location of the legend. Here are some common values for the loc parameter:

  • ‘best’: choose the best location automatically
  • ‘upper right’
  • ‘upper left’
  • ‘lower right’
  • ‘lower left’
  • ‘right’
  • ‘center left’
  • ‘center right’
  • ‘lower center’
  • ‘upper center’
  • ‘center’

Here is an example of how to set the legend location to the upper right corner:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='upper right')
plt.show()

Output:

Matplotlib Legend Location

Examples

Now let’s explore some examples of customizing the legend location in matplotlib plots.

Example 1: Upper Left Corner

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='upper left')
plt.show()

Output:

Matplotlib Legend Location

Example 2: Lower Right Corner

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='lower right')
plt.show()

Output:

Matplotlib Legend Location

Example 3: Right Side

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='right')
plt.show()

Output:

Matplotlib Legend Location

Example 4: Center Left

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='center left')
plt.show()

Output:

Matplotlib Legend Location

Example 5: Lower Center

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='lower center')
plt.show()

Output:

Matplotlib Legend Location

Example 6: Custom Location using Coordinates

You can also specify the location of the legend using coordinates. Here is an example of how to position the legend at a specific location using coordinates:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc=(0.5, 0.5))
plt.show()

Output:

Matplotlib Legend Location

Example 7: Outside Plot

If you want the legend to be outside the plot, you can use the bbox_to_anchor parameter. Here is an example of how to place the legend outside the plot:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()

Output:

Matplotlib Legend Location

Example 8: No Legend

If you want to remove the legend from the plot, you can simply omit the legend function. Here is an example without a legend:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.show()

Output:

Matplotlib Legend Location

Example 9: Legend Title

You can also add a title to the legend using the title parameter. Here is an example of adding a title to the legend:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend(title='Legend Title')
plt.show()

Output:

Matplotlib Legend Location

Example 10: Custom Legend Labels

If you want to customize the labels of the legend, you can pass a list of labels to the legend function. Here is an example of customizing the legend labels:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [10, 15, 13, 18, 16]
y2 = [8, 9, 11, 12, 14]

plt.plot(x, y1)
plt.plot(x, y2)
plt.legend(['Custom Label 1', 'Custom Label 2'])
plt.show()

Output:

Matplotlib Legend Location

Matplotlib Legend Location Conclusion

In this article, we have explored how to customize the location of the legend in a matplotlib plot. By understanding the different options available for setting the legend location, you can create more visually appealing and informative plots. Experiment with the examples provided to get a better understanding of how to customize the legend location in matplotlib.

Like(0)