Python 如何添加动态等高线图
我用juypter写了一些代码来可视化二元正态分布。我想修改代码,以便可以同时可视化等高线图(等密度,比如x-y曲面)。我应该补充什么Python 如何添加动态等高线图,python,matplotlib,contourf,Python,Matplotlib,Contourf,我用juypter写了一些代码来可视化二元正态分布。我想修改代码,以便可以同时可视化等高线图(等密度,比如x-y曲面)。我应该补充什么 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib as mpl %matplotlib if __name__ == '__main__': mpl.rcParams['font.sans
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
%matplotlib
if __name__ == '__main__':
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
d = np.random.randn(10000000, 2)
N = 30
density, edges = np.histogramdd(d, bins=[30, 30])
print("样本总数: ", np.sum(density))
density = density/density.max()
x = y = np.arange(N)
t = np.meshgrid(x,y)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(t[0], t[1], density, c='r', s=15*density, marker='o', depthshade=True)
ax.plot_surface(t[0], t[1], density, cmap='rainbow', rstride=1, cstride=1, alpha=0.9, lw=1)
cset = ax.contourf(x, y, density,
zdir ='z',
offset = np.min(density),
)
ax.set_xlabel("x轴")
ax.set_ylabel("y轴")
ax.set_zlabel("z轴")
plt.title("二元高斯分布")
# plt.tight_layout(0.1)
plt.show()
在plot_surface()
中,使用alpha=0.7并尝试使用contour()
代替contourf()
。在plot_surface()
中,使用alpha=0.7并尝试使用contour()
代替contourf()
。