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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
# Copyright 2007, 2008 Owen Taylor
# Copyright 2008 Kai Willadsen
# Copyright 2009, 2010 Jorn Baayen
#
# 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 pango
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
DEFAULT_FIGURE_WIDTH = 6
DEFAULT_FIGURE_HEIGHT = 4.5
DEFAULT_ASPECT_RATIO = DEFAULT_FIGURE_WIDTH / DEFAULT_FIGURE_HEIGHT
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(custom_result.ResultWidget):
__gsignals__ = {
'button-press-event': 'override',
'button-release-event': 'override',
'expose-event': 'override',
'size-allocate': 'override',
'unrealize': 'override'
}
def __init__(self, result):
custom_result.ResultWidget.__init__(self)
figsize=(DEFAULT_FIGURE_WIDTH, DEFAULT_FIGURE_HEIGHT)
self.figure = Figure(facecolor='white', figsize=figsize)
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
self.sidebar_width = -1
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 recompute_figure_size(self):
width = (self.sidebar_width / self.figure.dpi)
height = width / DEFAULT_ASPECT_RATIO
self.figure.set_figwidth(width)
self.figure.set_figheight(height)
self.queue_resize()
def sync_dpi(self, dpi):
self.figure.set_dpi(dpi)
if self.sidebar_width >= 0:
self.recompute_figure_size()
def set_sidebar_width(self, width):
if self.sidebar_width == width:
return
self.sidebar_width = width
if self.sidebar_width >= 0:
self.recompute_figure_size()
def sync_style(self, style):
self.cached_contents = None
matplotlib.rcParams['font.size'] = self.parent.style.font_desc.get_size() / pango.SCALE
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)
# Return dimensions to caller.
# 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 is not 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 is not 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 is not None and yshape is not None and xshape != yshape:
raise TypeError("Shapes of arguments %d and %d aren't compatible" % ((xi + 1), (yi + 1)))
if formati is not None and not isinstance(args[formati], basestring):
raise TypeError("Expected format string for argument %d" % (formati + 1))
class Axes(RecordedObject, custom_result.CustomResult):
def __init__(self, display='inline'):
RecordedObject.__init__(self)
self.display = display
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 print_result(self, print_context, render=True):
figure = Figure(facecolor='white', figsize=(6,4.5))
figure.set_dpi(72)
# Don't draw the frame, please.
figure.set_frameon(False)
canvas = _PlotResultCanvas(figure)
axes = figure.add_subplot(111)
self._replay(axes)
width, height = figure.bbox.width, figure.bbox.height
if render:
cr = print_context.get_cairo_context()
renderer = RendererCairo(figure.dpi)
renderer.set_width_height(width, height)
if hasattr(renderer, 'gc'):
# matplotlib-0.99 and newer
renderer.gc.ctx = cr
else:
# matplotlib-0.98
# RendererCairo.new_gc() does a restore to get the context back
# to its original state after changes
cr.save()
renderer.ctx = cr
figure.draw(renderer)
if not hasattr(renderer, 'gc'):
# matplotlib-0.98
# Reverse the save() we did before drawing
cr.restore()
return height
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
|