def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w', orientation='portrait'): root, ext = os.path.splitext(filename) ext = ext[1:] if ext == '': ext = IMAGE_FORMAT_DEFAULT filename = filename + '.' + ext self.figure.dpi.set(dpi) self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) ext = ext.lower() if ext in ('jpg', 'png'): # native printing width, height = self.figure.get_width_height() width, height = int(width), int(height) self._render_to_pixmap(width, height) # jpg colors don't match the display very well, png colors match better pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, width, height) #pixbuf.get_from_drawable(self._pixmap, self._pixmap.get_colormap(), pixbuf.get_from_drawable(self._pixmap, self._renderer._cmap, 0, 0, 0, 0, width, height) # pixbuf.save() recognises 'jpeg' not 'jpg' if ext == 'jpg': ext = 'jpeg' try: pixbuf.save(filename, ext) except gobject.GError, exc: error_msg('Save figure failure:\n%s' % (exc,), parent=self)
def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w', orientation='portrait'): root, ext = os.path.splitext(filename) ext = ext[1:] if ext == '': ext = IMAGE_FORMAT_DEFAULT filename = filename + '.' + ext self.figure.dpi.set(dpi) self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) ext = ext.lower() if ext in ('jpg', 'png'): # native printing width, height = self.figure.get_width_height() width, height = int(width), int(height) self._render_to_pixmap(width, height) # jpg colors don't match the display very well, png colors match better pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, width, height) pixbuf.get_from_drawable(self._pixmap, self._pixmap.get_colormap(), 0, 0, 0, 0, width, height) # pixbuf.save() recognises 'jpeg' not 'jpg' if ext == 'jpg': ext = 'jpeg' try: pixbuf.save(filename, ext) except gobject.GError, exc: error_msg('Save figure failure:\n%s' % (exc,), parent=self)
def draw_lines(self, gc, x, y): """ x and y are equal length arrays, draw lines connecting each point in x, y """ if len(x)==0: return if len(x)!=len(y): error_msg('x and y must be the same length') type = '<path ' y = self.height - y details = [' d="M %f,%f' % (x[0], y[0]) ] xys = zip(x[1:], y[1:]) details.extend(['L %f,%f' % tup for tup in xys]) details.append('" ') details = ' '.join(details) self._draw_svg(type, details, gc, None)
class FigureCanvasGDK(FigureCanvasBase): def __init__(self, figure): FigureCanvasBase.__init__(self, figure) self._pixmap_width = -1 self._pixmap_height = -1 self._renderer_init() def _renderer_init(self): #self._renderer = RendererGDK (self, self.figure.dpi) # self is no longer a widget subclass self._renderer = RendererGDK (gtk.DrawingArea(), self.figure.dpi) def _render_to_pixmap(self, width, height): """Render the figure to a gdk.Pixmap, is used for - rendering the pixmap to display (pylab.draw) - rendering the pixmap to save to a file (pylab.savefig) Should not be overridden """ if DEBUG: print 'FigureCanvasGDK.%s' % fn_name() create_pixmap = False if width > self._pixmap_width: # increase the pixmap in 10%+ (rather than 1 pixel) steps self._pixmap_width = max (int (self._pixmap_width * 1.1), width) create_pixmap = True if height > self._pixmap_height: self._pixmap_height = max (int (self._pixmap_height * 1.1), height) create_pixmap = True if create_pixmap: if DEBUG: print 'FigureCanvasGTK.%s new pixmap' % fn_name() #self._pixmap = gtk.gdk.Pixmap (self.window, self._pixmap_width, self._pixmap = gtk.gdk.Pixmap (None, self._pixmap_width, self._pixmap_height, depth=24) # gtk backend must use self.window self._renderer._set_pixmap (self._pixmap) self._renderer._set_width_height (width, height) self.figure.draw (self._renderer) def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w', orientation='portrait'): root, ext = os.path.splitext(filename) ext = ext[1:] if ext == '': ext = IMAGE_FORMAT_DEFAULT filename = filename + '.' + ext self.figure.dpi.set(dpi) self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) ext = ext.lower() if ext in ('jpg', 'png'): # native printing width, height = self.figure.get_width_height() width, height = int(width), int(height) self._render_to_pixmap(width, height) # jpg colors don't match the display very well, png colors match better pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, width, height) pixbuf.get_from_drawable(self._pixmap, self._pixmap.get_colormap(), 0, 0, 0, 0, width, height) # pixbuf.save() recognises 'jpeg' not 'jpg' if ext == 'jpg': ext = 'jpeg' try: pixbuf.save(filename, ext) except gobject.GError, exc: error_msg('Save figure failure:\n%s' % (exc,), parent=self) elif ext in ('eps', 'ps', 'svg',): if ext == 'svg': from backend_svg import FigureCanvasSVG as FigureCanvas else: from backend_ps import FigureCanvasPS as FigureCanvas try: fc = self.switch_backends(FigureCanvas) fc.print_figure(filename, dpi, facecolor, edgecolor, orientation) except IOError, exc: error_msg("Save figure failure:\n%s: %s" % (exc.filename, exc.strerror), parent=self)
error_msg('Save figure failure:\n%s' % (exc,), parent=self) elif ext in ('eps', 'ps', 'svg',): if ext == 'svg': from backend_svg import FigureCanvasSVG as FigureCanvas else: from backend_ps import FigureCanvasPS as FigureCanvas try: fc = self.switch_backends(FigureCanvas) fc.print_figure(filename, dpi, facecolor, edgecolor, orientation) except IOError, exc: error_msg("Save figure failure:\n%s: %s" % (exc.filename, exc.strerror), parent=self) except Exception, exc: error_msg("Save figure failure:\n%s" % exc, parent=self) elif ext in ('bmp', 'raw', 'rgb',): try: from backend_agg import FigureCanvasAgg as FigureCanvas except: error_msg('Save figure failure:\n' 'Agg must be loaded to save as bmp, raw and rgb', parent=self) else: fc = self.switch_backends(FigureCanvas) fc.print_figure(filename, dpi, facecolor, edgecolor, orientation) else: error_msg('Format "%s" is not supported.\nSupported formats are %s.' % (ext, ', '.join(IMAGE_FORMAT)),
def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w', orientation='portrait'): """ Render the figure to hardcopy. Set the figure patch face and edge colors. This is useful because some of the GUIs have a gray figure face color background and you'll probably want to override this on hardcopy If the extension matches PNG, write a PNG file If the extension matches BMP or RAW, write an RGBA bitmap file If filename is a fileobject, write png to file object (thus you can, for example, write the png to stdout """ if DEBUG: print 'FigureCanvasAgg.print_figure' # store the orig figure dpi, color and size information so we # can restore them later. For image creation alone, this is # not important since after the print the figure is done. But # backend_agg may be used as a renderer for a GUI figure, and # restoring figure props will be important in that case. # TODO: move most of this functionality into backend_bases origDPI = self.figure.dpi.get() origfacecolor = self.figure.get_facecolor() origedgecolor = self.figure.get_edgecolor() self.figure.dpi.set(dpi) self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) # render the printed figure self.draw() if isinstance(filename, file): # assume png and write to fileobject self.renderer._renderer.write_png(filename) #pass else: # take a look at the extension and choose the print handler basename, ext = os.path.splitext(filename) if not len(ext): ext = '.png' filename += ext ext = ext.lower() if (ext.find('rgb')>=0 or ext.find('raw')>=0 or ext.find('bmp')>=0 ): # agg doesn't handle unicode yet self.renderer._renderer.write_rgba(str(filename)) elif ext.find('png')>=0: # agg doesn't handle unicode yet self.renderer._renderer.write_png(str(filename)) #pass elif ext.find('svg')>=0: from backend_svg import FigureCanvasSVG svg = self.switch_backends(FigureCanvasSVG) svg.figure.dpi.set(72) svg.print_figure(filename, 72, facecolor, edgecolor, orientation) elif ext.find('ps')>=0 or ext.find('ep')>=0: from backend_ps import FigureCanvasPS # lazy import ps = self.switch_backends(FigureCanvasPS) ps.figure.dpi.set(72) ps.print_figure(filename, 72, facecolor, edgecolor, orientation) else: error_msg('Do not know know to handle extension *%s' % ext) # restore the original figure properties self.figure.dpi.set(origDPI) self.figure.set_facecolor(origfacecolor) self.figure.set_edgecolor(origedgecolor)
def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w', orientation='portrait'): """ Render the figure to hardcopy. Set the figure patch face and edge colors. This is useful because some of the GUIs have a gray figure face color background and you'll probably want to override this on hardcopy If the extension matches PNG, write a PNG file If the extension matches BMP or RAW, write an RGBA bitmap file If filename is a fileobject, write png to file object (thus you can, for example, write the png to stdout """ if __debug__: verbose.report('FigureCanvasAgg.print_figure', 'debug-annoying') # store the orig figure dpi, color and size information so we # can restore them later. For image creation alone, this is # not important since after the print the figure is done. But # backend_agg may be used as a renderer for a GUI figure, and # restoring figure props will be important in that case. # TODO: move most of this functionality into backend_bases origDPI = self.figure.dpi.get() origfacecolor = self.figure.get_facecolor() origedgecolor = self.figure.get_edgecolor() self.figure.dpi.set(dpi) self.figure.set_facecolor(facecolor) self.figure.set_edgecolor(edgecolor) # render the printed figure self.draw() if isinstance(filename, file): # assume png and write to fileobject self.renderer._renderer.write_png(filename) #pass else: # take a look at the extension and choose the print handler basename, ext = os.path.splitext(filename) if not len(ext): ext = '.png' filename += ext ext = ext.lower() if (ext.find('rgb')>=0 or ext.find('raw')>=0 or ext.find('bmp')>=0 ): # agg doesn't handle unicode yet self.renderer._renderer.write_rgba(str(filename)) elif ext.find('png')>=0: # agg doesn't handle unicode yet self.renderer._renderer.write_png(str(filename)) #pass elif ext.find('svg')>=0: from backend_svg import FigureCanvasSVG svg = self.switch_backends(FigureCanvasSVG) svg.print_figure(filename, dpi, facecolor, edgecolor, orientation) elif ext.find('ps')>=0 or ext.find('ep')>=0: from backend_ps import FigureCanvasPS # lazy import ps = self.switch_backends(FigureCanvasPS) ps.print_figure(filename, dpi, facecolor, edgecolor, orientation) else: error_msg('Do not know know to handle extension *%s' % ext) # restore the original figure properties self.figure.dpi.set(origDPI) self.figure.set_facecolor(origfacecolor) self.figure.set_edgecolor(origedgecolor)