Issue
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'>
Solution
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. )
Answered By - Luce
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.