Matplotlib:在 3D 表面上绘制 3D 曲线
Matplotlib: Plot 3D curve on top of 3D surface
我试图在 Matplotlib 中的 3D 表面上绘制 3D 曲线,但是当曲线在表面前面时,它几乎看不到(曲线是单位圆的图像):
应该有一条绿色曲线漂浮在表面上方一个单位。
如果从侧面看,可以看到不在曲面前面的曲线部分(曲面中心峰左右两侧的小环):
有什么方法可以让它工作吗?我试过白色表面,有和没有抗锯齿,但都无济于事。
这是我正在使用的代码:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
N = 5
# Make H surface
X = np.linspace(-1.25, 1.25, 1024)
Y = np.linspace(-1.25, 1.25, 1024)
X, Y = np.meshgrid(X, Y)
Z = 1/N * np.abs(1 - (X + Y*1j)**-N) / np.abs(1 - 1/(X + Y*1j))
Z = 20 * np.log10(Z)
# Make the image of the unit circle
omega = np.linspace(0, 2*np.pi, 2048)
circ_X = np.cos(omega)
circ_Y = np.sin(omega)
circ_Z = 1/N * np.sin(N*omega/2) / np.sin(omega/2)
circ_Z = 10 * np.log10(circ_Z**2) + 1
# Plot the H surface and the unit circle
surf = ax.plot_surface(X, Y, Z, cmap=cm.plasma, linewidth=0, antialiased=True)
circ = ax.plot(circ_X, circ_Y, circ_Z, color='green')
ax.set_zlim(-40, 10)
plt.show()
为了回答我自己的问题,我最终使用了 Mayavi:
#!/usr/bin/env python3
import mayavi.mlab as mlab
import numpy as np
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
N = 5
max = 15
min = -60
# Make H surface
X = np.linspace(-1.5, 1.5, 1024 * 5)
Y = np.linspace(-1.5, 1.5, 1024 * 5)
X, Y = np.meshgrid(X, Y)
z = X + Y * 1j
Z = 1 / N * np.abs(1 - z**-N) / np.abs(1 - 1 / z)
Z = 20 * np.log10(Z)
Z[np.logical_or(Z > max, Z < min)] = np.nan
Z = Z / 40 + 0.6
# Make the image of the unit circle
omega = np.linspace(2 * np.pi / 2048, 2 * np.pi - 2 * np.pi / 2048, 2047)
circ_X = np.cos(omega)
circ_Y = np.sin(omega)
circ_Z = 1 / N * np.sin(N * omega / 2) / np.sin(omega / 2)
circ_Z = 10 * np.log10(circ_Z**2) + 0.1
circ_Z[np.logical_or(circ_Z > max, circ_Z < min)] = min
circ_Z = circ_Z / 40 + 0.6
# Plot the H surface and the unit circle
mlab.figure(size=(1080 * 2, 720 * 2),
bgcolor=(1.0, 1.0, 1.0),
fgcolor=(.6, .1, .1))
mlab.surf(-np.transpose(X),
np.transpose(Y),
np.transpose(Z),
extent=[-1.5, 1.5, -1.5, 1.5, min / 40 + 0.6, max / 40 + 0.6],
opacity=0.9,
colormap='ocean')
mlab.plot3d(-circ_X, circ_Y, circ_Z, tube_radius=None)
view = (90.0, 60, 5, (0, 0, 0))
mlab.view(*view, reset_roll=True)
mlab.savefig(
os.path.join(os.path.dirname(script_dir), f'images/SMA-H-surf-N{N}.png'))
我试图在 Matplotlib 中的 3D 表面上绘制 3D 曲线,但是当曲线在表面前面时,它几乎看不到(曲线是单位圆的图像):
如果从侧面看,可以看到不在曲面前面的曲线部分(曲面中心峰左右两侧的小环):
有什么方法可以让它工作吗?我试过白色表面,有和没有抗锯齿,但都无济于事。
这是我正在使用的代码:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
N = 5
# Make H surface
X = np.linspace(-1.25, 1.25, 1024)
Y = np.linspace(-1.25, 1.25, 1024)
X, Y = np.meshgrid(X, Y)
Z = 1/N * np.abs(1 - (X + Y*1j)**-N) / np.abs(1 - 1/(X + Y*1j))
Z = 20 * np.log10(Z)
# Make the image of the unit circle
omega = np.linspace(0, 2*np.pi, 2048)
circ_X = np.cos(omega)
circ_Y = np.sin(omega)
circ_Z = 1/N * np.sin(N*omega/2) / np.sin(omega/2)
circ_Z = 10 * np.log10(circ_Z**2) + 1
# Plot the H surface and the unit circle
surf = ax.plot_surface(X, Y, Z, cmap=cm.plasma, linewidth=0, antialiased=True)
circ = ax.plot(circ_X, circ_Y, circ_Z, color='green')
ax.set_zlim(-40, 10)
plt.show()
为了回答我自己的问题,我最终使用了 Mayavi:
#!/usr/bin/env python3
import mayavi.mlab as mlab
import numpy as np
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
N = 5
max = 15
min = -60
# Make H surface
X = np.linspace(-1.5, 1.5, 1024 * 5)
Y = np.linspace(-1.5, 1.5, 1024 * 5)
X, Y = np.meshgrid(X, Y)
z = X + Y * 1j
Z = 1 / N * np.abs(1 - z**-N) / np.abs(1 - 1 / z)
Z = 20 * np.log10(Z)
Z[np.logical_or(Z > max, Z < min)] = np.nan
Z = Z / 40 + 0.6
# Make the image of the unit circle
omega = np.linspace(2 * np.pi / 2048, 2 * np.pi - 2 * np.pi / 2048, 2047)
circ_X = np.cos(omega)
circ_Y = np.sin(omega)
circ_Z = 1 / N * np.sin(N * omega / 2) / np.sin(omega / 2)
circ_Z = 10 * np.log10(circ_Z**2) + 0.1
circ_Z[np.logical_or(circ_Z > max, circ_Z < min)] = min
circ_Z = circ_Z / 40 + 0.6
# Plot the H surface and the unit circle
mlab.figure(size=(1080 * 2, 720 * 2),
bgcolor=(1.0, 1.0, 1.0),
fgcolor=(.6, .1, .1))
mlab.surf(-np.transpose(X),
np.transpose(Y),
np.transpose(Z),
extent=[-1.5, 1.5, -1.5, 1.5, min / 40 + 0.6, max / 40 + 0.6],
opacity=0.9,
colormap='ocean')
mlab.plot3d(-circ_X, circ_Y, circ_Z, tube_radius=None)
view = (90.0, 60, 5, (0, 0, 0))
mlab.view(*view, reset_roll=True)
mlab.savefig(
os.path.join(os.path.dirname(script_dir), f'images/SMA-H-surf-N{N}.png'))