Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get properties of picked object in mplot3d (matplotlib + python)?

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

like image 639
user1371437 Avatar asked May 03 '12 02:05

user1371437


People also ask

What is the use of Matplotlib *?

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.

How do you plot 3d coordinates in Python?

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 ...

What is axes3d?

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.


1 Answers

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]
like image 199
HYRY Avatar answered Nov 15 '22 23:11

HYRY