【问题标题】:Matplotlib - plot_surface : get the x,y,z values written in the bottom right cornerMatplotlib - plot_surface : 获取写在右下角的 x,y,z 值
【发布时间】:2011-10-08 13:22:26
【问题描述】:
我是 matplotlib 的新用户。使用 matplotlib 和 plot_surface 绘制曲面 (3D) 时,会出现一个带有图形的窗口。在那个窗口中,右下角有鼠标实际位置的坐标。是否可以访问这些值?
我在互联网上进行了搜索,但提出的解决方案很少。对于 2D 绘图,可以使用 tkinter.canvas 访问这些值,但在 3D 中,当使用相同的技术时,event.xdata 和 event.ydata 不会为鼠标返回合适的位置。
提前感谢您的宝贵帮助,
安东尼
【问题讨论】:
标签:
python
canvas
matplotlib
tkinter
mouseevent
【解决方案1】:
使用ax.format_coord(mouseevent.xdata,mouseevent.ydata),您可以在字符串('x=0.222, y=0.452, z=0.826') 中获取x、y、z 值,您可以从中提取值。
例如对于 y 坐标:
def gety(x,y):
s = ax.format_coord(x,y)
out = ""
for i in range(s.find('y')+2,s.find('z')-2):
out = out+s[i]
return float(out)
【解决方案2】:
我尝试了@Thilo 的回答,但只收到了一个浮点数。使用 Python 3.8 和 Matplotlib 3.2.1 版。
把他的功能改成这个
def getxyz(event):
s = ax.format_coord(event.xdata, event.ydata)
out = [float(x.split('=')[1].strip()) for x in s.split(',')]
print(out)
return out
非常感谢,因为如果没有这个答案,我永远不会知道像 format_coord 这样的东西存在。