plot_surface : 获取写在右下角的 x,y,z 值

发布于 2024-11-25 04:52:36 字数 229 浏览 5 评论 0原文

我是 matplotlib 的新用户。使用 matplotlib 和plot_surface 绘制曲面 (3D) 时,会出现一个带有图形的窗口。在该窗口中,右下角有鼠标实际位置的坐标。是否可以访问这些值?

我在互联网上搜索过,但提出的解决方案很少。对于 2D 绘图,可以使用 tkinter.canvas 访问这些值,但在 3D 中,当使用相同的技术时,event.xdata 和 event.ydata 不会返回鼠标的正确位置。

I am a quite new user of matplotlib. When plotting a surface (3D) with matplotlib and plot_surface, a window appears with the graph. In that windows, on the bottom right corner there is the coordinates of the actual position of the mouse. Is is possible to gain access to these values?

I have search on internet but very few solutions are proposed. For a 2D plot, these values are accessible using a tkinter.canvas but in 3D, when using the same technique the event.xdata and event.ydata did not return the good position for the mouse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-12-02 04:52:36

使用ax.format_coord(mouseevent.xdata,mouseevent.ydata),您可以获取字符串中的x、y、z值('x=0.222, y=0.452, z=0.826') 您可以从中提取值。

例如对于 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)

With ax.format_coord(mouseevent.xdata,mouseevent.ydata) you get the x, y, z values in a string ('x=0.222, y=0.452, z=0.826') from which you can extract the values.

For example for y-coordinate:

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)
表情可笑 2024-12-02 04:52:36

我尝试了@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 这样的东西存在。

I tried @Thilo's answer and received only a single float. Using Python 3.8 and Matplotlib version 3.2.1.

Modified his function to this

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

Many thanks, because without that answer I would never knew something like format_coord exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文