I’m walking through basic tutorials for matplotlib, and the example code that I’m working on is:
JavaScript
x
11
11
1
import numpy as np
2
3
import matplotlib.pylab as plt
4
5
x=[1,2,3,4]
6
y=[5,6,7,8]
7
8
line, = plt.plot(x,y,'-')
9
10
plt.show()
11
Does anyone know what the comma after line (line,=plt.plot(x,y,'-')
) means?
I thought it was a typo but obviously the whole code doesn’t work if I omit the comma.
Advertisement
Answer
plt.plot
returns a list of the Line2D
objects plotted, even if you plot only one line.
That comma is unpacking the single value into line
.
ex
JavaScript
1
3
1
a, b = [1, 2]
2
a, = [1, ]
3