Insert legends to a matplotlib figure

Hi,

I tried to insert legends to a matplotlib figure,

u = plt.plot(A, label = 'A')
v = plt.plot(B, label = 'B')
plt.legend([u, v], ['A','B'])
plt.show()

and I see a figure but no legend. I also see this warning message

C:\APPS\Anaconda3\lib\site-packages\matplotlib\legend.py:798: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x000002071BA29FD0>] instances.
A proxy artist may be used instead.
See: http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists
  "aka-proxy-artists".format(orig_handle)
C:\APPS\Anaconda3\lib\site-packages\matplotlib\legend.py:798: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x000002071CC539E8>] instances.
A proxy artist may be used instead.
See: http://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists
  "aka-proxy-artists".format(orig_handle)

Is there any way to show the legend? Thanks!

Does this do what you want?

u = plt.plot(A, label='A')
v = plt.plot(B, label='B')
plt.legend(loc='upper left')
plt.show()

I’m new to matplotlib and as such, I can’t (yet) explain the warnings that you see.

Thanks Rob! It works!

1 Like

You’re very welcome John.

There is a very good guide on the Real Python site; kinda one of my ‘go-to’ resources, for all things Python.