Python Seaborn Distplot Y值对应于给定的X值 [英] Python Seaborn Distplot Y value corresponding to a given X value

查看:722
本文介绍了Python Seaborn Distplot Y值对应于给定的X值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Seaborn distplot上绘制与某些X值相对应的点,以使它们落在密度曲线上或在密度曲线下方.这是来自以下URL的distplot: 从Seaborn网站-distplot示例

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

这是带有代码的图片:

例如,在上面所示的图中,我需要以编程方式确定与落在密度曲线上的X值0对应的Y轴值是多少.从图中看来,它大约在0.37左右.如何在我的程序中得到它?

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?

假设可以做到,那么我如何在所示的图中显示它,即,哪一行代码可以显示出来.我正在将一组R可视化转换为Python. R中的以下图显示了我要实现的目标:

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:

看到曲线上显示的点了吗?有许多要绘制的点值,但是如果您帮助我绘制一个点值,我可以尝试进行其余操作.我是Matplotlib和Seaborn软件包的初学者.

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.

推荐答案

为了获得distplot的kde曲线上某个点的y坐标,可以使用曲线的基础数据.您可以使用 get_data 该行的方法. 然后,您可以使用,例如,在感兴趣的点上插值数据. numpy.interp .

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:

从问题开始.我们有一个分布图,我们想在其kde曲线的某个点上绘制一个点.

Start with the problem. We have a distplot and we want to draw a point at a certain point on its kde curve.

  1. 查看文档; distplot函数是否具有我们想要的参数?不幸的是没有.
  2. 该函数是否返回对象?是的.是曲线吗?不幸的是没有.相反,它是一个matplotlib轴. (使用type()确认)
  3. 找出matplotlib轴是什么;阅读文档.乌夫(Uff),它很重,但是我们偶然发现了方法axes.get_lines();因为曲线应该是一条线,所以应该有帮助.
  4. 找出这些行是什么:它们是 Line2D 对象.再次查看文档,我们发现有方法get_data.所以现在我们有了曲线的数据.太好了!
  5. 在这一点上,如果我们有一个函数可以用x值调用以接收相应的y值,那就更好了.现在看来,我们需要自己找到该功能.
  6. 因此,给定曲线的xy数据,我们如何找到给定x值的y值?由于数据是离散的,因此我们需要进行插值.到处寻找插值"和最终,"python"将我们带到了numpy.interp.因此,这为我们提供了绘制点所需的坐标.
  7. 了解如何绘制点.好吧,这很容易.周围有很多例子.
  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.

就是这样.

这篇关于Python Seaborn Distplot Y值对应于给定的X值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆