试图绘制多元高斯分布.在3D图中,matplotlib返回一个空图 [英] Trying to plot multivariate Gaussian dist. in a 3D plot matplotlib returns an empty figure

查看:146
本文介绍了试图绘制多元高斯分布.在3D图中,matplotlib返回一个空图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过matplotlib绘制高斯分布,但我得到的只是一个空图:

I am trying to plot a Gaussian distribution via matplotlib, but all I get back is an empty figure:

当我在互联网上搜索时,我了解到ax.plot_surface()函数需要三个参数,分别是X值,Y值和Z(该函数根据X,Y计算Z).这是正确的吗?

When I searched the internet, I understood that three arguments are required for the ax.plot_surface() function, the X values, Y values, and Z (a function that calculates Z from X, Y). Is this correct?

我在下面发布了代码,希望您能帮助我弄清楚我在做什么错.谢谢!

I post the code below in hope you can help me figuring out what I am doing wrong here. Thanks!

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

###############################################
### The multivariate Gaussian density function
###############################################

def pdf_multivariate_gauss(x, mu, cov):
    '''
    Caculate the multivariate normal density (pdf)

    Keyword arguments:
        x = numpy array of a "d x 1" sample vector
        mu = numpy array of a "d x 1" mean vector
        cov = "numpy array of a d x d" covariance matrix
    '''
    assert(mu.shape[0] > mu.shape[1]), 'mu must be a row vector'
    assert(x.shape[0] > x.shape[1]), 'x must be a row vector'
    assert(cov.shape[0] == cov.shape[1]), 'covariance matrix must be square'
    assert(mu.shape[0] == cov.shape[0]), 'cov_mat and mu_vec must have the same dimensions'
    assert(mu.shape[0] == x.shape[0]), 'mu and x must have the same dimensions'
    part1 = 1 / ( ((2* np.pi)**(len(mu)/2)) * (np.linalg.det(cov)**(1/2)) )
    part2 = (-1/2) * ((x-mu).T.dot(np.linalg.inv(cov))).dot((x-mu))
    return float(part1 * np.exp(part2))

# Test
x = np.array([[0],[0]])
mu  = np.array([[0],[0]])
cov = np.eye(2) 

print(pdf_multivariate_gauss(x, mu, cov))

#prints 0.15915494309189535

###############################################
### The plot
###############################################

mu  = np.array([[0],[0]])
cov = np.eye(2) 

def construct_Z(X, Y, mu, cov):
    Z = []
    for i,j in zip(X,Y):
        x = np.array([i,j]).reshape(2,1)
        Z.append(pdf_multivariate_gauss(x, mu, cov))
    return Z

X = linspace(-5, 5, 200)
Y = linspace(-5, 5, 200)
Z = construct_Z(X, Y, mu, cov)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color='0.8',
                       alpha=0.85, linewidth=1)     
plt.show()

推荐答案

我不是matplotlib中3D绘图的专家,但我认为您的数据有误.

I'm no expert with 3D-plots in matplotlib, but I believe your data wrong.

您可以在本教程的源代码中看到

As you can see in the sourcecode in this tutorial, your X,Y and Z data have to be 2-dimensional arrays. Your X and Y are one-dimensional, and your Z is a simple list.

尝试将数据重塑为网格,也许使用X, Y = np.meshgrid(X, Y)

Try reshaping your data to a grid, maybe using X, Y = np.meshgrid(X, Y)

这篇关于试图绘制多元高斯分布.在3D图中,matplotlib返回一个空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆