Python Forum

Full Version: Plot multiple 2D Curves in 3D
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

i want to plot 3 different 2D curves in a 3D coordinate system, so lets say 3 simple sine functions. One in x and y direction, one in x and z direction and one y and z direction.
My Code already looks like:
ax = plt.figure().add_subplot(projection='3d')

# Plot a sin curve using the x and y axes.
x = np.linspace(0, 1, 100)
y = np.sin(x * 2 * np.pi) / 2 + 0.5
ax.plot(x, y, zs=0, zdir='z', label='curve in (x, y)')

y = np.linspace(0, 1, 100)
z = np.sin(x * 2 * np.pi) / 2 + 0.5
ax.plot(xs=0,ys=y,xz=z, zdir='z', label='curve in (y, x)')

# 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')
But it throws an Error:
Error:
x and y must have same first dimension, but have shapes (1,) and (100,)
With only one function in x and y direction it works just fine.

Can someone help me with this? Thank you in advance,

Maxi
at minimum, missing imports, definition of plt
please, At least supply minimum snippit
Okay so snippit is:

import numpy as np
import matplotlib.pyplot as plt

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

# Plot a sin curve using the x and y axes.
x = np.linspace(-1, 1, 100)
y = x**2
ax.plot(x, y, zs=0, zdir='z', label='curve in (x, y)')
x = np.linspace(-1, 1, 100)
y = x**2+2
ax.plot(x, y, zs=0, zdir='z', label='curve in (x, y)')
# Make legend, set axes limits and labels
ax.legend()
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Customize the view angle so it's easier to see that the scatter points lie
# on the plane y=0

plt.show()
Hope it helps
It doesn't fail for me, but axis is off on one plot
[attachment=2400]
(Jun-11-2023, 10:48 AM)Larz60+ Wrote: [ -> ]It doesn't fail for me, but axis is off on one plot

(Jun-11-2023, 10:48 AM)Larz60+ Wrote: [ -> ]It doesn't fail for me, but axis is off on one plot
Yes it works in x and y direction, but i also want to plot the function for example in y and z direction. If you try this it will fail
(Jun-11-2023, 10:57 AM)stc Wrote: [ -> ]but i also want to plot the function for example in y and z direction
Try this.
import numpy as np
import matplotlib.pyplot as plt

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

# Plot a sin curve using the y and z axes.
y = np.linspace(-1, 1, 100)
z = y**2
ax.plot([0]*len(y), y, z, label='curve in (y, z)')

y = np.linspace(-1, 1, 100)
z = y**2 + 2
ax.plot([0]*len(y), y, z, label='curve in (y, z)')

# Make legend, set axes limits and labels
ax.legend()
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 3)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()