A question about how plot from matplotlib works

Jason Swails jason.swails at gmail.com
Thu Feb 19 16:05:19 EST 2015


On Thu, Feb 19, 2015 at 5:47 AM, ast <nomail at invalid.com> wrote:

> Hello
>
>  import numpy as np
>>>> import matplotlib.pyplot as plt
>>>> x = np.arange(10)
>>>> y = x**2
>>>> x
>>>>
>>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>
>> y
>>>>
>>> array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
>
>> plt.plot(x,y)
>>>>
>>> [<matplotlib.lines.Line2D object at 0x044F5930>]
>
>> plt.show()
>>>>
>>>
>
> The question is:
>
> plt.plot() creates an object "matplotlib.lines.Line2D" but this object is
> not referenced. So this object should disapear from memory. But
> this doesn't happens since plt.show() draws the curve on a graphic
> window. So how does it work ?


​A reference to it is put in the "active" Axes instance of the
matplotlib.pyplot namespace.  There are many things that will prevent an
object from being garbage-collected (a common source of references are
caches). [1]

​In general, matplotlib has many containers.  In particular, Line2D objects
generated by the "plot" function are added to the Axes instance from which
"plot" was called.  When you don't explicitly specify an Axes object from
which to plot, matplotlib.pyplot applies it to some "default" Axes instance
living in the matplotlib.pyplot namespace.​

This is done to give matplotlib more of a Matlab-like feel.  To demonstrate
this, let's go try and FIND that reference to the lines:

>>> import matplotlib.pyplot as plt
​>>> import numpy as np
​>>> x = np.arange(10)
​>>> y = x ** 2
​>>> x
​array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
​>>> y
​array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
​>>> lines, = plt.plot(x, y)
​>>> id(lines)
​4466622800
​​
>>> lines
​<matplotlib.lines.Line2D object at 0x10a3b4150>
​>>> del lines
​>>> # Now let's find those lines
​... active_axes = plt.gca() # Get Current Axes
​>>> dir(active_axes)
​[..., get_lines, ...] <-- this is snipped for brevity
​>>> active_axes.get_lines()
​<a list of 1 Line2D objects>
​>>> active_axes.get_lines()[0]
​<matplotlib.lines.Line2D object at 0x10a3b4150>
​>>> id(active_axes.get_lines()[0])
​4466622800

And there we have it!  Success!  (Note, my comment indicates that the gca
in plt.gca() stands for "Get Current Axes").  I also snipped the list of
attributes in active_axes that I got from the "dir" command, since that
list is HUGE, but the method we want is, rather expectedly, "get_lines".

In *my* personal opinion, the matplotlib API is quite intuitive, such that,
coupled with Python's native introspective functions (like dir() and id())
and "help" function in the interpreter, I rarely have to consult
StackOverflow or even the API documentation online to do what I need.

For instance, you want to change the color or thickness of the error bar
hats on error bars in your plot?  Either save a reference to them when they
are generated (by plt.errorbar, for instance), or go *find* them inside the
Axes you are manipulating and set whatever properties you want.

Hope this helps,
Jason

[1] OK, so there are not many *things* -- only if there are active,
non-circular references will the object *not* be garbage-collected, loosely
speaking.  But there are many reasons and places that such references are
generated inside many APIs... caching being one of the most popular.

-- 
Jason M. Swails
BioMaPS,
Rutgers University
Postdoctoral Researcher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150219/14c79604/attachment.html>


More information about the Python-list mailing list