A question about how plot from matplotlib works

marco.nawijn at colosso.nl marco.nawijn at colosso.nl
Thu Feb 19 07:25:28 EST 2015


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
 



More information about the Python-list mailing list