2 from matplotlib
.figure
import Figure
3 from matplotlib
.backends
.backend_cairo
import RendererCairo
6 from reinteract
.custom_result
import CustomResult
12 class PlotResult(CustomResult
):
13 def __init__(self
, *args
, **kwargs
):
15 self
.__kwargs
= kwargs
17 def create_widget(self
):
18 widget
= PlotWidget(self
)
19 widget
.axes
.plot(*self
.__args
, **self
.__kwargs
)
23 class ImshowResult(CustomResult
):
24 def __init__(self
, *args
, **kwargs
):
26 self
.__kwargs
= kwargs
28 def create_widget(self
):
29 widget
= PlotWidget(self
)
30 widget
.axes
.imshow(*self
.__args
, **self
.__kwargs
)
34 class PlotWidget(gtk
.DrawingArea
):
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)
61 self
.figure
.draw(renderer
)
63 def do_button_press_event(self
, event
):
66 def do_button_release_event(self
, event
):
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)
90 # is not. We just duplicate the algorithm here
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 [, ....])
112 # plot(....., x, y, format [, ...])
120 if isinstance(arg
, numpy
.ndarray
):
122 elif isinstance(arg
, list):
123 # Not supporting nested python lists here
126 raise TypeError("Expected numpy array or list for argument %d" % (xi
+ 1))
130 # y isn't optional, pretend it is to preserve code symmetry
134 if isinstance(arg
, numpy
.ndarray
):
136 elif isinstance(arg
, list):
137 # Not supporting nested python lists here
140 raise TypeError("Expected numpy array or list for argument %d" % (yi
+ 1))
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
):
153 return PlotResult(*args
, **kwargs
)
155 def imshow(*args
, **kwargs
):
156 return ImshowResult(*args
, **kwargs
)