In my code I've already executed
ax.plot(x, y, 'b.-', ...)
and need to be able to set the label for the corresponding line after the fact, to have the same effect as if I'd
ax.plot(x, y, 'b.-', label='lbl', ...)
Is there a way to do this in Matplotlib?
If you grab the
line2D
object when you create it, you can set the label usingline.set_label()
:If you don't, you can find the
line2D
from theAxes
:Note,
ax.lines[-1]
will access the last line created, so if you make more than one line, you would need to be careful which line you set the label on using this method.A minimal example: