I plot a series of points using mplot3d
:
import pylab as p
import mpl_toolkits.mplot3d.axes3d as p3
fig = p.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter([1], [0], [0], c='r', marker='^', picker=5)
ax.scatter([0], [1], [0], c='g', marker='^', picker=5)
ax.scatter([0], [0], [1], c='b', marker='^', picker=5)
and then I add a picker function:
def onpick(event):
ind = event.ind
print ind
fig.canvas.mpl_connect('pick_event', onpick)
and finally plot it:
p.show()
Is there a way of getting the 3D coordinates from the marker I am clicking?
So far I can get the index of the point in the list I used at ax.scatter()
, but that wont cut it as I use ax.scatter()
many times and this has to be this way (I use different colors for example).
Regards
Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.
ax = plt.axes(projection='3d') # Data for a three-dimensional line zline = np.linspace(0, 15, 1000) xline = np.sin(zline) yline = np.cos(zline) ax.plot3D(xline, yline, zline, 'gray') # Data for three-dimensional scattered points zdata = 15 * np.random.random(100) xdata = np.sin(zdata) + 0.1 * np.random.randn(100) ydata ...
The axes3d is used since it takes a different kind of axis in order to actually graph something in three dimensions. Next: fig = plt. figure() ax1 = fig. add_subplot(111, projection='3d') Here, we define the figure as usual, and then we define the ax1 as a typical subplot, just with a 3d projection this time.
You can use _offsets3d attribute of event.artist to get the coordinate data, and then use ind to get the picked point:
def onpick(event):
ind = event.ind[0]
x, y, z = event.artist._offsets3d
print x[ind], y[ind], z[ind]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With