我希望通过鼠标事件(例如点击)获取3D绘图中的坐标(x,y,z)。 MATLAB具有此功能datacursormode
。一个好的形象在以下链接。
http://www.mathworks.com/help/matlab/ref/datacursormode.html
mpldatacursor
(https://github.com/joferkington/mpldatacursor)是matplotlib的类似函数,但是,这似乎不适合3D图。 x和y值不合适,即使它们可以得到。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor
x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)+ np.cos(Y)
fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1)
datacursor(surf)
plt.show()
如果有可能,我也想得到z值。 有什么好办法吗?
答案 0 :(得分:-1)
mpldatacursor对于我想要的东西来说太复杂了,它想要以某种方式在回调函数中接收x,y,z坐标。我从mpldatacursor pick_info.py中提取了一个辅助函数(get_xyz_mouse_click),该函数执行获取坐标所需的最低要求(即,没有悬停窗口,没有复杂的事件处理)。这是辅助函数:
import numpy as np
import matplotlib.transforms as mtransforms
from mpl_toolkits import mplot3d
def get_xyz_mouse_click(event, ax):
"""
Get coordinates clicked by user
"""
if ax.M is None:
return {}
xd, yd = event.xdata, event.ydata
p = (xd, yd)
edges = ax.tunit_edges()
ldists = [(mplot3d.proj3d.line2d_seg_dist(p0, p1, p), i) for \
i, (p0, p1) in enumerate(edges)]
ldists.sort()
# nearest edge
edgei = ldists[0][1]
p0, p1 = edges[edgei]
# scale the z value to match
x0, y0, z0 = p0
x1, y1, z1 = p1
d0 = np.hypot(x0-xd, y0-yd)
d1 = np.hypot(x1-xd, y1-yd)
dt = d0+d1
z = d1/dt * z0 + d0/dt * z1
x, y, z = mplot3d.proj3d.inv_transform(xd, yd, z, ax.M)
return x, y, z
下面是在3D散点图中使用它的示例:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import simple_pick_info.pick_info
# Fixing random state for reproducibility
np.random.seed(19680801)
def randrange(n, vmin, vmax):
'''
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
'''
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
def on_press(event):
x,y,z = simple_pick_info.pick_info.get_xyz_mouse_click(event, ax)
print(f'Clicked at: x={x}, y={y}, z={z}')
cid = fig.canvas.mpl_connect('button_press_event', on_press)
您应该编辑on_press()
以对x,y,z进行操作。仍然存在这样的问题,即使用轴网格引用的其他答案产生一个点(即,它不会在原始数据中搜索最近的邻居)。我建议您对原始数据模型(点,线等)进行距离转换,因为这将非常困难,因为它很难在曲面中搜索补丁。
我真的希望它能以Matlab的datacursormode的工作方式内置到matplotlib中!