chaco - 使多个容器显示单独的图

chaco - making several Containers show separate plots

我写了一个 chaco plotting class 来绘制一些数据并允许用户与之交互。然后我想制作一个 TraitsUI GUI,其中包含此 chaco 图的多个不同实例,以便用户可以拥有多个图并独立地与它们交互。

但是,当我尝试实现它时,我似乎发现我的查科图的每个单独实例都显示了所有图中的所有数据。我在下面制作了一个非常简单的 GUI 来重现该问题。

在下面的示例中,我希望每个选项卡都显示一个带有单线图的容器。但是,每个容器似乎都绘制了在任何容器中绘制的所有图。从此处的文档 chaco container docs,我认为我所做的应该有效。

我也试过使用 ListEditor 视图,但这有同样的问题。

我是不是对 chaco Containers 有什么误解?如何让每个容器实例独立运行?任何帮助将不胜感激。

谢谢!

import enthought.chaco.api as chaco
import enthought.traits.api as traits
import enthought.traits.ui.api as traitsui
from enthought.enable.api import  ComponentEditor
import scipy


class BasicPlot(traits.HasTraits):

    container = chaco.Plot(padding=(120,20,20,40), bgcolor="white",
                                     use_backbuffer = True,
                                     border_visible = True,
                                     fill_padding = True)



    traits_view = traitsui.View(traitsui.Item('container', editor = ComponentEditor(), show_label = False),
                       width = 500, height = 500,
                       resizable = True, title = "My line plot")

    def __init__(self, n, *args, **kw):
        super(BasicPlot, self).__init__(*args, **kw)
        xs = scipy.linspace(0, 6.3, 1000)
        ys = scipy.sin(n*xs)
        plot = chaco.create_line_plot([xs,ys])
        self.container.add(plot)
        chaco.add_default_grids(plot)
        chaco.add_default_axes(plot)

class tabbedPlots(traits.HasTraits):

    bp1 = BasicPlot(1)
    bp2 = BasicPlot(2)

    bpGroup = traitsui.Group(traitsui.Item("bp1", editor = traitsui.InstanceEditor(), style="custom", show_label=False),
                              traitsui.Item("bp2", editor = traitsui.InstanceEditor(), style="custom", show_label=False), layout="tabbed")

    traits_view = traitsui.View(bpGroup,title = "Log File Plots")    


class tabbedPlotsList(traits.HasTraits):

    bps = traits.List(BasicPlot)


    bpGroup = traitsui.Group(
                        traitsui.Item('bps',style="custom",
                                      editor=traitsui.ListEditor(use_notebook=True, deletable=True,export = 'DockWindowShell', page_name=".name")
                                      ,label="logFilePlots", show_label=False)
                                      )

    traits_view = traitsui.View(bpGroup,title = "Log File Plots")

    def __init__(self, **traitsDict):
       super(tabbedPlotsList, **traitsDict)
       self.bps = [BasicPlot(n) for n in range(0,8)]

if __name__=="__main__":

    gui = tabbedPlots()
    gui.configure_traits()
    gui2 = tabbedPlotsList()
    gui2.configure_traits()

我找到了解决方法。

def __init__(self, n, *args, **kw):
    super(BasicPlot, self).__init__(*args, **kw)
    self.container = chaco.Plot(padding=(120,20,20,40), bgcolor="white",
                                     use_backbuffer = True,
                                     border_visible = True,
                                     fill_padding = True)
    xs = scipy.linspace(0, 6.3, 1000)
    ys = scipy.sin(n*xs)
    plot = chaco.create_line_plot([xs,ys])
    self.container.add(plot)
    chaco.add_default_grids(plot)
    chaco.add_default_axes(plot)

要使其按预期工作,容器不能是 class 属性。相反,它必须在 init 中定义为 self.container(...)。 (这是有道理的)

如果进行此更改,您将获得所需的功能。