2015-03-13 32 views
2

假設我有一個像這樣的4d numpy數組:my_array[x,y,z,t]Mayavi的時間相關數據

有沒有簡單的方法將整個數組加載到Mayavi中,並簡單地選擇我想調查的t

我知道可以對數據進行動畫製作,但我想旋轉我的數字「在旅途中」。

回答

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如果你喜歡這一點。

+0

感謝您的回覆。你能詳細說明你的代碼嗎?據我所見,'t = Int(0)'返回一個traits對象,這使得它不可能用來切割稱爲'a'的numpy數組。 另外:'self.a'似乎不是一個可能的調用,而是'self.plot = something(self.a [t])'中的'a''。用something()函數的例子也會很好。 – 2015-05-19 12:03:19

+0

我改了一點代碼。東西函數可以是任何你喜歡的繪圖模塊,例如surf()或mesh()。我提供的示例代碼通常不能正常工作,您必須根據需要進行調整,它應該讓您瞭解如何解決問題,因爲您的問題相當普遍。 – Singularity 2015-05-19 12:43:56

+0

對代碼的更改有助於:)。但是,通過添加來自mayavi.mlab導入網格的'from'和將函數從'something'更改爲'mesh'將首先產生一個空圖,接着是TypeError:grid_source()需要3個參數(給出1) '在開始和'AttributeError:'Data_plot'對象沒有屬性'plot'' t改變。我同意你的看法,答案應該是相當一般的,但是有一個可以很容易被改變的例子(很容易被改變)會很好:)。 – 2015-05-19 13:17:49