Skip to content

Instantly share code, notes, and snippets.

@scott-maddox
Last active June 17, 2019 14:54
Show Gist options
  • Save scott-maddox/a306e0b0c0cdb978e933f88ce707af93 to your computer and use it in GitHub Desktop.
Save scott-maddox/a306e0b0c0cdb978e933f88ce707af93 to your computer and use it in GitHub Desktop.
from chaco.api import ArrayPlotData, GridContainer, Plot
from chaco.example_support import COLOR_PALETTE
from chaco.tools.api import PanTool, ZoomTool
from enable.api import ComponentEditor
from numpy import linspace
from scipy.special import jn
from traits.api import HasTraits, Int, Property
from traitsui.api import Group, Item, View
def _create_plot_component(count, grid_shape):
container = GridContainer(padding=40,
fill_padding=True,
bgcolor="lightgray",
use_backbuffer=True,
shape=grid_shape,
spacing=(20, 20),
width=3 * 400,
resizable="v")
# Create the initial series of data
x = linspace(-5, 15.0, 100)
pd = ArrayPlotData(index=x)
# Plot some bessel functions and add the plots to our container
for i in range(count):
pd.set_data("y" + str(i), jn(i, x))
plot = Plot(pd)
plot.plot(("index", "y" + str(i)),
color=tuple(COLOR_PALETTE[i % 10]),
line_width=2.0,
bgcolor="white",
border_visible=True)
# Tweak some of the plot properties
plot.border_width = 1
plot.padding = 0
plot.padding_top = 30
plot.bounds = [400, 400]
# Attach some tools to the plot
plot.tools.append(PanTool(plot))
zoom = ZoomTool(plot, tool_mode="box", always_on=False)
plot.overlays.append(zoom)
# Add to the grid container
container.add(plot)
return container
class Demo(HasTraits):
count = Int
grid_shape = Property(depends_on='count')
plot = Property(depends_on='count')
def default_traits_view(self):
return View(
Group(
Item(
'plot',
editor=ComponentEditor(),
show_label=False,
width=self.grid_shape[1]*400,
height=self.grid_shape[0]*400,
),
),
width=self.grid_shape[1]*400 + 50,
height=600,
scrollable=True,
resizable=True,
title="Resizable Grid Container"
)
def _get_grid_shape(self):
# 3 columns, and as many rows as needed
return ((self.count + 2) // 3, 3)
def _get_plot(self):
return _create_plot_component(self.count, self.grid_shape)
if __name__ == "__main__":
Demo(count=13).configure_traits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment