在三维投影中填充绘图下方的空间

2024-06-25 05:40:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在3D绘图中填充曲线下方的区域。在2d绘图中,我已经使用了fill_between来表示类似的内容。在3d环境中是否有一个简单的等价物?曲线位于y-z平面中

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

def gaussian(x, mu, sig):
    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))



x=[-1,0,1,2,3,4]
y=[0,1,2,3,4,5]
sigma=0.4
m=1
b=1

Blankz=np.empty([0])
Blanky=np.empty([0])
Blankx=np.empty([0])
j=0
for i in np.arange(-0.5,2.5,0.01):
    Blankz=np.insert(Blankz,j,gaussian(i,1,sigma))
    Blanky=np.insert(Blanky,j,i)
    Blankx=np.insert(Blankx,j,0)
    j=j+1


fig=plt.figure(figsize=(16,6))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-1,5)
ax.set_ylim(-1,6)
ax.set_zlim(0,1)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.plot(x,y,0, color="blue",linestyle="dashed")

plt.plot(Blankx,Blanky,Blankz, color="red")
ax.view_init(60,320)

enter image description here 基本上我想要一个fill_-between函数,它填充从z=Blankz[I]到z=0的y=-0.5,2.5范围内的区域。我想填充高斯分布的区域


Tags: import区域绘图nppltbetweenaxfill
1条回答
网友
1楼 · 发布于 2024-06-25 05:40:57

试试^{}^{}

verts = [list(zip(Blanky, Blankz))]
poly = PolyCollection(verts, facecolors=[mcolors.to_rgba('y', alpha=0.6)])
ax.add_collection3d(poly, zs=[Blankx[0]], zdir='x')

完整代码

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import colors as mcolors


def gaussian(x, mu, sig):
    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))

x=[-1,0,1,2,3,4]
y=[0,1,2,3,4,5]
sigma=0.4
m=1
b=1

Blankz = np.empty([0])
Blanky = np.empty([0])
Blankx = np.empty([0])

for j, i in enumerate(np.arange(-0.5, 2.5, 0.01)):
    Blankz = np.insert(Blankz, j, gaussian(i,1,sigma))
    Blanky = np.insert(Blanky, j, i)
    Blankx = np.insert(Blankx, j, 0)



fig = plt.figure(figsize=(16,6))
ax = fig.gca(projection='3d')
ax.set_xlim(-1,5)
ax.set_ylim(-1,6)
ax.set_zlim(0,2)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

# create vertice and add it
verts = [list(zip(Blanky, Blankz))]
poly = PolyCollection(verts, facecolors=[mcolors.to_rgba('y', alpha=0.6)])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=[Blankx[0]], zdir='x')

plt.plot(x,y,0, color="blue",linestyle="dashed")
plt.plot(Blankx,  Blanky, Blankz, color="red")
ax.view_init(60,320)
plt.show()

输出enter image description here

相关问题 更多 >