如果您正在尝试研究两个变量的直方图以及它们如何在单个函数中相互关联,请考虑阅读有关多变量正态分布的信息。这肯定适用于研究图像中像素的分布。
https://juanitorduz.github.io/multivariate_normal/
看起来这就是你想要做的?:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
sns.set_context("notebook")
sns.set_style("darkgrid")
# %% Construct normal distribution data
n = 100
hist1 = np.random.normal(0,1,n)
hist2 = np.random.normal(0,1,n)
# %% Plot distributions on their own axis
sns.jointplot(x=hist1, y=hist2, kind="kde", space=0)
与 KDE 绘图不同的过程,它实际找到定义数据的多变量 PDF,然后绘制 PDF。这次hist2
的分布hist1
与等高线图上的分布不同:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
sns.set_context("notebook")
sns.set_style("darkgrid")
from scipy.stats import multivariate_normal as mvn
# %% Create test data for multivariate PDF
n = 1000
hist1 = np.random.normal(0,1,n)
hist2 = np.random.normal(0,2,n)
# %% Calculate mean and covariance of data
mean = [hist1.mean(), hist2.mean()]
cov_mat = np.cov( np.array([hist1, hist2]) )
# %% Create multivariate function with calculated means and covariance
mv_norm_f = mvn(mean=mean, cov=cov_mat)
# %% Setup ranges of variables for PDF function
range = np.linspace(-1,1,n)
x, y = np.meshgrid(range, range, indexing='xy')
xy = np.empty(x.shape + (2,))
xy[:, :, 0] = x
xy[:, :, 1] = y
print(x.shape)
print(xy.shape)
# %% Call PDF function on ranges of variables
z = mv_norm_f.pdf( xy )
# %% Shaded contour plot the PDF
plt.figure()
plt.contourf(x, y, z)
plt.xlabel("X")
plt.ylabel("Y")
plt.colorbar()
plt.grid('on')
plt.show()