Link - UnLink axis on different plots preserving actual curves positions

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Link - UnLink axis on different plots preserving actual curves positions

Eraldo Pomponi
Hi All, 

I'm trying to produce two Chaco plots that should have their index_range linked or unlinked 
alternatively on successive button_fired events.

What I think I should do, when axis are linked together, is to increase/decrease
index_mapper.high_pos  and index_mapper.low_pos of the same quantity 
in both plots when they change but I haven't figured it out how to do that. 
Maybe I'm missing the point and the way to achieve this behavior is another. 
An example of what I'm trying to do follows (inspired by tabbed_plot.py example): 

#######################################################################

import numpy as np

from traits.api import HasTraits, Button, Instance, Bool, Any
from traitsui.api import View, UItem
from chaco.api import Plot, ArrayPlotData
from chaco.tools.api import PanTool, ZoomTool
from enable.api import ComponentEditor


class LinkedAxisPlot(HasTraits):

    link_axis = Button('Link axis')
    _linked = Bool(False)
    _index_range = Any

    # ArrayPlotData for plot
    plot_data = Instance(ArrayPlotData, ())

    plot1 = Instance(Plot)
    plot2 = Instance(Plot)

    view = View(UItem('plot1',
                     editor=ComponentEditor()
                     ),
                UItem('link_axis'),
                UItem('plot2',
                     editor=ComponentEditor()
                     ),
                height=0.50,
                width=0.30,
                resizable=True
                )

    def _link_axis_fired(self):
        if self._linked:
            self.plot1.index_range = self._index_range
            self._linked = False
        else:
            self._index_range = self.plot1.index_range
            self.plot1.index_range = self.plot2.index_range
            self._linked = True

    def _plot_data_default(self):

        plot_data = ArrayPlotData()

        x = np.linspace(-10 * np.pi, 10 * np.pi, 4096)
        plot_data.set_data('index', x)
        plot_data.set_data('y1', np.sin(x))
        plot_data.set_data('y2', np.cos(x + np.pi))

        return plot_data

    def _plot1_default(self):

        plot = Plot(self.plot_data)

        plot.plot(('index', 'y1'), 'line', 'Sin')
        plot.tools.append(PanTool(plot))
        plot.overlays.append(ZoomTool(plot))

        return plot

    def _plot2_default(self):

        plot = Plot(self.plot_data)

        plot.plot(('index', 'y2'), 'line', 'Cos', color='red')
        plot.tools.append(PanTool(plot))
        plot.overlays.append(ZoomTool(plot))

        return plot

if __name__ == "__main__":

    tv = LinkedAxisPlot()
    tv.configure_traits()

###############################################################

Any advice?  Thanks a lot

Cheers, 
Eraldo 


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev