/usr/share/pyshared/replot.py is in reinteract 0.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | # Copyright 2007, 2008 Owen Taylor
# Copyright 2008 Kai Willadsen
#
# This file is part of Reinteract and distributed under the terms
# of the BSD license. See the file COPYING in the Reinteract
# distribution for full details.
#
########################################################################
import cairo
import gtk
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_cairo import RendererCairo, FigureCanvasCairo
import numpy
from reinteract.recorded_object import RecordedObject, default_filter
import reinteract.custom_result as custom_result
class _PlotResultCanvas(FigureCanvasCairo):
def draw_event(*args):
# Since we never change anything about the figure, the only time we
# need to redraw is in response to an expose event, which we handle
# ourselves
pass
class PlotWidget(gtk.DrawingArea):
__gsignals__ = {
'button-press-event': 'override',
'button-release-event': 'override',
'expose-event': 'override',
'size-allocate': 'override',
'unrealize': 'override'
}
def __init__(self, result):
gtk.DrawingArea.__init__(self)
self.figure = Figure(facecolor='white', figsize=(6,4.5))
self.canvas = _PlotResultCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE)
self.cached_contents = None
def do_expose_event(self, event):
cr = self.window.cairo_create()
if not self.cached_contents:
self.cached_contents = cr.get_target().create_similar(cairo.CONTENT_COLOR,
self.allocation.width, self.allocation.height)
renderer = RendererCairo(self.figure.dpi)
renderer.set_width_height(self.allocation.width, self.allocation.height)
renderer.set_ctx_from_surface(self.cached_contents)
self.figure.draw(renderer)
# event.region is not bound: http://bugzilla.gnome.org/show_bug.cgi?id=487158
# gdk_context = gtk.gdk.CairoContext(renderer.ctx)
# gdk_context.region(event.region)
# gdk_context.clip()
cr.set_source_surface(self.cached_contents, 0, 0)
cr.paint()
def do_size_allocate(self, allocation):
if allocation.width != self.allocation.width or allocation.height != self.allocation.height:
self.cached_contents = None
gtk.DrawingArea.do_size_allocate(self, allocation)
def do_unrealize(self):
gtk.DrawingArea.do_unrealize(self)
self.cached_contents = None
def do_button_press_event(self, event):
if event.button == 3:
custom_result.show_menu(self, event, save_callback=self.__save)
return True
else:
return True
def do_button_release_event(self, event):
return True
def do_realize(self):
gtk.DrawingArea.do_realize(self)
cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
self.window.set_cursor(cursor)
def do_size_request(self, requisition):
try:
# matplotlib < 0.98
requisition.width = self.figure.bbox.width()
requisition.height = self.figure.bbox.height()
except TypeError:
# matplotlib >= 0.98
requisition.width = self.figure.bbox.width
requisition.height = self.figure.bbox.height
def __save(self, filename):
# The save/restore here was added to matplotlib's after 0.90. We duplicate
# it for compatibility with older versions. (The code would need modification
# for 0.98 and newer, which is the reason for the particular version in the
# check)
version = [int(x) for x in matplotlib.__version__.split('.')]
need_save = version[:2] < [0, 98]
if need_save:
orig_dpi = self.figure.dpi.get()
orig_facecolor = self.figure.get_facecolor()
orig_edgecolor = self.figure.get_edgecolor()
try:
self.canvas.print_figure(filename)
finally:
if need_save:
self.figure.dpi.set(orig_dpi)
self.figure.set_facecolor(orig_facecolor)
self.figure.set_edgecolor(orig_edgecolor)
self.figure.set_canvas(self.canvas)
# def do_size_allocate(self, allocation):
# gtk.DrawingArea.do_size_allocate(self, allocation)
#
# dpi = self.figure.dpi.get()
# self.figure.set_size_inches (allocation.width / dpi, allocation.height / dpi)
def _validate_args(args):
#
# The matplotlib argument parsing is a little wonky
#
# plot(x, y, 'fmt', y2)
# plot(x1, y2, x2, y2, 'fmt', y3)
#
# Are valid, but
#
# plot(x, y, y2)
#
# is not. We just duplicate the algorithm here
#
l = len(args)
i = 0
while True:
xi = None
yi = None
formati = None
remaining = l - i
if remaining == 0:
break
elif remaining == 1:
yi = i
i += 1
# The 'remaining != 3 and' encapsulates the wonkyness referred to above
elif remaining == 2 or (remaining != 3 and not isinstance(args[i + 2], basestring)):
# plot(...., x, y [, ....])
xi = i
yi = i + 1
i += 2
else:
# plot(....., x, y, format [, ...])
xi = i
yi = i + 1
formati = i + 2
i += 3
if xi != None:
arg = args[xi]
if isinstance(arg, numpy.ndarray):
xshape = arg.shape
elif isinstance(arg, list):
# Not supporting nested python lists here
xshape = (len(arg),)
else:
raise TypeError("Expected numpy array or list for argument %d" % (xi + 1))
else:
xshape = None
# y isn't optional, pretend it is to preserve code symmetry
if yi != None:
arg = args[yi]
if isinstance(arg, numpy.ndarray):
yshape = arg.shape
elif isinstance(arg, list):
# Not supporting nested python lists here
yshape = (len(arg),)
else:
raise TypeError("Expected numpy array or list for argument %d" % (yi + 1))
else:
yshape = None
if xshape != None and yshape != None and xshape != yshape:
raise TypeError("Shapes of arguments %d and %d aren't compatible" % ((xi + 1), (yi + 1)))
if formati != None and not isinstance(args[formati], basestring):
raise TypeError("Expected format string for argument %d" % (formati + 1))
class Axes(RecordedObject, custom_result.CustomResult):
def _check_plot(self, name, args, kwargs, spec):
_validate_args(args)
def create_widget(self):
widget = PlotWidget(self)
self._replay(widget.axes)
return widget
def filter_method(baseclass, name):
if not default_filter(baseclass, name):
return False
if name.startswith('get_'):
return False
if name == 'create_widget':
return False
return True
Axes._set_target_class(matplotlib.axes.Axes, filter_method)
def plot(*args, **kwargs):
axes = Axes()
axes.plot(*args, **kwargs)
return axes
def imshow(*args, **kwargs):
axes = Axes()
axes.imshow(*args, **kwargs)
return axes
|