【问题标题】:Time dependent data in MayaviMayavi 中的时间相关数据
【发布时间】:2015-03-13 13:18:02
【问题描述】:

假设我有一个像这样的 4d numpy 数组:my_array[x,y,z,t]

有没有一种简单的方法可以将整个数组加载到 Mayavi 中,并且只需选择我想要调查的 t

我知道可以为数据制作动画,但我想“随时随地”旋转我的人物。

【问题讨论】:

    标签: python numpy multidimensional-array mayavi


    【解决方案1】:

    可以通过输入框设置对话,在其中可以设置t。 你必须使用 traits.api,它可能看起来像这样:

    from traits.api import HasTraits, Int, Instance, on_trait_change
    from traitsui.api import View, Item, Group
    from mayavi.core.ui.api import SceneEditor, MlabSceneModel, MayaviScene
    
    class Data_plot(HasTraits):
      a = my_array
      t = Int(0)
      scene = Instance(MlabSceneModel, ())
      plot = Instance(PipelineBase)
    
      @on_trait_change('scene.activated')
      def show_plot(self):
        self.plot = something(self.a[self.t]) #something can be surf or mesh or other
    
      @on_trait_change('t')
      def update_plot(self):
        self.plot.parent.parent.scalar_data = self.a[self.t] #Change the data
    
      view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                    show_label=False),
                    Group('_', 't'),
                    resizable=True,
                    )
    
    my_plot = Data_plot(a=my_array)
    my_plot.configure_traits()
    

    如果您愿意,也可以使用命令Range 而不是Int 来设置滑块。

    【讨论】:

    • 感谢您的回复。你能详细说明你的代码吗?据我所知,t = Int(0) 返回一个特征对象,这使得它无法用于对名为 a 的 numpy 数组进行切片。另外:self.a 似乎不是一个可能的电话,而是self.plot = something(self.a[t]) 中的a。如果有一个 something() 函数的例子也很好。
    • 我稍微修改了代码。 something 函数可以是您喜欢的任何绘图模块,例如 surf() 或 mesh()。我提供的示例代码通常无法正常工作,您必须根据需要对其进行调整,它应该只是让您了解如何解决问题,因为您的问题很笼统。
    • 对代码的更改有帮助:)。但是,通过添加from mayavi.mlab import mesh 并将函数从something 更改为mesh 将首先导致一个空图,然后在开始时是TypeError: grid_source() takes exactly 3 arguments (1 given) ,在更改t 时是AttributeError: 'Data_plot' object has no attribute 'plot'。我同意你的观点,答案应该很笼统,但是有一个开箱即用的例子(很容易改变)会很好:)。
    • 我使用了类似的代码来绘制图像平面小部件并对其进行更新,对于 mesh(),数据更新应该与 .set 或 .reset 命令一起使用。
    • 对于网格,您需要 x、y、z 坐标作为 2d 数组,因此您需要选择适合的数组部分。在这里查找:link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2013-06-08
    • 2014-01-30
    • 2017-09-30
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多