0

I would like to plot two curves in 3d such that the x-axis is shared for two curves. One curve must lie on the X-Z plane while the second curve must be on the X-Y plane.

I have tried to accomplish the same using with the following code.

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()

I have shown what I have accomplihed with this code in the left panel of this figure. However, I would like to get something as shown in the right panel this figure.

Any help would be deeply appreciated!

2 Answers2

0

You can do it simply like this:

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)')
Julien
  • 13,986
  • 5
  • 29
  • 53
0

You almost had it correct. You just needed to flip the x-y axis. The following code will do it for you. enter image description here

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()