Python Forum
Plot multiple 2D Curves in 3D
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot multiple 2D Curves in 3D
#1
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
Reply
#2
at minimum, missing imports, definition of plt
please, At least supply minimum snippit
Reply
#3
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
Reply
#4
It doesn't fail for me, but axis is off on one plot
   
Reply
#5
(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
Reply
#6
(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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,695 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple Plotting in Same PLot quest 0 1,810 Apr-18-2021, 10:29 AM
Last Post: quest
  How to plot intraday data of several days in one plot mistermister 3 2,933 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Level curves don't match after rotation schniefen 1 1,550 Dec-14-2020, 09:56 PM
Last Post: schniefen
  plot multiple employee sales data in a single graph pitanshu 0 1,921 Oct-24-2019, 01:56 PM
Last Post: pitanshu
  how to show the distance between two curves in a graph termo 6 7,043 Oct-21-2019, 09:08 AM
Last Post: DeaD_EyE
  Plot multiple csv into one graph problem with visualize linkxxx86 1 5,736 Oct-14-2019, 05:54 PM
Last Post: linkxxx86
  How to plot multiple scatter plots in seaborn vikola 2 6,513 Jul-14-2019, 10:30 AM
Last Post: vikola
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,942 Jun-12-2019, 03:31 PM
Last Post: SriMekala
  Finding the intersection of interpolated curves Tina 1 4,168 Mar-23-2017, 05:33 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020