我想比较二元随机变量的直方图及其概率分布。我已经为此编写了代码,并且将两个图都放在一个图上。但是我无法区分两者。条形图掩盖了表面图。
如何更改颜色或任何其他参数,以便可以将它们一起可视化并比较两个图?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
from mpl_toolkits.mplot3d import Axes3D
mu_x = 0
variance_x = 1
mu_y = 0
variance_y = 1
sample=1000
x, y = np.random.multivariate_normal((mu_x, mu_y), [[variance_x, 0], [0, variance_y]], sample).T
hist, xedges, yedges = np.histogram2d(x, y, bins=100, range=[[-10, 10], [-10, 10]],density=True)
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
x = np.linspace(-10,10,500)
y = np.linspace(-10,10,500)
X, Y = np.meshgrid(x,y)
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X; pos[:, :, 1] = Y
rv = multivariate_normal([mu_x, mu_y], [[variance_x, 0], [0, variance_y]])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
ax.plot_surface(X, Y, rv.pdf(pos),cmap='viridis',linewidth=0)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()