Python Matplotlib:ax.format_coord()在3D trisurf绘图中-返回(x,y,z)而不是(方位角,仰角)?

Python Matplotlib:ax.format_coord()在3D trisurf绘图中-返回(x,y,z)而不是(方位角,仰角)?,python,matplotlib,3d,mouseevent,Python,Matplotlib,3d,Mouseevent,我试图重做这个已经回答过的问题,但不能得到同样的结果,正如上面所说的。所以,我有一个类似的代码: import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from plyfile import PlyData, PlyElement #Handle the "onclick" event def onclick(event): print('%s click:

我试图重做这个已经回答过的问题,但不能得到同样的结果,正如上面所说的。所以,我有一个类似的代码:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from plyfile import PlyData, PlyElement

#Handle the "onclick" event
def onclick(event):
    print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          ('double' if event.dblclick else 'single', event.button,
           event.x, event.y, event.xdata, event.ydata))
    print(gety(event.xdata, event.ydata))

#copied from https://stackoverflow.com/questions/6748184/matplotlib-plot-surface-get-the-x-y-z-values-written-in-the-bottom-right-cor?rq=1
def gety(x,y):
    s = ax.format_coord(x,y)
    print(s) #here it prints "azimuth=-60 deg, elevation=30deg"
    out = ""
    for i in range(s.find('y')+2,s.find('z')-2):
        out = out+s[i]
    return float(out)

#Read a PLY file and prepare it for display
plydata = PlyData.read("some.ply")
mesh = plydata.elements[0]
triangles_as_tuples = [(x[0], x[1], x[2]) for x in plydata['face'].data['vertex_indices']]
polymesh = np.array(triangles_as_tuples)

#Display the loaded triangular mesh in 3D plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(mesh.data['x'], mesh.data['y'], mesh.data['z'], triangles=polymesh, linewidth=0.2, antialiased=False)
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
这样,三角形曲面将正确显示(尽管速度较慢)。我可以在右下角看到曲面的(x,y,z)坐标,同时将鼠标悬停在绘图上。但是,当我尝试通过点击鼠标(通过连接的事件处理程序)获取这些坐标时,ax.format_coord(x,y)fction返回的不是一个笛卡尔坐标字符串,而是一个“方位角=-60度,仰角=30度”的字符串,无论我在绘图中的何处单击,直到曲面旋转为止。然后它返回另一个值。从这里,我想这些是当前视图的球坐标,而不是点击点,出于某种原因

有人能发现我做错了什么吗?如何获得曲面上的笛卡尔坐标


仅供参考:这一切都与我之前的问题有关,这个问题被认为过于宽泛和笼统。

按下鼠标按钮是ax.format_coord的触发器,用于返回3D绘图上的角度坐标,而不是笛卡尔坐标。因此,一个选项是让ax.format_coord认为没有按下任何按钮,在这种情况下,它将根据需要返回通常的笛卡尔x,y,z坐标

要实现这一点,即使您单击了鼠标按钮,也有一种不合理的方法,即在调用该函数时,将按下的
ax.按钮(存储当前鼠标按钮)设置为不合理的值

def gety(x,y):
    # store the current mousebutton
    b = ax.button_pressed
    # set current mousebutton to something unreasonable
    ax.button_pressed = -1
    # get the coordinate string out
    s = ax.format_coord(x,y)
    # set the mousebutton back to its previous state
    ax.button_pressed = b
    return s

从二维单击事件获取三维坐标无论如何都不是很有用,是吗?我的意思是2D中的一个点是3D中的一条完整的线。你想得到直线方程吗?请看。我是说,你到底为什么要展示一些无人能运行的代码?@ImportanceOfBeingErnest感谢你指出示例代码不起作用。我已经验证过了,现在应该可以正常工作了。请尝试(使用同一文件夹中的任何标准PLY文件)。@importanceofbeinger如果您是对的,2D中的点是3D中的一条线,但它与曲面的交点数量有限。通常只有一两个。我想挑一个离观众最近的。MatMattLB似乎能够正确地解决这个问题,因为它显示了窗口右下角所需的坐标。如果你考虑下角的MatPultLIB所显示的坐标是有用的,你可以使用下面提供的答案。但我不知道为什么。你能解释一下,为什么有必要抑制鼠标按钮事件吗?我试着解释得更详细一些。如果没有帮助的话,请随时询问到底什么是不清楚的。