如何使用 matplotlib 绘制 3d 高斯分布? [英] How to plot 3d gaussian distribution with matplotlib?

查看:109
本文介绍了如何使用 matplotlib 绘制 3d 高斯分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了3d高斯分布的均值和西格玛,然后我想用python代码绘制3d分布,并获得分布图.

I have obtained the means and sigmas of 3d Gaussian distribution, then I want to plot the 3d distribution with python code, and obtain the distribution figure.

推荐答案

这是基于 mpl_toolkits 的文档和基于 scipy multinormal pdf 的 SO 的答案:

This is based on documentation of mpl_toolkits and an answer on SO based on scipy multinormal pdf:

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

import numpy as np
from scipy.stats import multivariate_normal

x, y = np.mgrid[-1.0:1.0:30j, -1.0:1.0:30j]

# Need an (N, 2) array of (x, y) pairs.
xy = np.column_stack([x.flat, y.flat])

mu = np.array([0.0, 0.0])

sigma = np.array([.5, .5])
covariance = np.diag(sigma**2)

z = multivariate_normal.pdf(xy, mean=mu, cov=covariance)

# Reshape back to a (30, 30) grid.
z = z.reshape(x.shape)





fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')



ax.plot_surface(x,y,z)
#ax.plot_wireframe(x,y,z)

plt.show()

参考:-

  1. 在 Python 中生成 3D 高斯分布

https://matplotlib.org/tutorials/toolkits/mplot3d.html#sphx-glr-tutorials-toolkits-mplot3d-py

这篇关于如何使用 matplotlib 绘制 3d 高斯分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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