Fix left-over debugging print
[reinteract/rox.git] / lib / replot.py
blob81d437f3d8357289d2c1803166abec7181159154
1 import gtk
2 from matplotlib.figure import Figure
3 from matplotlib.backends.backend_cairo import RendererCairo
4 import numpy
6 from reinteract.custom_result import CustomResult
8 class _DummyCanvas:
9 def draw_event(*args):
10 pass
12 class PlotResult(CustomResult):
13 def __init__(self, *args, **kwargs):
14 self.__args = args
15 self.__kwargs = kwargs
17 def create_widget(self):
18 widget = PlotWidget(self)
19 widget.axes.plot(*self.__args, **self.__kwargs)
21 return widget
23 class ImshowResult(CustomResult):
24 def __init__(self, *args, **kwargs):
25 self.__args = args
26 self.__kwargs = kwargs
28 def create_widget(self):
29 widget = PlotWidget(self)
30 widget.axes.imshow(*self.__args, **self.__kwargs)
32 return widget
34 class PlotWidget(gtk.DrawingArea):
35 __gsignals__ = {
36 'button-press-event': 'override',
37 'button-release-event': 'override',
38 'expose-event': 'override'
41 def __init__(self, result):
42 gtk.DrawingArea.__init__(self)
43 self.figure = Figure(facecolor='white', figsize=(6,4.5))
44 self.figure.set_canvas(_DummyCanvas())
46 self.axes = self.figure.add_axes((0.05,0.05,0.9,0.9))
47 self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE)
49 def do_expose_event(self, event):
50 cr = self.window.cairo_create()
52 renderer = RendererCairo(self.figure.dpi)
53 renderer.set_width_height(self.allocation.width, self.allocation.height)
54 renderer.set_ctx_from_surface(cr.get_target())
56 # event.region is not bound: http://bugzilla.gnome.org/show_bug.cgi?id=487158
57 # gdk_context = gtk.gdk.CairoContext(renderer.ctx)
58 # gdk_context.region(event.region)
59 # gdk_context.clip()
61 self.figure.draw(renderer)
63 def do_button_press_event(self, event):
64 return True
66 def do_button_release_event(self, event):
67 return True
69 def do_size_request(self, requisition):
70 requisition.width = self.figure.bbox.width()
71 requisition.height = self.figure.bbox.height()
73 # def do_size_allocate(self, allocation):
74 # gtk.DrawingArea.do_size_allocate(self, allocation)
76 # dpi = self.figure.dpi.get()
77 # self.figure.set_size_inches (allocation.width / dpi, allocation.height / dpi)
79 def _validate_args(args):
81 # The matplotlib argument parsing is a little wonky
83 # plot(x, y, 'fmt', y2)
84 # plot(x1, y2, x2, y2, 'fmt', y3)
86 # Are valid, but
88 # plot(x, y, y2)
90 # is not. We just duplicate the algorithm here
92 l = len(args)
93 i = 0
94 while True:
95 xi = None
96 yi = None
97 formati = None
99 remaining = l - i
100 if remaining == 0:
101 break
102 elif remaining == 1:
103 yi = i
104 i += 1
105 # The 'remaining != 3 and' encapsulates the wonkyness referred to above
106 elif remaining == 2 or (remaining != 3 and not isinstance(args[i + 2], basestring)):
107 # plot(...., x, y [, ....])
108 xi = i
109 yi = i + 1
110 i += 2
111 else:
112 # plot(....., x, y, format [, ...])
113 xi = i
114 yi = i + 1
115 formati = i + 2
116 i += 3
118 if xi != None:
119 arg = args[xi]
120 if isinstance(arg, numpy.ndarray):
121 xshape = arg.shape
122 elif isinstance(arg, list):
123 # Not supporting nested python lists here
124 xshape = (len(arg),)
125 else:
126 raise TypeError("Expected numpy array or list for argument %d" % (xi + 1))
127 else:
128 xshape = None
130 # y isn't optional, pretend it is to preserve code symmetry
132 if yi != None:
133 arg = args[yi]
134 if isinstance(arg, numpy.ndarray):
135 yshape = arg.shape
136 elif isinstance(arg, list):
137 # Not supporting nested python lists here
138 yshape = (len(arg),)
139 else:
140 raise TypeError("Expected numpy array or list for argument %d" % (yi + 1))
141 else:
142 yshape = None
144 if xshape != None and yshape != None and xshape != yshape:
145 raise TypeError("Shapes of arguments %d and %d aren't compatible" % ((xi + 1), (yi + 1)))
147 if formati != None and not isinstance(args[formati], basestring):
148 raise TypeError("Expected format string for argument %d" % (formati + 1))
151 def plot(*args, **kwargs):
152 _validate_args(args)
153 return PlotResult(*args, **kwargs)
155 def imshow(*args, **kwargs):
156 return ImshowResult(*args, **kwargs)