Python 中的 3D 概率密度图

3D Probability Density Plots in Python

我正在处理一个数据集,该数据集由 'pulse length' 个值和对应于每个脉冲长度的 5 或 6 个 'voltage values' 组成。第一个值是脉冲长度,后跟电压。找到下面的table。

15 -56V -47V -53V -50V -50V


16 -49V -46V -52V -47V -50V


17 -50V -51V -47V -50V -49V


18 -50V -51V -48V -48V -45V


19 -49V -51V -45V -47V -52V


20 -45V -47V -50V -47V -54V


21 -46V -52V -52V -49V -54V


22 -53V -51V -53V -56V -52V


23 -52V -45V -51V -56V -53V


24 -51V -52V -54V -58V -52V


25 -56V -53V -57V -55V -53V


26 -53V -52V -55V -52V


27 -54V -49V -56V -54V


28 -52V -52V -57V -56V -53V


29 -63V -60V -54V -58V -61V


30 -59V -70V -61V


我希望 X 轴和 Y 轴是脉冲长度和电压,我希望 Z 轴是它的概率分布。我有一个使用 'voltage values' 集及其概率的二维图。 图中,红色图对应一个脉冲长度,绿色图对应另一个脉冲长度。我尝试使用来自堆栈溢出 () 的多元正态分布示例以相同的方式绘制 3D 图。由于我对 3D 图的经验很少,因此我无法在具有不同 Y 轴 'pulse length' 值的同一表面上绘制多个表面图。我试过的代码如下。

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

 #Parameters to set
 mu_x = -48.8
 sigma_x = np.sqrt(6.5)

 mu_y = 0
 sigma_y = np.sqrt(16)

 #Create grid and multivariate normal
 x = range(-100,0)
 y = range(15,30)
 X, Y = np.meshgrid(x,y)
 Z = bivariate_normal(X,Y,sigma_x,sigma_y,mu_x,mu_y)


 #Make a 3D plot
 fig = plt.figure()
 ax = fig.gca(projection='3d')
 ax.plot_surface(X, Y, Z,cmap='Reds',linewidth=0, antialiased=True, 
 zorder = 0.5)

 ax.set_xlabel('Voltage')
 ax.set_ylabel('Pulse Length')
 ax.set_zlabel('Normal Distribution')
 plt.show()

如果有人能帮我对多个脉冲长度做同样的事情,我将非常感激。 谢谢。

我不知道你到底想实现什么样的情节,但据我了解,你想要的是下图这样的东西。我只把 relevant/modified 代码放在下面。也不清楚您的脉冲长度是什么变量。由于您有很多脉冲长度,您可以将定义 mu_x、'mu_y'、Z 的函数放在 for 循环中并绘制几个 3d 表面。

# Create grid and multivariate normal
x = np.linspace(-100, 0, 200) # Create a mesh of 200 x-points
y = np.linspace(-30, 30, 200) # Create a mesh of 200 y-points

X, Y = np.meshgrid(x,y)
Z = bivariate_normal(X,Y,sigma_x,sigma_y,mu_x,mu_y)
Z2 = bivariate_normal(X,Y,sigma_x,sigma_y,mu_x-20,mu_y+10)

fig = plt.figure(figsize=(10, 8))
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z,cmap='Reds',linewidth=0, antialiased=True, zorder = 0.5)
ax.plot_surface(X, Y, Z2,cmap='Blues',linewidth=0, alpha=0.5, antialiased=True, zorder = 0.5)

输出