I have a matplotlib plot and I would like to find out which lines have set a label. Calling get_label() on the lines works, but also gives a non-empty result if the label is not set, e.g.,
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.plot([0.0], [0.0], label="test 1")
plt.plot([0.0], [0.0], label="test 2")
plt.plot([0.0], [0.0]) # no label
lines = []
for child in plt.gca().get_children():
if isinstance(child, mpl.lines.Line2D):
print(child.get_label())
test 1
test 2
_line2 # !
Any hints?