简体   繁体   English

Python Seaborn Distplot Y值对应于给定的X值

[英]Python Seaborn Distplot Y value corresponding to a given X value

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. 我需要在Seaborn distplot上绘制与某些X值相对应的点,以使它们落在密度曲线上或密度曲线下方。 Here is a distplot from the following URL: From the Seaborn site - distplot examples 这是来自以下URL的distplot: 从Seaborn网站-distplot示例

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. 因此,例如,在上面显示的曲线图中,我需要以编程方式确定与落在密度曲线上的X值0对应的Y轴值是多少。 From the figure, it seems like it is somewhere around 0.37. 从图中看来,它大约在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, ie, what line of code will show that. 假设可以做到,那么我如何在所示的图中显示它,即,哪一行代码可以显示出来。 I am translating a set of R visualizations to Python. 我正在将一组R可视化转换为Python。 The following plot in R shows what I am trying to achieve: R中的以下图显示了我要实现的目标:

在此处输入图片说明

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. 我是Matplotlib和Seaborn软件包的初学者。

在此处输入图片说明

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. 为了获得distplot的kde曲线上某个点的y坐标,可以使用曲线的基础数据。 You can get the data from the line plot using the get_data method of the line. 您可以使用线的get_data方法从线图中获取数据。 You can then interpolate the data on the point(s) you are interested in, using eg numpy.interp . 然后,您可以使用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. 我们有一个分布图,我们想在其kde曲线的某个点上绘制一个点。

  1. Look at the documentation; 查看文档; does the distplot function have an argument that does what we want? distplot函数是否具有我们想要的参数? Unfortunately not. 不幸的是没有。
  2. Does the function return an object? 函数是否返回对象? Yes. 是。 Is it the curve? 是曲线吗? Unfortunately not. 不幸的是没有。 Instead it's a matplotlib axes. 相反,它是一个matplotlib轴。 (finding that out with type() ) (使用type()找出答案)
  3. Find out what a matplotlib axes is; 找出什么是matplotlib轴; read the documentation . 阅读文档 Uff, it's pretty heavy, but we stumble upon a method axes.get_lines() ; Uff,它很重,但是我们偶然发现了一个axes.get_lines()方法; since the curve should be a line, that should help. 因为曲线应该是一条线,所以应该有所帮助。
  4. Find out what those lines are: They are Line2D objects. 找出这些线是什么:它们是Line2D对象。 Again looking at the documentation we find that there is a method get_data . 再次查看文档,我们发现有一个方法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. 此时,如果我们有一个函数可以调用x值来接收相应的y值,那就更好了。 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? 因此,给定曲线的xy数据,我们如何找到给定x值的y值? Since the data is discrete, we need to interpolate. 由于数据是离散的,因此我们需要进行插值。 Looking around for "interpolate" & "python" eventually brings us to numpy.interp . 到处寻找“ interpolate”和“ python”最终将我们带到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. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM