Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No handles with labels found to put in legend

I'm trying to create a parallelogram in PyPlot. I'm not up to drawing the parallelogram--first I'm putting in the vector arrows--using the following code:

fig = plt.figure() ax = fig.add_subplot(111) ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') plt.axis([-5,5,-5,5]) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') plt.grid() plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u') plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v') plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v') plt.legend() 

This returns the following error:

No handles with labels found to put in legend. 

I'm not sure why, because, based on the documentation for plt.arrow(), label is an acceptable kwarg, and plt.legend() should ostensibly be reading that. The rest of the figure draws fine; it's just missing the legend.

like image 722
Yehuda Avatar asked Dec 15 '19 18:12

Yehuda


People also ask

What is no handles with labels found to put in legend?

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.

What are handles in legend?

legend handle. The original object which is used to generate an appropriate entry in the legend.

How do I fix the legend position in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.


2 Answers

It might be late but for anyone with the same issue the solution is using the method legend() for the corresponding ax not as for plt

fig = plt.figure() ax = fig.add_subplot(111) ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') plt.axis([-5,5,-5,5]) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') plt.grid() plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True, label='u') plt.arrow(0,0, 1,3, head_width=0.2, color='r', length_includes_head=True, label='v') plt.arrow(0,0, 4,4, head_width=0.2, color='r', length_includes_head=True, label='u+v') ax.legend() 
like image 83
Multivac Avatar answered Sep 22 '22 10:09

Multivac


You can explicitly define the elements in the legend.

For full control of which artists have a legend entry, it is possible to pass an iterable of legend artists followed by an iterable of legend labels respectively. Reference

Example:

arr1 = plt.arrow(0,0, 3,1, head_width=0.2, color='r', length_includes_head=True) arr2 = plt.arrow(0,0, 1,3, head_width=0.2, color='g', length_includes_head=True) arr3 = plt.arrow(0,0, 4,4, head_width=0.2, color='b', length_includes_head=True)  plt.xlim(0,5) plt.ylim(0,5)  plt.legend([arr1, arr2, arr3], ['u','v','u+v']) 

enter image description here

like image 30
abc Avatar answered Sep 22 '22 10:09

abc