Sunday, January 9, 2022

ItsMyCode: No handles with labels found to put in legend

ItsMyCode |

In Python matplotlib No handles with labels found to put in legend occur if you have not defined the label parameters whenever you plot the figure and try to call the plt.legend() method.

The matplotlib.pyplot  is a state-based interface to matplotlib and provides a way to plot interactive figures in Python.  

We can use matplotlib.pyplot.legend() method to place a legend on the axes.

However, if we do not add the labels parameter and then call the matplotlib.pyplot.legend() function, you will get No handles with labels found to put in legend. 

If you are using the latest version of Python, then the error would be No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 2000)
y1 = np.sin(x)
y2 = np.arcsin(x)
plt.plot(x, y1)
plt.plot(x, y2)
plt.legend()
plt.show()

Output

No handles with labels found to put in legend.Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

Another way you get this error is if you call the legend method before plotting it. Ensure to verify your code and call the legend function after completing the plotting.

Solution – No handles with labels found to put in legend

Now that we know why the error occurs, let us see how to resolve the error and plot the legends correctly.

There are three different ways to call the matplotlib.pyplot.legend() method in Python.

Calling legend() without any arguments

If you want the legends to be detected automatically, you can call legend() method without passing any arguments. This will automatically detect the legend elements, including the labels, and plot them for you.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 8, 1000)
y1 = np.sin(x)
y2 = np.arcsin(x)

plt.plot(x, y1, label='sin')
plt.plot(x, y2, label='arcsin')
plt.legend()
plt.show()

Output

Passing labels as arguments to legend() method

You can pass the labels as an argument to the legend() method as iterable of strings. 

Each string is used as a label for the elements in the order they were created.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 8, 1000)
y1 = np.sin(x)

plt.plot([4, 7, 9])
plt.plot(x, y1, '-b')
plt.legend(['Line1', 'Line2'])
plt.show()
Note: This is not the recommended approach since the relationship between the elements and the passed labels exist only through the order it created and can lead to confusion.

Passing handles and labels as a parameter to legend() method

If we need complete control, we can pass the elements followed by the iterable of strings as labels explicitly to the legend() function.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 8, 1000)
y1 = [4, 7, 9]
y2 = np.sin(x)
y3 = np.arcsin(x)

line, = plt.plot(y1)
sine, = plt.plot(x, y2)
arcsine, = plt.plot(x, y3)

plt.legend(handles = [line, sine, arcsine], 
           labels  = ['Line', 'Sine', 'Arcsine'])
plt.show()

Reference: Stackoverflow, Educative

The post No handles with labels found to put in legend appeared first on ItsMyCode.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...