0

I'm studying matplotlib and I have a question.

Why just putting a comma in var_1, the type of the variable changes completely, it becomes Line2D, while without the comma the type is list ?

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
var_1, = ax.plot([], [])
var_2 = ax.plot([], [])
print(type(var_1), type(var_2))

#<class 'matplotlib.lines.Line2D'> <class 'list'>
rafaelcb21
  • 12,422
  • 28
  • 62
  • 86
  • Note that the [`plot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) command is very versatile, and you can plot multiple curves with just one function call. For consistency, always a list of these curves (`Line2D`) is returned, also when there is only one curve created. – JohanC Oct 05 '22 at 16:14

1 Answers1

2
ax.plot([], [])

is returning a list of length 1. In the first assignment, the comma indicates you're assigning contents of the list to the variable, similar to

 a, b, c = 1, 2, 3  

or

 a, b, c = [1, 2, 3]

In your case, its the same as

a, = 1,

(Since 1, generates a tuple (list-like object) of length one. )

Luce
  • 184
  • 2
  • 9