Saturday, September 23, 2023
HomeProgrammingFind out how to Place Legend Outdoors the Plot in Matplotlib

Find out how to Place Legend Outdoors the Plot in Matplotlib


Introduction

In knowledge visualization, typically create complicated graphs that must have legends for the reader to have the ability to interpret the graph. However what if these legends get in the way in which of the particular knowledge that they should see? On this Byte, we’ll see how one can transfer the legend in order that it is exterior of the plot in Matplotlib.

Legends in Matplotlib

In Matplotlib, legends present a mapping of labels to the weather of the plot. These will be crucial to assist the reader perceive the visualization they’re taking a look at. With out the legend, you won’t know which line represented which knowledge! Here is a fundamental instance of how legends work in Matplotlib:

import matplotlib.pyplot as plt

# Create a easy line plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label='Pattern Knowledge')

# Add a legend
plt.legend()

# Show the plot
plt.present()

This can produce a plot with a legend positioned within the upper-left nook contained in the plot. The legend incorporates the label ‘Pattern Knowledge’ that we specified within the plt.plot() perform.

Why Place the Legend Outdoors the Plot?

Whereas having the legend contained in the plot is the default setting in Matplotlib, it is not all the time the only option. Legends can obscure vital particulars of the plot, particularly when coping with complicated knowledge visualizations. By positioning the legend exterior the plot, we are able to make certain that all knowledge factors are clearly seen, making our plots simpler to interpret.

Find out how to Place the Legend Outdoors the Plot in Matplotlib

Positioning the legend exterior the plot in Matplotlib is pretty simple to do. We merely want to make use of the bbox_to_anchor and loc parameters of the legend() perform. Here is easy methods to do it:

import matplotlib.pyplot as plt

# Create a easy line plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], label='Pattern Knowledge')

# Add a legend exterior the plot
plt.legend(bbox_to_anchor=(1, 1.10), loc='higher proper')

# Show the plot
plt.present()

On this instance, bbox_to_anchor is a tuple specifying the coordinates of the legend’s anchor level, and loc signifies the situation of the anchor level with respect to the legend’s bounding field. The coordinates are in axes fraction (i.e., from 0 to 1) relative to the scale of the plot. So, (1, 1.10) positions the anchor level simply exterior the highest proper nook of the plot.

legend outside the plot

Positioning this legend is a little more of an artwork than a science, so you could must mess around with the values a bit to see what works.

Widespread Points and Options

One frequent subject is the legend getting reduce off once you save the determine utilizing plt.savefig(). This occurs as a result of plt.savefig() does not routinely alter the determine measurement to accommodate the legend. To repair this, you should utilize the bbox_inches parameter and set it to ‘tight’ like so:

plt.savefig('my_plot.png', bbox_inches='tight')

One other frequent subject is the legend overlapping with the plot when positioned exterior. This may be fastened by adjusting the plot measurement or the legend measurement to make sure they match collectively properly. Once more, that is one thing you may doubtless have to check with many alternative values to seek out the correct configuration and positioning.

Be aware: Adjusting the plot measurement will be achieved utilizing plt.subplots_adjust(), whereas the legend measurement will be adjusted utilizing legend.get_frame().

Conclusion

And there you may have it! On this Byte, we confirmed how one can place the legend exterior the plot in Matplotlib and defined some frequent points. We have additionally talked a bit about some use-cases the place you may must place the legend exterior the plot.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments