Skip to content

Instantly share code, notes, and snippets.

@rkern
Forked from gazzar/arrayplotter.py
Created October 17, 2012 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkern/3904795 to your computer and use it in GitHub Desktop.
Save rkern/3904795 to your computer and use it in GitHub Desktop.
Chaco FunctionImageData test
#!/usr/bin/env python
"""
Demonstrates use of the FunctionImageData that depends on an external range
and returns different data depending on that range.
"""
# Major library imports
from numpy import pi, linspace, meshgrid, sin
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance, Int, Bool, on_trait_change
from traitsui.api import Item, Group, HGroup, View
# Chaco imports
from chaco.api import (ArrayPlotData, DataView, GridDataSource, LinePlot, Plot,
ScatterPlot, jet)
from chaco.tools.api import PanTool, ZoomTool
from chaco.function_image_data import FunctionImageData
class PlotExample(HasTraits):
plot = Instance(Component)
use_tools = Bool(True)
sidelength = Int(300)
img_plot = Instance(Component)
traits_view = View(
Group(
Item('plot', editor=ComponentEditor(size=(50,50)), show_label=False),
orientation = "vertical"),
resizable=True, title="Explore Plot",
width=600, height=600
)
def xyfunc(self, xlow, xhigh, ylow, yhigh):
print xlow, xhigh, ylow, yhigh
xs = linspace(xlow, xhigh, self.sidelength)
ys = linspace(ylow, yhigh, self.sidelength)
x, y = meshgrid(xs,ys)
return sin(1/x)*sin(1/y)
def _plot_default(self):
pd = ArrayPlotData()
plot = Plot(pd)
fid = FunctionImageData(func=self.xyfunc, data_range=plot.range2d)
pd.set_data("imagedata", fid)
self.img_plot = plot.img_plot("imagedata", colormap=jet,
interpolation='nearest',
xbounds=(-1.0, 1.0),
ybounds=(-1.0, 1.0),
)[0]
plot.tools.append(PanTool(plot))
plot.tools.append(ZoomTool(plot))
return plot
@on_trait_change('plot.range2d.updated')
def _update_ranges(self):
if self.img_plot is not None:
low_xy = self.plot.range2d.low
high_xy = self.plot.range2d.high
self.img_plot.index.set_data(
linspace(low_xy[0], high_xy[0], self.sidelength),
linspace(low_xy[1], high_xy[1], self.sidelength),
('ascending', 'ascending'),
)
demo = PlotExample()
if __name__ == "__main__":
demo.configure_traits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment