如何添加动态轮廓图

时间:2020-07-10 13:57:09

标签: python matplotlib contourf

我在juypter中编写了一些代码以可视化Bivariate正态分布。我想修改代码,以便可以同时可视化等高线图(密度,即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-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()

0 个答案:

没有答案