带选定点的 3D 线图
3D Line Drawing with selected point
我正在使用 matplotlib,我想 select 指向我的 3D 散点图并将它们与 line.I 可以读取 (X,Y,Z) 值但我找不到我如何连接 line.Here 是我的代码。
提前致谢
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#Coordinates of Grid Lines
X_Grid=[0,4] #Joint Points X
Y_Grid=[0,5] #Joint Points Y
Z_Grid=[0,3,5]#Joint Points Z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Select Point')
X,Y,Z=list(),list(),list()
for i in X_Grid:
for j in Y_Grid:
for k in Z_Grid:
X.append(i)
Y.append(j)
Z.append(k)
ax.scatter(X,Y,Z,marker="*",c="green",picker=5)
def onpick(event):
ind = event.ind[0]
x, y, z = event.artist._offsets3d
print((x[ind],y[ind],z[ind]))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
要在绘图中画一条线,您只需从 onpick()
函数中调用 plot()
。当然,这意味着您需要跟踪您点击的点的坐标。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Coordinates of Grid Lines
X_Grid = [0, 4] # Joint Points X
Y_Grid = [0, 5] # Joint Points Y
Z_Grid = [0, 3, 5] # Joint Points Z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Select Point')
X, Y, Z = list(), list(), list()
for i in X_Grid:
for j in Y_Grid:
for k in Z_Grid:
X.append(i)
Y.append(j)
Z.append(k)
ax.scatter(X, Y, Z, marker="*", c="green", picker=5)
picked = []
def onpick(event):
ind = event.ind[0]
x, y, z = event.artist._offsets3d
# print((x[ind],y[ind],z[ind]))
picked.append((x[ind], y[ind], z[ind]))
print(picked)
if len(picked) == 2:
# draw the line
temp = np.array(picked) # use a numpy array to simplify notation
ax.plot(temp[:, 0], temp[:, 1], temp[:, 2])
ax.figure.canvas.draw_idle()
picked[:] = [] # reset list for future pick events
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
我正在使用 matplotlib,我想 select 指向我的 3D 散点图并将它们与 line.I 可以读取 (X,Y,Z) 值但我找不到我如何连接 line.Here 是我的代码。
提前致谢
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#Coordinates of Grid Lines
X_Grid=[0,4] #Joint Points X
Y_Grid=[0,5] #Joint Points Y
Z_Grid=[0,3,5]#Joint Points Z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Select Point')
X,Y,Z=list(),list(),list()
for i in X_Grid:
for j in Y_Grid:
for k in Z_Grid:
X.append(i)
Y.append(j)
Z.append(k)
ax.scatter(X,Y,Z,marker="*",c="green",picker=5)
def onpick(event):
ind = event.ind[0]
x, y, z = event.artist._offsets3d
print((x[ind],y[ind],z[ind]))
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
要在绘图中画一条线,您只需从 onpick()
函数中调用 plot()
。当然,这意味着您需要跟踪您点击的点的坐标。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Coordinates of Grid Lines
X_Grid = [0, 4] # Joint Points X
Y_Grid = [0, 5] # Joint Points Y
Z_Grid = [0, 3, 5] # Joint Points Z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('Select Point')
X, Y, Z = list(), list(), list()
for i in X_Grid:
for j in Y_Grid:
for k in Z_Grid:
X.append(i)
Y.append(j)
Z.append(k)
ax.scatter(X, Y, Z, marker="*", c="green", picker=5)
picked = []
def onpick(event):
ind = event.ind[0]
x, y, z = event.artist._offsets3d
# print((x[ind],y[ind],z[ind]))
picked.append((x[ind], y[ind], z[ind]))
print(picked)
if len(picked) == 2:
# draw the line
temp = np.array(picked) # use a numpy array to simplify notation
ax.plot(temp[:, 0], temp[:, 1], temp[:, 2])
ax.figure.canvas.draw_idle()
picked[:] = [] # reset list for future pick events
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()