
In order to obtain the y coordinate for a point on the kde curve of the distplot, you can use the underlying data of the curve. You can get the data from the line plot using the get_data
method of the line.
You can then interpolate the data on the point(s) you are interested in, using e.g. numpy.interp
.
import seaborn.apionly as sns
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
x = np.random.randn(100)
ax = sns.distplot(x, hist_kws={"ec":"k"})
data_x, data_y = ax.lines[0].get_data()
xi = 0 # coordinate where to find the value of kde curve
yi = np.interp(xi,data_x, data_y)
print ("x={},y={}".format(xi, yi)) # prints x=0,y=0.3698
ax.plot([0],[yi], marker="o")
plt.show()
Being asked in the comments about how to obtain this solution:
Start with the problem. We have a distplot and we want to draw a point at a certain point on its kde curve.
- Look at the documentation; does the distplot function have an argument that does what we want? Unfortunately not.
- Does the function return an object? Yes. Is it the curve? Unfortunately not. Instead it's a matplotlib axes. (finding that out with
type()
)
- Find out what a matplotlib axes is; read the documentation. Uff, it's pretty heavy, but we stumble upon a method
axes.get_lines()
; since the curve should be a line, that should help.
- Find out what those lines are: They are
Line2D
objects. Again looking at the documentation we find that there is a method get_data
. So now we have the data of the curve. Great!
- At this point it would be even better if we had a function we could call with our x value to receive the corresponding y value. Now it seems, we need to find that function by ourselves.
- So given
x
and y
data of the curve, how do we find the y value of a given x value? Since the data is discrete, we need to interpolate. Looking around for "interpolate" & "python" eventually brings us to numpy.interp
. So this provides us with the coordinates we need to draw a point.
- Find out how to draw a point. Well that's easy. Lots of examples for that around.
That's it.