Wie weise ich mehrere Labels gleichzeitig in Matplotlib zu?

Ich habe folgenden Datensatz:

x = [0, 1, 2, 3, 4]
y = [ [0, 1, 2, 3, 4],
      [5, 6, 7, 8, 9],
      [9, 8, 7, 6, 5] ]

Ich jetzt zeichnen Sie es mit:

import matplotlib.pyplot as plt
plt.plot(x, y)

Allerdings möchte ich zur Bezeichnung der 3 y-Datensätze mit diesem Befehl, der einen Fehler auslöst, wenn .legend() heißt:

lineObjects = plt.plot(x, y, label=['foo', 'bar', 'baz'])
plt.legend()

File "./plot_nmos.py", line 33, in <module>
  plt.legend()
...
AttributeError: 'list' object has no attribute 'startswith'

Wenn ich überprüfen das lineObjects:

>>> lineObjects[0].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[1].get_label()
['foo', 'bar', 'baz']
>>> lineObjects[2].get_label()
['foo', 'bar', 'baz']

Frage

Gibt es einen eleganten Weg zu weisen mehrere labels, die nur mit der .plot() Methode?

InformationsquelleAutor der Frage Kit | 2012-07-14

Schreibe einen Kommentar