将hist2d输出转换为matplotlib中的轮廓 [英] Turn hist2d output into contours in matplotlib

查看:91
本文介绍了将hist2d输出转换为matplotlib中的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用matplotlib.hist2d在Python中生成了一些数据。数据示例如下所示。

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

如您所见,该数据具有一些轮廓通过在整个图中跟踪相同的颜色找到它。我看到以0.015为中心的伽马分布。我想获取这些数据并收集这些轮廓,以便可以看到每个颜色级别的线迹。我尝试在此处使用轮廓函数

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.

有人知道获得这些轮廓的最佳方法吗?理想情况下,我想采用这些轮廓并为其拟合函数(如gamma函数),然后获取函数参数。

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.

谢谢

推荐答案

因此,问题在于 hist2d 创建的图像绘制在数据坐标中,但是您要创建的轮廓以像素坐标表示。解决此问题的简单方法是指定轮廓的范围(即在x和y轴上缩放/重新放置它们)。

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

例如:

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)

会产生:

这篇关于将hist2d输出转换为matplotlib中的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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