python - 在 matplotlib 中将 hist2d 输出转换为轮廓

标签 python matplotlib histogram distribution contour

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

如您所见,通过在整个绘图中追踪相同的颜色可以找到该数据中的一些等高线。我看到一个以 0.015 为中心的 Gamma 分布。我想获取这些数据并收集这些轮廓,这样我就可以看到穿过每个颜色级别的线条轨迹。我尝试像这里一样使用轮廓函数

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

但这似乎没有产生任何结果。

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

谢谢

最佳答案

所以问题是 hist2d 创建的图像是在数据坐标中绘制的,但您尝试创建的轮廓是在像素坐标中。解决此问题的简单方法是指定轮廓的范围(即在 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)

将产生:

enter image description here

关于python - 在 matplotlib 中将 hist2d 输出转换为轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351621/

相关文章:

python - Matplotlib:分配不同的颜色

python - 基于两个因素可视化错误

python - 在 Python 中绘制分段函数

python - pyplot.hist 中的直方图条不以 xticks 为中心

python - pandas python 中一个字段与另一个字段的直方图

r - 在 R 中的多列上绘制每行直方图

python - 在 Python 3.4 上设置 newRelic 的问题

python - tkinter Canvas 图像不显示

Python逐行读取整个文件与内存统计

python - ThreadPoolExecutor 可以工作,但 ProcessPoolExecutor 不能