使用mlab在mayavi中用文本注释很多点 [英] annotating many points with text in mayavi using mlab

查看:226
本文介绍了使用mlab在mayavi中用文本注释很多点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mayavi.mlab注释使用points3d()函数绘制的点。
每个点都与一个标签关联,我想使用text3d()函数在这些点旁边绘制标签。绘制点很快,但是mlab.text3d()函数似乎不接受坐标数组,因此我必须遍历这些点并分别绘制文本,这很慢:

I'm trying to annotate points plotted with the points3d() function using mayavi.mlab. Each point is associated with a label which I would like to plot next to the points using the text3d() function. Plotting the points is fast, however the mlab.text3d() function does not seem to accept arrays of coordinates, so I have to loop over the points and plot 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 )

有什么想法可以加快速度吗?另外,是否可以添加图例(例如在matplotlib中),我在文档中找不到任何内容。

Any ideas how I could speed this up? Also, is it possible to add a legend (as for instance in matplotlib), I couldn't find anything in the docs.

谢谢,

Patrick

推荐答案

上面的操作方式每次绘制时都会渲染场景点或文字。太慢了您可以通过 figure.scene.disable_render = True / False 禁用场景渲染,绘制然后渲染场景:

The way you are doing it above will render the scene every time you plot a point or text. This is slow. You can disable the scene rendering, do the plotting and then render the scene by 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

morphic Viewer中的Figure类中的其他类 https://github.com/duanemalcolm/ morphic / blob / master / morphic / viewer.py

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

代码中的另一个好窍门是重用现有对象,即,如果您绘制了已输入文字,请勿重新绘制下摆,只需更新其位置和文字属性即可。这意味着保存mlab对象。您可以在morphic.Viewer中看到我的操作方式。

Another good trick in the code is to reuse existing objects, i.e., if you've plotted the text already, don't replot them, just update their position and text attributes. This means saving the mlab object. You can see how I do this in morphic.Viewer.

这篇关于使用mlab在mayavi中用文本注释很多点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆