Skip to content
Advertisement

matplotlib 2d line line,=plot comma meaning

I’m walking through basic tutorials for matplotlib, and the example code that I’m working on is:

import numpy as np

import matplotlib.pylab as plt

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

line, = plt.plot(x,y,'-')

plt.show()

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

a, b = [1, 2]
a, = [1, ]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement