将hist2d输出转换为matplotlib中的等值线(Turn hist2d output into contours in matplotlib)

编程入门 行业动态 更新时间:2024-10-27 00:24:40
将hist2d输出转换为matplotlib中的等值线(Turn hist2d output into contours in matplotlib)

我使用matplotlib.hist2d在Python中生成了一些数据。 下面是一个数据的例子。

正如您所看到的,通过在整个图中追踪相同的颜色,可以看到这些数据中有一些轮廓。 我看到一个以0.015为中心的伽玛分布。 我想采取这些数据并收集这些轮廓,以便我可以看到通过每个颜色级别的线迹。 我试着在这里玩轮廓功能

counts, xedges, yedges, Image = hist2d(x, y, bins=bins, norm=LogNorm(), range=[[0, 1], [0, 400]]) contour(counts)

但这似乎没有产生任何东西。

有谁知道获得这些轮廓的最佳方法吗? 理想情况下,我想采用这些轮廓,并为它们配置一个函数(如伽马函数),然后获取函数参数。

谢谢

I have generated some data in Python using matplotlib.hist2d. An example of the data is seen below.

As you can see this data has some contours in it found by tracing the same color throughout the plot. I see a gamma distribution centered around 0.015. I would like to take this data and gather these contours so I can see a line trace through each color level. I tried playing around with the contour function as here

counts, xedges, yedges, Image = hist2d(x, y, bins=bins, norm=LogNorm(), range=[[0, 1], [0, 400]]) contour(counts)

but that didn't seem to produce anything.

Does anyone know the best way to get these contours? Ideally I'd like to take these contours and fit a function (like a gamma function) to them and then get the function parameters.

Thanks

最满意答案

所以问题是由hist2d创建的图像被绘制在数据坐标中,但您尝试创建的轮廓是在像素坐标中。 围绕这个的简单方法是指定轮廓的extent (即,将它们重新缩放/重新定位在x和y轴上)。

例如:

from matplotlib.colors import LogNorm from matplotlib.pyplot import * x = np.random.normal(5,10,100000) y = np.random.normal(5,10,100000) counts,ybins,xbins,image = hist2d(x,y,bins=100,norm=LogNorm()) contour(counts,extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],linewidths=3)

会产生:

在这里输入图像描述

So the problem is that the image created by hist2d is plotted in data coordinates, but the contours you are trying to create are in pixel coordinates. The simple way around this is to specify the extent of the contours (i.e. rescale/reposition them in the x and y axes).

For example:

from matplotlib.colors import LogNorm from matplotlib.pyplot import * x = np.random.normal(5,10,100000) y = np.random.normal(5,10,100000) counts,ybins,xbins,image = hist2d(x,y,bins=100,norm=LogNorm()) contour(counts,extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],linewidths=3)

Will produce:

enter image description here

更多推荐

本文发布于:2023-07-20 20:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:等值线   转换为   matplotlib   hist2d   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!