我想绘制两个二维直方图之间的差异。例如,考虑以下代码:
from numpy import c_
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
n = 100000
x = np.random.standard_normal(n)
y = 3.0 * x + 2.0 * np.random.standard_normal(n)
x1 = np.random.normal(loc=-2, scale=5, size=n)
y1 = (x1)**2 + np.random.standard_normal(n)
plt.figure(1)
h, xedges, yedges, image = plt.hist2d(x,y, bins=50, norm=mcolors.LogNorm(), cmap = plt.cm.rainbow)
plt.figure(2)
h1, xedges1, yedges1, image1 = plt.hist2d(x1,y1, bins=50, norm=mcolors.LogNorm(), cmap = plt.cm.rainbow)
是否可以绘制出它们之间的差异?
提前致谢。
答案 0 :(得分:0)
在我看来,您可以对数组进行操作,以找到 .._diff 值,然后进行绘图。 示例:
x_diff = x - x1
y_diff = y - y1
h_diff, xedges_diff, yedges_diff, image_diff = plt.hist2d(x_diff, y_diff, bins=50, norm=mcolors.LogNorm(), cmap = plt.cm.rainbow)
答案 1 :(得分:0)
您有两大类比较函数:bin-to-bin 比较和 cross-bin 比较。
Bin-to-bin 比较:标准的差异总和非常糟糕。 有一个改进,卡方距离。
Cross-bin 比较:一个称为 bin-similarity matrix 的标准示例需要一些相似度矩阵 M,其中 M(i,j) 是 bins i 和 j 之间的相似度。直方图 H1 和 H2 之间的距离为 sqrt((H1-H2)M(H1-H2))。地球移动距离 (EMD) 是另一种跨箱距离。
This paper 以一种很好的方式向您介绍了直方图距离。
总而言之,您需要更好地定义差异在您的情况下意味着什么。
编辑:我认为最简单的方法是使用 opencv's implementation of the histogram comparison,其中包括直方图的交集、Bhattacharyya 距离、卡方等指标。
cv.compareHist(hist_1, hist_2, compare_method)
答案 2 :(得分:0)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
n = 100000
x = np.random.standard_normal(n)
y = 3.0 * x + 2.0 * np.random.standard_normal(n)
x1 = np.random.normal(loc=-2, scale=5, size=n)
y1 = (x1)**2 + np.random.standard_normal(n)
plt.figure(1)
h, xedges, yedges, image = plt.hist2d(x,y, bins=(50, 60), norm=mcolors.LogNorm(), cmap = plt.cm.rainbow)
plt.figure(2)
h1, xedges1, yedges1, image1 = plt.hist2d(x1,y1, bins=(xedges, yedges), norm=mcolors.LogNorm(), cmap = plt.cm.rainbow)
plt.figure(3)
plt.pcolormesh(xedges, yedges, (h-h1).T)
plt.show()
出于某种原因,matplotlib 中的每个统计函数都返回转置的内容,但您明白了。