multigoodverse
multigoodverse

Reputation: 8072

Matplotlib graph appearing as a Python list type

The following code produces a graph:

import matplotlib
gr = matplotlib.pyplot.plot([1,2,3])

enter image description here

Now, if I check the type of this seemingly graph object:

type(gr)

The output is:

list

I was expecting it to be some sort of matplotlib graph object, but this comes to be a native Python list. Can anyone explain why?

Upvotes: 1

Views: 729

Answers (2)

EdChum
EdChum

Reputation: 394209

Well what is returned is a list of the lines that are plotted not the graph object itself, see the docs

Return value is a list of lines that were added.

in my case I see this as the object:

[<matplotlib.lines.Line2D at 0xb2f83c8>]

the type is a list and the contents are a Line2D object:

In [141]:

for e in l:
    print(e)
Line2D(_line0)

The contents of the Line2D are the lines added to the plot:

In [146]:

l[0].get_data()
Out[146]:
(array([ 0.,  1.,  2.]), array([1, 2, 3]))

EDIT

If you want to access the Figure for saving, I personally write like this:

import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.Figure() #  do something with Figure here

Upvotes: 4

Ed Smith
Ed Smith

Reputation: 13216

From the matplotlib documents, the plot function,

Return value is a list of lines that were added.

Each of the list items themselves are matplotlib.lines.Line2D types, as perhaps would be expected.

Upvotes: 1

Related Questions