Issue
I'm currently having a small problem with plotting several different lines in a 3d plot. I have a list of lists containing three numpy arrays corresponding to the xyz coordinates for the three points on each line, i.e.
lines=[[array([10,0,0]),array([10,0,101.5]),array([-5,0,250])],[array([9,0,0]), array([9,0,101.5]),array([-4,0,250])]]
would represent 2 lines with 3 sets of xyz coordinates in each (the first one here would be (10,0,0),(10,0,101.5) and (-5,0,250)).
In general I would have n lines in this list each with 3 sets of xyz coordinates each. I would like to plot these lines on a single 3d plot with matplotlib. All I've managed to do so far is to create n plots each containing a single line.
Thanks for the help!
EDIT: I have a list 'lines' containing 'line' objects which are just lists themselves containing 3 numpy arrays for the 3 points on each line. I tried to use the following method:
for line in lines:
fig = plt.figure()
ax = fig.gca(projection='3d')
z = []
for i in [0,1,2]:
z.append(line[i][2])
x = []
for i in [0,1,2]:
x.append(line[i][0])
y = []
for i in [0,1,2]:
y.append(line[i][1])
ax.plot(x, y, z, label='path')
plt.show()
I think I understand why this gives me 2 plots of lines 1 and 2 but I can't figure out a way to put both lines on the same plot.
Solution
You almost got it. The solution to your problem is simple, just move required statments out of for loop:
import matplotlib.pyplot as plt
lines=[[array([10,0,0]),array([10,0,101.5]),array([-5,0,250])],[array([9,0,0]), array([9,0,101.5]),array([-4,0,250])]]
fig = plt.figure()
ax = fig.gca(projection='3d')
for line in lines:
z = []
for i in [0,1,2]:
z.append(line[i][2])
x = []
for i in [0,1,2]:
x.append(line[i][0])
y = []
for i in [0,1,2]:
y.append(line[i][1])
ax.plot(x, y, z, label='path')
plt.show()
Answered By - Budo Zindovic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.