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.
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.