0

I am reading over some example code for matplotlib and I came across something I was not understanding. In line 8, what is the purpose of the comma after the word "line"?

from pylab import *
import time

ion()  

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas

print ('FPS:' , 200/(time.time()-tstart))
ataveras64
  • 25
  • 2
  • Please don't use `pylab`. And certainly avoid `import *` which clutters the local namespace, leading to bizarre behavior. Instead, use `from matplotlib import pyplot as plt` and `import numpy as np`. See [What is the difference between pylab and pyplot?](https://stackoverflow.com/questions/11469336/what-is-the-difference-between-pylab-and-pyplot) – JohanC Feb 14 '22 at 09:13

1 Answers1

1

It is for unpacking only the first element of a multiple-element tuple.

In this example, plot() returns a single-element tuple and if you saved it directly into line by using line = plot(...), your variable line would have the whole tuple with one element.

Instead, by doing line, = plot(...), the element itself is being "extracted" and saved into line instead of the entire tuple.

Here are some more examples.

Rajdeep Biswas
  • 187
  • 2
  • 11