Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85894 > unrolled thread
| Started by | "ast" <nomail@invalid.com> |
|---|---|
| First post | 2015-02-19 11:47 +0100 |
| Last post | 2015-02-19 16:05 -0500 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
A question about how plot from matplotlib works "ast" <nomail@invalid.com> - 2015-02-19 11:47 +0100
Re: A question about how plot from matplotlib works marco.nawijn@colosso.nl - 2015-02-19 04:25 -0800
Re: A question about how plot from matplotlib works Jason Swails <jason.swails@gmail.com> - 2015-02-19 16:05 -0500
| From | "ast" <nomail@invalid.com> |
|---|---|
| Date | 2015-02-19 11:47 +0100 |
| Subject | A question about how plot from matplotlib works |
| Message-ID | <54e5bf49$0$3054$426a74cc@news.free.fr> |
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 ?
[toc] | [next] | [standalone]
| From | marco.nawijn@colosso.nl |
|---|---|
| Date | 2015-02-19 04:25 -0800 |
| Message-ID | <c8c880e4-936f-4c2a-9ca5-4df0ef0439ad@googlegroups.com> |
| In reply to | #85894 |
On Thursday, February 19, 2015 at 11:47:53 AM UTC+1, ast 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 ? Hi, I have not checked the source code, but pyplot probably implicitly generates a few objects for you. In particular it probably creates a default figure, so when you say "plt.plot(x,y)", behind the scenes pyplot will request the current figure and add the Line2D items to it. Marco
[toc] | [prev] | [next] | [standalone]
| From | Jason Swails <jason.swails@gmail.com> |
|---|---|
| Date | 2015-02-19 16:05 -0500 |
| Message-ID | <mailman.18906.1424379921.18130.python-list@python.org> |
| In reply to | #85894 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Feb 19, 2015 at 5:47 AM, ast <nomail@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web