Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does a comma transform the type into Line2D of an Axes?

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'>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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. )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading