Skip to content

Commit dd81ec4

Browse files
author
Steve Chaplin
committed
SC 25/02/2005
svn path=/trunk/matplotlib/; revision=985
1 parent 8761974 commit dd81ec4

File tree

4 files changed

+65
-50
lines changed

4 files changed

+65
-50
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
New entries should be added at the top
22

3+
2005-02-25 Add experimental feature to backend_gtk.py to enable/disable
4+
double buffering (DBL_BUFFER=True/False) - SC
5+
36
2005-02-24 colors.py change ColorConverter.to_rgb() so it always returns rgb
47
(and not rgba), allow cnames keys to be cached, change the exception
58
raised from RuntimeError to ValueError (like hex2color())

examples/anim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.3
1+
#!/usr/bin/env python
22
"""
33
A simple example of an animated plot in matplotlib. You can test the
44
speed of animation of various backends by running the script with the

lib/matplotlib/backends/backend_gdk.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
from __future__ import division
22

3-
import os, math
3+
import math, os, warnings
44
import sys
55
def fn_name(): return sys._getframe(1).f_code.co_name
66

77
import matplotlib
8-
from matplotlib import verbose
9-
from matplotlib.numerix import asarray, fromstring, UInt8, zeros, \
10-
where, transpose, nonzero, indices, ones, nx
118

129
import matplotlib.numerix as numerix
13-
from matplotlib.cbook import is_string_like, enumerate
14-
from matplotlib.font_manager import fontManager
10+
from matplotlib.numerix import asarray, fromstring, UInt8, zeros, \
11+
where, transpose, nonzero, indices, ones, nx
1512

13+
from matplotlib._pylab_helpers import Gcf
1614
from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \
1715
FigureManagerBase, FigureCanvasBase
18-
from matplotlib._pylab_helpers import Gcf
16+
from matplotlib.cbook import is_string_like, enumerate
1917
from matplotlib.figure import Figure
18+
from matplotlib.font_manager import fontManager
2019
from matplotlib.mathtext import math_parse_s_ft2font
2120

2221
pygtk_version_required = (1,99,16)
@@ -29,7 +28,8 @@ def fn_name(): return sys._getframe(1).f_code.co_name
2928
raise SystemExit('PyGTK version %d.%d.%d or greater is required to run the GTK Matplotlib backends'
3029
% pygtk_version_required)
3130

32-
import gtk, gobject, pango
31+
#import gobject
32+
import gtk, pango
3333
from gtk import gdk
3434
if gtk.pygtk_version < pygtk_version_required:
3535
raise SystemExit ("PyGTK %d.%d.%d is installed\n"
@@ -78,7 +78,7 @@ class RendererGDK(RendererBase):
7878
rotated = {} # a map from text prop tups to rotated text pixbufs
7979

8080
def __init__(self, gtkDA, dpi):
81-
# gtkDA is used in '<widget>.create_pango_layout(s)' only
81+
# gtkDA is used in '<widget>.create_pango_layout(s)' (and cmap line below) only
8282
self.gtkDA = gtkDA
8383
self.dpi = dpi
8484
self._cmap = gtkDA.get_colormap()
@@ -118,20 +118,22 @@ def draw_image(self, x, y, im, origin, bbox):
118118
X.shape = rows, cols, 4
119119

120120
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
121-
#has_alpha=1, bits_per_sample=8,
122121
has_alpha=True, bits_per_sample=8,
123122
width=cols, height=rows)
124123
try:
125124
pa = pb.get_pixels_array()
126125
except AttributeError:
127126
pa = pb.pixel_array
128-
except RuntimeError, exc: # pygtk was not compiled with Numeric Python support
129-
raise
127+
except RuntimeError: # pygtk was not compiled with Numeric Python support
128+
warnings.warn('draw_image not supported: %s' % exc)
129+
return
130+
130131
pa[:,:,:] = X
131132

132133
gc = self.new_gc()
133134

134-
if flipud: y = self.height-y-rows
135+
if flipud:
136+
y = self.height-y-rows
135137

136138
try: # requires GTK+ 2.2
137139
# can use None instead of gc.gdkGC, if don't need clipping
@@ -252,7 +254,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
252254
try: # requires GTK+ 2.2
253255
# can use None instead of gc.gdkGC, if don't need clipping
254256
self.gdkDrawable.draw_pixbuf (gc.gdkGC, pb, 0, 0,
255-
int(x), int(y), cols, rows,
257+
int(x), int(y), imw, imh,
256258
gdk.RGB_DITHER_NONE, 0, 0)
257259
except AttributeError:
258260
pb.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0,
@@ -346,7 +348,6 @@ def flipy(self):
346348
return True
347349

348350
def get_canvas_width_height(self):
349-
#return self.gtkDA.allocation.width, self.gtkDA.allocation.height
350351
return self.width, self.height
351352

352353
def get_text_width_height(self, s, prop, ismath):
@@ -474,15 +475,13 @@ def __init__(self, figure):
474475
self._renderer_init()
475476

476477
def _renderer_init(self):
477-
#self._renderer = RendererGDK (self, self.figure.dpi) # self is no longer a widget subclass
478478
self._renderer = RendererGDK (gtk.DrawingArea(), self.figure.dpi)
479479

480480

481-
def _render_to_pixmap(self, width, height):
481+
def _render_figure(self, width, height):
482482
"""Render the figure to a gdk.Pixmap, is used for
483483
- rendering the pixmap to display (pylab.draw)
484484
- rendering the pixmap to save to a file (pylab.savefig)
485-
Should not be overridden
486485
"""
487486
if DEBUG: print 'FigureCanvasGDK.%s' % fn_name()
488487
create_pixmap = False
@@ -497,7 +496,6 @@ def _render_to_pixmap(self, width, height):
497496

498497
if create_pixmap:
499498
if DEBUG: print 'FigureCanvasGTK.%s new pixmap' % fn_name()
500-
#self._pixmap = gtk.gdk.Pixmap (self.window, self._pixmap_width,
501499
self._pixmap = gtk.gdk.Pixmap (None, self._pixmap_width,
502500
self._pixmap_height, depth=24)
503501
# gtk backend must use self.window
@@ -523,7 +521,7 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
523521
if ext in ('jpg', 'png'): # native printing
524522
width, height = self.figure.get_width_height()
525523
width, height = int(width), int(height)
526-
self._render_to_pixmap(width, height)
524+
self._render_figure(width, height)
527525

528526
# jpg colors don't match the display very well, png colors match better
529527
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8,

lib/matplotlib/backends/backend_gtk.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def fn_name(): return sys._getframe(1).f_code.co_name
5151
IMAGE_FORMAT.sort()
5252
IMAGE_FORMAT_DEFAULT = 'png'
5353

54+
DBL_BUFFER = True
55+
#DBL_BUFFER = False # test to see if switching double buffering off is useful
56+
5457

5558
cursord = {
5659
cursors.MOVE : gtk.gdk.Cursor(gtk.gdk.FLEUR),
@@ -119,10 +122,6 @@ def __init__(self, figure):
119122

120123
self.set_flags(gtk.CAN_FOCUS)
121124
self.grab_focus()
122-
# widget can take any size, gtk.Window sets default size
123-
#self.set_size_request (int (figure.bbox.width()),
124-
# int (figure.bbox.height()))
125-
126125
self.set_double_buffered(False)
127126

128127
self.connect('key_press_event', self.key_press_event)
@@ -212,33 +211,36 @@ def _renderer_init(self):
212211
_set_pixmap ()
213212
_set_width_height ()
214213
that are used by
215-
_render_to_pixmap()
214+
_render_figure()
216215
"""
217216
self._renderer = RendererGDK (self, self.figure.dpi)
218217

219218

220-
def _render_to_pixmap(self, width, height):
219+
def _render_figure(self, width, height):
221220
"""Render the figure to a gdk.Pixmap, is used for
222221
- rendering the pixmap to display (pylab.draw)
223222
- rendering the pixmap to save to a file (pylab.savefig)
224-
Should not be overridden
223+
Should not be overridden by GTK backends
225224
"""
226225
if DEBUG: print 'FigureCanvasGTK.%s' % fn_name()
227-
create_pixmap = False
228-
if width > self._pixmap_width:
229-
# increase the pixmap in 10%+ (rather than 1 pixel) steps
230-
self._pixmap_width = max (int (self._pixmap_width * 1.1), width)
231-
create_pixmap = True
232-
233-
if height > self._pixmap_height:
234-
self._pixmap_height = max (int (self._pixmap_height * 1.1), height)
235-
create_pixmap = True
236-
237-
if create_pixmap:
238-
if DEBUG: print 'FigureCanvasGTK.%s new pixmap' % fn_name()
239-
self._pixmap = gtk.gdk.Pixmap (self.window, self._pixmap_width,
240-
self._pixmap_height)
241-
self._renderer._set_pixmap (self._pixmap)
226+
if DBL_BUFFER:
227+
create_pixmap = False
228+
if width > self._pixmap_width:
229+
# increase the pixmap in 10%+ (rather than 1 pixel) steps
230+
self._pixmap_width = max (int (self._pixmap_width * 1.1), width)
231+
create_pixmap = True
232+
233+
if height > self._pixmap_height:
234+
self._pixmap_height = max (int (self._pixmap_height * 1.1), height)
235+
create_pixmap = True
236+
237+
if create_pixmap:
238+
if DEBUG: print 'FigureCanvasGTK.%s new pixmap' % fn_name()
239+
self._pixmap = gtk.gdk.Pixmap (self.window, self._pixmap_width,
240+
self._pixmap_height)
241+
self._renderer._set_pixmap (self._pixmap)
242+
else:
243+
self._renderer._set_pixmap (self.window)
242244

243245
self._renderer._set_width_height (width, height)
244246
self.figure.draw (self._renderer)
@@ -249,12 +251,18 @@ def expose_event(self, widget, event):
249251
Should not be overridden.
250252
"""
251253
if DEBUG: print 'FigureCanvasGTK.%s' % fn_name()
252-
if self._draw_pixmap and GTK_WIDGET_DRAWABLE(self):
253-
width, height = self.allocation.width, self.allocation.height
254-
self._render_to_pixmap(width, height)
255-
self.window.set_back_pixmap (self._pixmap, False)
256-
self.window.clear() # draw pixmap as the gdk.Window's bg
257-
self._draw_pixmap = False
254+
if DBL_BUFFER:
255+
if self._draw_pixmap and GTK_WIDGET_DRAWABLE(self):
256+
width, height = self.allocation.width, self.allocation.height
257+
self._render_figure(width, height)
258+
self.window.set_back_pixmap (self._pixmap, False)
259+
self.window.clear() # draw pixmap as the gdk.Window's bg
260+
self._draw_pixmap = False
261+
else:
262+
if GTK_WIDGET_DRAWABLE(self):
263+
width, height = self.allocation.width, self.allocation.height
264+
self._render_figure(width, height)
265+
258266
return True
259267

260268

@@ -283,9 +291,15 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
283291

284292
ext = ext.lower()
285293
if ext in ('jpg', 'png'): # native printing
294+
global DBL_BUFFER
295+
dbl_buffer_save = DBL_BUFFER
296+
DBL_BUFFER = True # need to use pixmap not gdk.window
297+
286298
width, height = self.figure.get_width_height()
287299
width, height = int(width), int(height)
288-
self._render_to_pixmap(width, height)
300+
self._render_figure(width, height)
301+
302+
DBL_BUFFER = dbl_buffer_save
289303

290304
# jpg colors don't match the display very well, png colors match better
291305
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8,

0 commit comments

Comments
 (0)