Skip to content

Instantly share code, notes, and snippets.

@sergey-miryanov
Created January 13, 2011 09:26
Show Gist options
  • Save sergey-miryanov/777627 to your computer and use it in GitHub Desktop.
Save sergey-miryanov/777627 to your computer and use it in GitHub Desktop.
Template, Plot and Line
def gridShape (count) :
columns = int (ceil (sqrt (count)))
columns = columns if columns > 0 else 1
rows = int (count) / columns
if count % columns > 0 :
rows += 1
return (rows, columns)
class GridContainer (object) :
def __init__ (self, count) :
self._grid = ChacoGridContainer (padding = 15, fill_padding = True, use_backbuffer = True,
shape = gridShape (count), spacing = (0, 0))
def add (self, plot) :
self._grid.add (plot)
def plot (self) :
return self._grid
class PlotContainer (object) :
def __init__ (self) :
self._container = OverlayPlotContainer (use_backbuffer = True)
self._plots = {}
def add (self, name, plot) :
self._plots[name] = plot
def plot (self, name, xaxis) :
plot = None
for plot in self._plots.values () :
self._container.add (plot)
if plot :
if xaxis == "years" :
xAxis = ScalesPlotAxis (plot, orientation = "bottom",
tick_label_font = "Sans 8",
tick_generator = YearsTickGenerator (self._container))
elif xaxis == "date-time" :
xAxis = ScalesPlotAxis (plot, orientation = "bottom",
tick_label_font = "Sans 8",
tick_generator = DatetimeTickGenerator (self._container))
else :
xAxis = PlotAxis (plot, orientation = "bottom",
tick_label_font = "Sans 8")
yAxis = PlotAxis (plot, orientation = "left",
tick_label_font = "Sans 8")
title = PlotLabel (name, component = plot, font = "Sans 10")
verticalGrid = PlotGrid (component = plot,
mapper = plot.index_mapper,
orientation = "vertical",
line_color = "gray",
line_style = "dot",
use_draw_order = True)
horizontalGrid = PlotGrid (component = plot,
mapper = plot.value_mapper,
orientation = "horizontal",
line_color = "gray",
line_style = "dot",
use_draw_order = True)
inspectorIndex = LineInspector (component = plot,
line_color = "gray",
line_style = 'dot',
axis = "index")
inspectorValue = LineInspector (component = plot,
line_color = "gray",
line_style = 'dot',
axis = "value")
legend = Legend (component = plot,
align = "ur",
padding = 30,
plots = self._plots)
legend.tools.append (LegendTool (legend, drag_button = "right"))
plot.overlays.append (inspectorIndex)
plot.overlays.append (inspectorValue)
plot.underlays.append (verticalGrid)
plot.underlays.append (horizontalGrid)
plot.overlays.append (xAxis)
plot.overlays.append (yAxis)
plot.overlays.append (title)
plot.overlays.append (RangeZoomTool (component = plot,
tool_mode = "range",
axis = "index",
always_on = True,
enable_wheel = False))
plot.overlays.append (legend)
return self._container
class Template (object) :
def __init__ (self, name, xaxis, grid) :
self._name = name
self._xaxis = xaxis
self._grid = grid
self._epoch = date (1970, 1, 1)
def dataIndex (self, data) :
if self._xaxis == "years" :
def filter_ (d) :
d = self._epoch + timedelta (d)
return d.year
index = data.index (filter_)
return (index, unique (index))
return (data.index (), data.index ())
def view (self, data) :
index, filteredIndex = self.dataIndex (data)
indexData = ArrayDataSource (filteredIndex, sort_order = "ascending")
grid = GridContainer (len (self._grid))
for plot in self._grid :
container = PlotContainer ()
indexMapper = LinearMapper (range = DataRange1D (indexData))
for i, line in enumerate (plot.lines ()) :
value = ArrayDataSource (line.data (data, index))
p = line.plot ((indexData, indexMapper), (value, LinearMapper (range = DataRange1D (value))))
container.add (line.name (), p)
grid.add (container.plot ("%s: %s" % (self._name, plot.name ()), self._xaxis))
return grid.plot ()
def name (self) :
return self._name
def grid (self) :
return self._grid
def remove (self, what) :
self._grid.remove (what)
class Plot (object) :
def __init__ (self, name, lines) :
self._name = name
self._lines = lines
def name (self) :
return self._name
def lines (self) :
return self._lines
def append (self, line) :
self._lines.append (line)
def remove (self, line) :
self._lines.remove (line)
class Line (object) :
def __init__ (self, line, color, data) :
self._line = line
self._color = color
if type (data[-1]) is list :
self._data = data[:-1]
self._alt = [self._data[:-2] + [d] + [self._data[-1]] for d in data[-1]]
else :
self._data = data
self._alt = []
def name (self) :
return "/".join (self._data[1:-1])
def data (self, data, index) :
path = self._data[:-1]
alt = [a[:-1] for a in self._alt]
return array (groupby (index, data.data (path, alt), add))
def plot (self, index, value) :
index, indexMapper = index
value, valueMapper = value
if self._line == "line" :
return self._linePlot (index, indexMapper, value, valueMapper)
elif self._line == "scatter-line" :
return self._scatterLinePlot (index, indexMapper, value, valueMapper)
elif self._line == "scatter" :
return self._scatterPlot (index, indexMapper, value, valueMapper)
raise RuntimeError (tr ("Unknown plot type: %s") % self._type)
def _scatterPlot (self, index, indexMapper, value, valueMapper) :
return ScatterPlot (index = index,
value = value,
index_mapper = indexMapper,
value_mapper = valueMapper,
line_width = 1,
padding = 30,
padding_bottom = 30,
border_visible = True,
color = self._color,
marker = "square",
marker_size = 2)
def _linePlot (self, index, indexMapper, value, valueMapper) :
return LinePlot (index = index,
value = value,
index_mapper = indexMapper,
value_mapper = valueMapper,
line_width = 2.0,
padding = 30,
padding_bottom = 30,
border_visible = True,
color = self._color)
def _scatterLinePlot (self, index, indexMapper, value, valueMapper) :
return ScatterLinePlot (index = index,
value = value,
index_mapper = indexMapper,
value_mapper = valueMapper,
line_width = 1,
padding = 30,
padding_bottom = 30,
border_visible = True,
color = self._color,
marker = "square",
marker_size = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment