How to extract x,y data from kdensity plot from matplotlib for python
<p>I am trying to figure out how to make a 3d figure of uni-variate kdensity plots as they change over time (since they pull from a sliding time window of data over time).  </p>

<p>Since I can't figure out how to do that directly, I am first trying to get the x,y plotting data for kdensity plots of matplotlib in python.  I hope after I extract them I can use them along with a time variable to make a three dimensional plot.  </p>

<p>I see several posts telling how to do this in Matlab. All reference getting <code>Xdata</code> and <code>Ydata</code> from the underlying figure:</p>

<pre><code>x=get(h,'Xdata')
y=get(h,'Ydata')
</code></pre>

<p>How about in python?</p>

<p>Not sure how kdensity plots work, but note that <code>matplotlib.pyplot.plot</code> returns a list of the added <code>Line2D</code> objects, which are, in fact, where the X and Y data are stored.  I suspect they did that to make it work similarly to MATLAB.</p>

<pre><code>import matplotlib.pyplot as plt

h = plt.plot([1,2,3],[2,4,6])  # [&lt;matplotlib.lines.Line2D object at 0x021DA9F0&gt;]
x = h[0].get_xdata()  # [1,2,3]
y = h[0].get_ydata()  # [2,4,6]
</code></pre>

python matplotlib distribution