使用公共 x 轴在 3d 图上绘制曲线

Plotting curves on 3d plot with common x-axis

我想在 3d 中绘制两条曲线,以便两条曲线共享 x 轴。一条曲线必须在 X-Z 平面上,而第二条曲线必须在 X-Y 平面上。

我尝试使用以下代码完成相同的操作。

import numpy as np
import matplotlib.pyplot as plt

ax = plt.figure().add_subplot(projection='3d')

x = np.linspace(0, 1, 100)
y = abs(np.sin(x*2*np.pi))

ax.plot(x, y, 'b', zs=0, zdir='x', label='curve in (x, z)')
ax.plot(x, y, 'r', zs=0, zdir='z', label='curve in (x, y)')
# Make legend, set axes limits and labels
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.view_init(elev=20., azim=-35)

plt.show()

我已经在 this figure. However, I would like to get something as shown in the right panel this figure 的左侧面板中展示了我使用此代码完成的工作。

任何帮助将不胜感激!

你可以像这样简单地做:

x = np.linspace(0, 1, 100)
y = np.abs(np.sin(x*2*np.pi))
zero = np.zeros_like(x)

ax.plot(zero, x, y, 'b', label='curve in (x, z)')
ax.plot(y, x, zero , 'r', label='curve in (x, y)')

你几乎答对了。您只需要翻转 x-y 轴。以下代码将为您完成。

import numpy as np
import matplotlib.pyplot as plt

ax = plt.figure().add_subplot(projection='3d')

x = np.linspace(0, 1, 100)
y = abs(np.sin(x*2*np.pi))

ax.plot(x, y, 'b', zs=0, zdir='x', label='curve in (x, z)')
ax.plot(y, x, 'r', zs=0, zdir='z', label='curve in (x, y)')
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.view_init(elev=20., azim=-35)

plt.show()