I need to plot points on a Seaborn distplot corresponding to certain X values such that they fall either on the density curve or below it. Here is a distplot from the following URL: From the Seaborn site - distplot examples
Here is an image with the code:
So for example, in the plot shown above, I need to determine programmatically what is the Y axis value corresponding to the X value of 0 that falls on the density curve. From the figure, it seems like it is somewhere around 0.37. How can I get that in my program?
Assuming that can be done, then how can I show it in the plot shown, i.e., what line of code will show that. I am translating a set of R visualizations to Python. The following plot in R shows what I am trying to achieve:
See the points shown on the curve? There are many point values to be drawn, but if you help me draw one, I can try to do the rest. I am a beginning user of both Matplotlib and Seaborn packages.
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
.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.
type()
)axes.get_lines()
; since the curve should be a line, that should help.Line2D
objects. Again looking at the documentation we find that there is a methodget_data
. So now we have the data of the curve. Great!x
andy
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 tonumpy.interp
. So this provides us with the coordinates we need to draw a point.That's it.