[英]Creating a 3D plot of several lines in one figure
I'm currently having a small problem with plotting several different lines in a 3d plot.我目前在 3d 图中绘制几条不同的线时遇到了一个小问题。 I have a list of lists containing three numpy arrays corresponding to the xyz coordinates for the three points on each line, ie我有一个列表列表,其中包含三个 numpy 数组,这些数组对应于每条线上三个点的 xyz 坐标,即
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)).将代表 2 条线,每条线有 3 组 xyz 坐标(这里的第一个是 (10,0,0)、(10,0,101.5) 和 (-5,0,250))。
In general I would have n lines in this list each with 3 sets of xyz coordinates each.一般来说,我会在这个列表中有 n 行,每行有 3 组 xyz 坐标。 I would like to plot these lines on a single 3d plot with matplotlib.我想用 matplotlib 在单个 3d 图上绘制这些线。 All I've managed to do so far is to create n plots each containing a single line.到目前为止,我所做的只是创建 n 个图,每个图都包含一条线。
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.编辑:我有一个包含 'line' 对象的列表 'lines',这些对象只是列表本身,其中包含每行 3 个点的 3 个 numpy 数组。 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.我想我明白为什么这给了我第 1 行和第 2 行的 2 个图,但我想不出将两条线放在同一个图上的方法。
You almost got it.你几乎明白了。 The solution to your problem is simple, just move required statments out of for loop:解决您的问题很简单,只需将所需的语句移出 for 循环:
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()
I had a similar problem trying to plot a 3D path between locations, and this was about the closest / most helpful solution I found.我在尝试绘制位置之间的 3D 路径时遇到了类似的问题,这是我找到的最接近/最有用的解决方案。 So just if anybody else is trying to do this and might find this similar solution sheds a bit of light :因此,如果其他人正在尝试这样做并且可能会发现这个类似的解决方案会有所启发:
for location in list_of_locations:
x_list.append(locdata[location].x) # locdata is a dictionary with the co-ordinates of each named location
y_list.append(locdata[location].y)
z_list.append(locdata[location].z)
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in range(len(x_list)-1):
xs = [x_list[i], x_list[i+1]]
ys = [y_list[i], y_list[i+1]]
zs = [z_list[i], z_list[i+1]]
ax.plot(xs,ys,zs)
plt.show()
I'm sure it doesn't need to be two separate for loops but for my little data set this was totally fine, and easy to read.我确信它不需要是两个单独的 for 循环,但对于我的小数据集来说,这完全没问题,而且易于阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.