python - Matplotlib:3D trisurf图中的ax.format_coord() - 返回(x,y,z)而不是(方位角,仰角)?
问题描述
我试图重做这个已经回答的问题Matplotlib - plot_surface : get the x,y,z values write 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) 函数返回的不是笛卡尔坐标字符串,而是“方位角=-60 度,高程= 30度”,无论我在图中的哪个位置单击,直到表面旋转。然后它返回另一个值。从这里我想这些是当前视图的球坐标,而不是点击的点,出于某种原因......
有人可以找出我做错了什么吗?如何获得表面上的笛卡尔坐标?
仅供参考:这一切都与我之前的问题Python: Graphic input in 3D相关,该问题被认为过于宽泛和通用。
解决方案
ax.format_coord
按下的鼠标按钮是返回 3D 图上的角坐标而不是笛卡尔坐标的触发器。因此,一个选项是让ax.format_coord
认为没有按下任何按钮,在这种情况下,它将根据需要返回通常的笛卡尔 x,y,z 坐标。
即使您单击了鼠标按钮,实现这一点的一种 hacky 方法也是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
推荐阅读
- r - 用寓言预测,如何使用特价?
- python - 似乎无法在 python 中找到带有硒的元素
- sockets - 让币安期货 wss 保持活力
- arrays - 比较对象返回无效结果
- java - @InjectMock 中的 NullPointerException
- javascript - “无法将 axe-core 注入其中一个 iframe!” AX 可访问性错误
- docusignapi - docusign RecipientSignatureInformation 允许使用哪些字体样式
- php - php/mysql : 成员得分最高的前 10 名
- progressive-web-apps - 什么是无头 PWA。PWA 和无头 PWA 有什么区别?
- python - 从 urllib 中提取特定数据