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))
1
  • 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? Commented Feb 14, 2022 at 9:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.