Introduction
In knowledge visualization, typically create advanced graphs that have to have legends for the reader to have the ability to interpret the graph. However what if these legends get in the best way 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 may be essential to assist the reader perceive the visualization they’re . With out the legend, you won’t know which line represented which knowledge! This 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 Information')
# Add a legend
plt.legend()
# Show the plot
plt.present()
It will produce a plot with a legend situated within the upper-left nook contained in the plot. The legend comprises the label ‘Pattern Information’ 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 isn’t at all times your best option. Legends can obscure essential particulars of the plot, particularly when coping with advanced knowledge visualizations. By positioning the legend exterior the plot, we will make certain that all knowledge factors are clearly seen, making our plots simpler to interpret.
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. This is tips on how 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 Information')
# 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 dimensions of the plot. So, (1, 1.10)
positions the anchor level simply exterior the highest proper nook of the plot.
Positioning this legend is a little more of an artwork than a science, so you might have to mess around with the values a bit to see what works.
Frequent Points and Options
One widespread concern is the legend getting minimize off while you save the determine utilizing plt.savefig()
. This occurs as a result of plt.savefig()
does not robotically modify the determine dimension to accommodate the legend. To repair this, you should use the bbox_inches
parameter and set it to ‘tight’ like so:
plt.savefig('my_plot.png', bbox_inches='tight')
One other widespread concern is the legend overlapping with the plot when positioned exterior. This may be mounted by adjusting the plot dimension or the legend dimension to make sure they match collectively properly. Once more, that is one thing you will seemingly have to check with many various values to seek out the best configuration and positioning.
Observe: Adjusting the plot dimension may be performed utilizing plt.subplots_adjust()
, whereas the legend dimension may be adjusted utilizing legend.get_frame()
.
Conclusion
And there you have got it! On this Byte, we confirmed how one can place the legend exterior the plot in Matplotlib and defined some widespread points. We have additionally talked a bit about some use-cases the place you will have to place the legend exterior the plot.