如何突出显示 3D 曲面图上的切片?

问题描述 投票:0回答:2

我有一个 3D 曲面图。我还想以 2D 形式绘制该图的切片,并以某种方式在 3D 图上指示切片的来源(例如对切片上的点进行着色以“突出显示”切片,或绘制相交平面或其他内容)。

下面是一个示例,我只是将特定行设置为 0,这样我就可以看到切片在 3D 绘图上的位置。

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


# Grid and test function
N = 29;
x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
X,Y = np.meshgrid(x,y)
F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
Z = F(X,Y)

# 3D Surface plot
plt.figure(figsize = (5,6))
Z2 = Z.copy(); Z2[10,:] = 0 # <----- Replace this code
ax = plt.subplot(211, projection='3d')
ax.plot_surface(X,Y,Z2)

# 2D Plot of slice of 3D plot 
plt.subplot(212)
plt.plot(x,Z[10,:])
plt.show()

plt.savefig('surfacePlotHighlight.png')

python matplotlib surface matplotlib-3d
2个回答
2
投票

您可以使用

facecolor
中的
plot_surface
选项以及
plot
中类似的颜色设置为 X 或 Y 方向的切片着色。例如

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

# Grid and test function
N = 29;
x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
X,Y = np.meshgrid(x,y)
F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
Z = F(X,Y)

# 3D Surface plot
plt.figure(figsize = (5,6))
ax = plt.subplot(211, projection='3d')
# Normalise Y for calling in the cmap.
Ys = Y/Y.max()
cmap = plt.cm.viridis
ax.plot_surface(X, Y, Z2, facecolors=cmap(Ys))

# 2D Plot of slice of 3D plot 
# Normalise y for calling in the cmap.
ys = y/y.max()
plt.subplot(212)
plt.plot(x,Z[10,:], color=cmap(ys[10]))
plt.plot(x,Z[20,:], color=cmap(ys[20]))
plt.show()

plt.savefig('surfacePlotHighlight.png')

编辑: 这可用于通过编辑颜色数组以调出特定单元格来突出显示单行(或列,或任意点集),例如:

# 3D Surface plot
plt.figure(1,figsize = (5,6))
ax = plt.subplot(211, projection='3d')
# Create array to specify color of each pixel on surface
Ys = Y*0
Ys[:,:] = .3
Ys[10] = 1
Ys[20] = .7
cmap = plt.cm.viridis
ax.plot_surface(X, Y, Z, facecolors=cmap(Ys))

# 2D Plot of slice of 3D plot 
# Normalise y for calling in the cmap.
ys = Ys[:,0]
plt.subplot(212)
plt.plot(x,Z[10,:], color=cmap(ys[10]))
plt.plot(x,Z[20,:], color=cmap(ys[20]))
plt.show()

plt.savefig('surfacePlotHighlight.png')


2
投票

您可以对以与其他行不同的颜色显示的行进行着色。

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


# Grid and test function
N = 29;
x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
X,Y = np.meshgrid(x,y)
F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
Z = F(X,Y)


y0 = 10
norm=plt.Normalize(Z.min(), Z.max())
C = plt.cm.Blues_r(norm(Z)/2)
C[y0] = plt.cm.Reds_r(norm(Z[y0])/2)
# 3D Surface plot
plt.figure(figsize = (5,6))

ax = plt.subplot(211, projection='3d')
ax.plot_surface(X,Y,Z, facecolors=C)

# 2D Plot of slice of 3D plot 
plt.subplot(212)
plt.plot(x,Z[y0,:], color=plt.cm.Reds(.7))
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.