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

标签 python matplotlib 3d mouseevent

我试图重做这个已经回答的问题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 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) 函数返回的不是笛卡尔坐标字符串,而是“azimuth=-60 deg,海拔=30deg”,无论我在图中的哪个位置单击,直到表面旋转。然后它返回另一个值。由此,我认为出于某种原因,这些是当前 View 的球面坐标,而不是单击的点......

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

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

最佳答案

按下鼠标按钮会触发 ax.format_coord返回 3D 绘图上的角坐标而不是笛卡尔坐标。所以一个选择是让 ax.format_coord认为没有按下任何按钮,在这种情况下,它将根据需要返回通常的笛卡尔 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

关于python - Matplotlib:3D trisurf 图中的 ax.format_coord() - 返回 (x,y,z) 而不是(方位角,仰角)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50283031/

相关文章:

python - 下面的背包填充算法的实现有什么问题?

python - 在matplotlib trisurf 3d图python上反转y轴

python - Matplotlib pyplot - 刻度控件和显示日期

3d - 点云XYZ格式规范

math - 透视圆形形状的实用方法?

python - (1,2) 是 (0, 3) 的子集,但如何在 sympy 中表示它?

python - django,属性更新模型实例

python - 像素网格中非相邻单元的随机采样

python-3.x - 根据 CityGML 数据绘制建筑表面

math - 提取视锥体平面(Gribb 和 Hartmann 方法)