0

I have a understanding question about the lengend in matplotlib. Here is a code example how I plot two different plots with legend

plt.figure(3)
line1,=plt.plot(freq,numpy.sqrt(MX*MX+MY*MY))
line2,=plt.plot(freq,numpy.abs(STAA))
plt.xlabel('offset frequency / rad/s')
plt.ylabel('magnitude of trans. magnetization')
plt.legend([line1,line2], ['analytical solution','with approximation'])
plt.show()

line1,= creates a tuple with one element. My question is: what is the reason matplotlib needs a tuple here instead of a "normal" object? There must be some sort of benefit I am not aware of.

cheers, glostas

Glostas
  • 1,090
  • 2
  • 11
  • 21
  • This not a duplicate of the linked question. I edited the question such that it is more clear what I want to know. The answers found in the link are about what the ,= means (which was indeed an unsolved question before the edit) but do answer why it has to be used here which is the more interesting question. – Glostas Sep 18 '15 at 11:34
  • 1
    actually, `line1,=` returns just the `Line2D` object (not a tuple), whereas `line1 =` would return a one-element list containing the `Line2D`. When you make a list from `line1` and `line2` to give to `legend`, you want just the `Line2D`, not the list – tmdavison Sep 18 '15 at 11:52
  • Thanks, but I am not sure if I understand the way python function in this case. Why is ,= returning an object and = a list? What you said is perfectly true but why is it like this? – Glostas Sep 18 '15 at 12:57
  • 2
    the answer to that is in the [_possible_ duplicate question](http://stackoverflow.com/questions/1708292/meaning-of-using-commas-and-underscores-with-python-assignment-operator) – tmdavison Sep 18 '15 at 12:58
  • It is the same like a=2, then x=a gives a list while x,=a gives 2. – Glostas Sep 18 '15 at 13:05

0 Answers0