Matplotlib:3D trisurf 图中的 ax.format_coord() - return (x,y,z) 而不是(方位角,仰角)?

Matplotlib: ax.format_coord() in 3D trisurf plot - return (x,y,z) instead of (azimuth, elevation)?

我试图重做这个已经回答的问题 Matplotlib - plot_surface : get the x,y,z values written in the bottom right corner,但无法获得与此处所述相同的结果。所以,我有这样的代码:

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 
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) 函数 returns 不是一串笛卡尔坐标,而是一串 "azimuth=-60 deg, elevation=30deg",无论我点击图中的哪个位置,直到表面旋转。然后它 returns 另一个值。由此我想这些是当前视图的球坐标,而不是点击点,出于某种原因...

有人可以找出我做错了什么吗?如何获得曲面上的笛卡尔坐标?

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

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

即使您单击了鼠标按钮,实现此目的的一个有点老套的方法是在调用该函数时将 ax.button_pressed(存储当前鼠标按钮)设置为不合理的值。

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