简体   繁体   English

使用 matplotlib 交互式选择 3D 图形上的点

[英]Interactively select points on 3D graph using matplotlib

I have been trying to select certain points in a 3D graph.我一直在尝试在 3D 图形中选择某些点。 I want the code to print the points selected by a mouse click so then I can calculate the distance between two selected points.我希望代码打印通过鼠标单击选择的点,这样我就可以计算两个选定点之间的距离。 I am not very experienced so I have tried multiple ways to do it.我不是很有经验,所以我尝试了多种方法来做到这一点。 My code:我的代码:

在此处输入图片说明

%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mp
from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 

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

x1 = -100 + df1['N']
y1 = -1*df1['V']
z1 = -2.588 + df1['E']

x2 = df2['N']
y2 = -1*df2['V']
z2 = -1.294+df2['E']

ax.plot (x1,y1,z1)
ax.plot (x2,y2,z2)

ax.set_xlabel('N axis')
ax.set_ylabel('V axis')
ax.set_zlabel('E axis')

ax.view_init(elev=93 , azim=-89  )
projections = []
ax3d = fig.gca(projection='3d')
def on_click(event):
    azim, elev = ax3d.azim, ax3d.elev
    projections.append((azim, elev))
    print(azim, elev)

cid = fig.canvas.mpl_connect('button_release_event', on_click)

def onclick(event):
    print('%s click: button=%d, x=%d, y=%d, z=%d, xdata=%f, ydata=%f, zdata=%f' %
          ('double' if event.dblclick else 'single', event.button,
           event.x, event.y, evente.z, event.xdata, event.ydata, event.zdata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)


plt.show()

We use the format_coord() function of the axes to get the coordinates.我们使用轴的 format_coord() 函数来获取坐标。 But the format_coord() function returns azimuth and elevation when the button is pressed.但是 format_coord() 函数在按下按钮时返回方位角和仰角。 We can set the button presses to some value that doesn't make sense, call the format_coord to return the coordinates and then set the button presses to the original value.我们可以将按钮按下设置为一些没有意义的值,调用 format_coord 返回坐标,然后将按钮按下设置为原始值。

def on_click(event):
    pressed = ax.button_pressed
    ax.button_pressed = -1 # some value that doesn't make sense.
    coords = ax.format_coord(event.xdata, event.ydata) # coordinates string in the form x=value, y=value, z= value
    ax.button_pressed = pressed
    return coords

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM