Python Seaborn Distplot Y value corresponding to a

2020-02-10 10:03发布

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:

enter image description here

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:

enter image description here

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.

1条回答
可以哭但决不认输i
2楼-- · 2020-02-10 10:43

enter image description here

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.

  1. Look at the documentation; does the distplot function have an argument that does what we want? Unfortunately not.
  2. Does the function return an object? Yes. Is it the curve? Unfortunately not. Instead it's a matplotlib axes. (finding that out with type())
  3. 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.
  4. 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!
  5. 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.
  6. 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.
  7. Find out how to draw a point. Well that's easy. Lots of examples for that around.

That's it.

查看更多
登录 后发表回答