Search code examples
traitstraitsuichaco

chaco - making several Containers show separate plots


I have written a chaco plotting class that plots some data and allows the user to interact with it. I then wanted to make a TraitsUI GUI that has several different instances of this chaco plot so that the user can have several of the plots and interact with them independently.

However, when I try and implement this I seem to get that each of the separate instances of my chaco plot are displaying all the data from all the plots. I have made a very simple GUI below that reproduces the problem.

In the example below I would like each tab to show a container with a single line plot. However, each container seems to plot all the plots that have been plotted in any of the containers. From the documentation here chaco container docs, I think what I have done should work.

I have also tried using the ListEditor view, but this has the same problem.

Am I misunderstanding something about chaco Containers? How can I get each container instance to act independently? Any help would be appreciated.

Thanks!

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()

Solution

  • I found the fix to this.

    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)
    

    To make it work as desired the container cannot be a class attribute. Instead it must be defined inside the init as self.container(...). (This makes sense)

    If this change is made you get the desired functionality.