Search code examples
pythonmatplotlibkernel-density

Set axes as variable value instead of variable index


I followed an example of kernel density estimation in 2-dimensions from https://kde-diffusion.readthedocs.io/en/stable/usage.html to calculate the density of random (x,y) data. The code are below:

from numpy.random import normal
x = normal(size=1000)
y = normal(size=1000)

# Estimate density within ±5 standard deviations.
from kde_diffusion import kde2d
(density, grid, bandwidth) = kde2d(x, y, n=32, limits=None)

# Display estimated density as image.
from matplotlib import pyplot
figure = pyplot.figure()
axes = figure.add_subplot()
axes.imshow(density.T)
pyplot.show()

Figure

Figure x,y axes are indexes of grid(array0(32 x variables), array1(32 y variables)). Is there any way to show x, y axes as corresponding variable value in array0 and array1?


Solution

  • density here is a tuple with two elements, x and y. We can unpack the tuple as such: x, y = density and pack it up again as x, y = density