查找二维直方图的峰 [英] Find peak of 2d histogram
问题描述
我对某些(x, y)
数据进行了二维直方图处理,并得到了如下图所示的图像:
I make a 2d histogram of some (x, y)
data and I get an image like this one:
我想要一种方法来获取将最大值存储在H
中的点的(x, y)
坐标.例如,在上图的情况下,它将是两个具有近似坐标的点:(1090, 1040)
和(1110, 1090)
.
I want a way to get the (x, y)
coordinates of the point(s) that store the maximum values in H
. For example, in the case of the image above it would be two points with the aprox coordinates: (1090, 1040)
and (1110, 1090)
.
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from os import getcwd
from os.path import join, realpath, dirname
# Path to dir where this code exists.
mypath = realpath(join(getcwd(), dirname(__file__)))
myfile = 'datafile.dat'
x, y = np.loadtxt(join(mypath,myfile), usecols=(1, 2), unpack=True)
fig = plt.figure()
ax = fig.add_subplot(111)
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
rang = [[xmin, xmax], [ymin, ymax]]
binsxy = [int((xmax - xmin) / 20), int((ymax - ymin) / 20)]
H, xedges, yedges = np.histogram2d(x, y, range=rang, bins=binsxy)
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
cp = ax.imshow(H.transpose()[::-1], interpolation='nearest', extent=extent, cmap=cm.jet)
fig.colorbar(cp)
plt.show()
修改
我已经尝试了Marek和qarma发布的解决方案,试图获取垃圾箱的坐标而不是它们的索引,就像这样:
I've tried the solutions posted by Marek and qarma attempting to obtain the coordinates of the bins rather than the index of them, like so:
# Marek's answer
x_cent, y_cent = unravel_index(H.argmax(), H.shape)
print('Marek')
print(x_cent, y_cent)
print(xedges[x_cent], yedges[y_cent])
# qarma's answer
idx = list(H.flatten()).index(H.max())
x_cent2, y_cent2 = idx / H.shape[1], idx % H.shape[1]
local_maxs = np.argwhere(H == H.max())
print('\nqarma')
print(x_cent2, y_cent2)
print(xedges[x_cent2], yedges[y_cent2])
print(xedges[local_maxs[0,0]], yedges[local_maxs[0,1]], xedges[local_maxs[1,0]], yedges[local_maxs[1,1]])
其结果是:
Marek
(53, 50)
(1072.7838144329899, 1005.0837113402063)
qarma
(53, 50)
(1072.7838144329899, 1005.0837113402063)
(1072.7838144329899, 1005.0837113402063, 1092.8257731958763, 1065.3611340206187)
所以最大坐标是相同的,这很好!现在,我遇到了一个小问题,因为如果放大2d图,我会发现全局最大值和局部最大值的坐标都稍微偏离中心:
So the maximum coordinates are the same which is good! Now I have a small issue because if I zoom in on the 2d plot, I see that the coordinates are a little off-centered for both the global maximum and the local maximum:
这是为什么?
推荐答案
在这里,您可以找到第一个全局最大值
Here's how you can find first global maximum
idx = list(H.flatten()).index(H.max())
x, y = idx / H.shape[1], idx % H.shape[1]
剩下的所有最大值的查找坐标留给读者练习...
Finding coordinate of all maxima was left as exercise to the reader...
numpy.argwhere(H == H.max())
修改
您的代码:
H, xedges, yedges = np.histogram2d(x, y, range=rang, bins=binsxy)
此处H
包含直方图值和直方图bin的xedges, yedges
边界.请注意,edges
数组的大小比相应维度中H
的大小大一.因此:
Here H
contains histogram values and xedges, yedges
boundaries for histogram bins. Note that size of edges
arrays is one larger than size of H
in corresponding dimension. Thus:
for x, y in numpy.argwhere(H == H.max()):
# center is between x and x+1
print numpy.average(xedges[x:x + 2]), numpy.average(yedges[y:y + 2])
这篇关于查找二维直方图的峰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!