Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get output of pandas .plot(kind='kde')?

Tags:

python

pandas

When I plot density distribution of my pandas Series I use

.plot(kind='kde')

Is it possible to get output values of this plot? If yes how to do this? I need the plotted values.

like image 924
drastega Avatar asked Jun 11 '14 01:06

drastega


2 Answers

There are no output value from .plot(kind='kde'), it returns a axes object.

The raw values can be accessed by _x and _y method of the matplotlib.lines.Line2D object in the plot

In [266]:

ser = pd.Series(np.random.randn(1000))
ax=ser.plot(kind='kde')

In [265]:

ax.get_children() #it is the 3nd object
Out[265]:
[<matplotlib.axis.XAxis at 0x85ea370>,
 <matplotlib.axis.YAxis at 0x8255750>,
 <matplotlib.lines.Line2D at 0x87a5a10>,
 <matplotlib.text.Text at 0x8796f30>,
 <matplotlib.text.Text at 0x87a5850>,
 <matplotlib.text.Text at 0x87a56d0>,
 <matplotlib.patches.Rectangle at 0x87a56f0>,
 <matplotlib.spines.Spine at 0x85ea5d0>,
 <matplotlib.spines.Spine at 0x85eaed0>,
 <matplotlib.spines.Spine at 0x85eab50>,
 <matplotlib.spines.Spine at 0x85ea3b0>]
In [264]:
#get the values
ax.get_children()[2]._x
ax.get_children()[2]._y
like image 175
CT Zhu Avatar answered Oct 02 '22 11:10

CT Zhu


You can also directly call the scipy.stats.gaussian_kde() function, that's what happens in pandas source code:

https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L284

Doc for the function is there:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html

like image 22
glyg Avatar answered Oct 02 '22 12:10

glyg