Plot normal distribution in 3D

后端 未结 3 623
无人及你
无人及你 2020-12-29 15:38

I am trying to plot the comun distribution of two normal distributed variables.

The code below plots one normal distributed variable. What would the code be for plo

3条回答
  •  抹茶落季
    2020-12-29 16:04

    The following adaption to @Ianhi's code above returns a contour plot version of the 3D plot above.

    import matplotlib.pyplot as plt
    from matplotlib import style
    style.use('fivethirtyeight')
    import numpy as np
    from scipy.stats import multivariate_normal
    
    
    
    
    #Parameters to set
    mu_x = 0
    variance_x = 3
    
    mu_y = 0
    variance_y = 15
    
    x = np.linspace(-10,10,500)
    y = np.linspace(-10,10,500)
    X,Y = np.meshgrid(x,y)
    
    pos = np.array([X.flatten(),Y.flatten()]).T
    
    
    
    rv = multivariate_normal([mu_x, mu_y], [[variance_x, 0], [0, variance_y]])
    
    
    fig = plt.figure(figsize=(10,10))
    ax0 = fig.add_subplot(111)
    ax0.contour(rv.pdf(pos).reshape(500,500))
    
    
    
    plt.show()
    

提交回复
热议问题