Annotating many points of text in Mayavi using mlab

I am trying to annotate points constructed using points3d () function using mayavi.mlab. Each point is associated with a label that I would like to build next to the points using the text3d () function. Building points is fast, but the mlab.text3d () function does not seem to accept coordinate arrays, so I have to iterate over the points and print the text individually, which is very slow:

for i in xrange(0, self.n_labels): self.mlab_data.append( mlab.points3d( pX[self.labels == self.u_labels[i], 0], pX[self.labels == self.u_labels[i], 1], pX[self.labels == self.u_labels[i], 2], color=self.colours[i], opacity=1, scale_mode="none", scale_factor=sf ) ) idcs, = np.where(self.labels == self.u_labels[i]) for n in idcs.flatten(): mlab.text3d( pX[n, 0], pX[n, 1], pX[n, 2], "%d" % self.u_labels[i], color=self.colours[i], opacity=1, scale=sf ) 

Any ideas how I could speed this up? Also, is it possible to add a legend (for example, in matplotlib), I could not find anything in the documents.

Thanks,

Patrick

+6
source share
1 answer

The way you do it above will render the scene every time you draw a point or text. It is slow. You can turn off scene rendering, print, and then render the scene using figure.scene.disable_render = True / False :

  import scipy from mayavi import mlab X = 100 * scipy.rand(100, 3) figure = mlab.figure('myfig') figure.scene.disable_render = True # Super duper trick mlab.points3d(X[:,0], X[:,1], X[:,2], scale_factor=0.4) for i, x in enumerate(X): mlab.text3d(x[0], x[1], x[2], str(i), scale=(2, 2, 2)) figure.scene.disable_render = False # Super duper trick 

I use this trick and others in the Figure class in a morphic viewer https://github.com/duanemalcolm/morphic/blob/master/morphic/viewer.py

Another good trick in the code is to reuse existing objects, i.e. if you have already drawn text, do not rearrange them, just update their positions and text attributes. This means saving the mlab object. You can see how I do it in morphic.Viewer.

+5
source

Source: https://habr.com/ru/post/927935/


All Articles