[Yt-svn] yt: New extension DualEPS that creates EPS figures with compress...

hg at spacepope.org hg at spacepope.org
Thu Apr 29 19:57:18 PDT 2010


hg Repository: yt
details:   yt/rev/3426c531f05c
changeset: 1633:3426c531f05c
user:      John Wise <jwise at astro.princeton.edu>
date:
Thu Apr 29 22:57:15 2010 -0400
description:
New extension DualEPS that creates EPS figures with compressed bits
*and* vector graphics.  It is able to create figures with one call
from raven plots or PlotCollections.  After creating the figure, you
can further annotate it.

Matplotlib can do this for PDFs but not EPSs, which are saved as
bitmaps that aren't suitable for publication unless they are very high
resolution.

diffstat:

 yt/extensions/DualEPS.py |  696 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 696 insertions(+), 0 deletions(-)

diffs (truncated from 700 to 300 lines):

diff -r ae9295fe98c9 -r 3426c531f05c yt/extensions/DualEPS.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yt/extensions/DualEPS.py	Thu Apr 29 22:57:15 2010 -0400
@@ -0,0 +1,696 @@
+"""
+DualEPS: A class to combine bitmap compression and vector graphics
+
+Author: John Wise <jwise at astro.princeton.edu>
+Date: April 2010
+Affiliation: Princeton
+Homepage: http://yt.enzotools.org/
+
+Requirements: PyX
+
+License:
+  Copyright (C) 2010 John Wise.  All Rights Reserved.
+
+  This file is part of yt.
+
+  yt is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+import pyx
+from yt.mods import *
+from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
+from matplotlib import cm
+
+class DualEPS:
+    def __init__(self, figsize=(12,12)):
+        """
+        Initializes the DualEPS class with a figure size of *figsize*
+        centimeters for a single plot.
+        """
+        pyx.unit.set(xscale=1.4)
+        self.figsize = figsize
+        self.canvas = None
+        self.colormaps = None
+        self.axes_drawn = False
+
+    def hello_world(self):
+        """
+        A simple test.
+        """
+        if self.canvas is None:
+            self.canvas = pyx.canvas.canvas()
+        p = pyx.path.line(0,0,1,1)
+        self.canvas.stroke(p)
+        self.canvas.text(0,0, "Hello world.")
+
+#=============================================================================
+
+    def axis_box(self, xrange=(0,1), yrange=(0,1), xlabel="", ylabel="",
+                 xlog=False, ylog=False, tickcolor=None, bare_axes=False,
+                 pos=(0,0), xaxis_side=0, yaxis_side=0):
+        """
+        Draws an axis box at position *pos* with a range of *xrange*
+        and *yrange*, labels *xlabel* and *ylabel*.  The colors of the
+        tickmarks can be specified with *tickcolor*.  For no tick
+        labels or marks, set *bare_axes* to True.  For the x-axis
+        (y-axis) labels to be on the right (top), set *xaxis_side*
+        (*yaxis_side*) to 1.
+        """
+        if tickcolor is None:
+            c1 = pyx.graph.axis.painter.regular\
+                 (tickattrs=[pyx.color.cmyk.black])
+            c2 = pyx.graph.axis.painter.regular\
+                 (tickattrs=[pyx.color.cmyk.black], labelattrs=None)
+        else:
+            c1 = pyx.graph.axis.painter.regular(tickattrs=[tickcolor])
+            c2 = pyx.graph.axis.painter.regular\
+                 (tickattrs=[tickcolor], labelattrs=None)
+
+        xticklabels = True
+        yticklabels = True
+        if xaxis_side == 0:
+            xleftlabel = xlabel
+            xrightlabel = ""
+            c1x = c1
+            c2x = c2
+        elif xaxis_side == 1:
+            xleftlabel = ""
+            xrightlabel = xlabel
+            c1x = c2
+            c2x = c1
+        else:
+            xticklabels = False
+            xleftlabel = ""
+            xrightlabel = ""
+            c1x = c1
+            c2x = c2
+        if yaxis_side == 0:
+            yleftlabel = ylabel
+            yrightlabel = ""
+            c1y = c1
+            c2y = c2
+        elif yaxis_side == 1:
+            yleftlabel = ""
+            yrightlabel = ylabel
+            c1y = c2
+            c2y = c1
+        else:
+            yticklabels = False
+            yleftlabel = ""
+            yrightlabel = ""
+            c1y = c1
+            c2y = c2
+
+        if xlog:
+            if xticklabels:
+                xaxis = pyx.graph.axis.log(min=xrange[0],max=xrange[1],
+                                           title=xleftlabel, painter=c1x)
+                xaxis2 = pyx.graph.axis.log(min=xrange[0],max=xrange[1],
+                                            title=xrightlabel, painter=c2x)
+            else:
+                xaxis = pyx.graph.axis.log(min=xrange[0],max=xrange[1],
+                                           title=xleftlabel, painter=c1x,
+                                           parter=None)
+                xaxis2 = pyx.graph.axis.log(min=xrange[0],max=xrange[1],
+                                            title=xrightlabel, painter=c2x,
+                                            parter=None)
+        else:
+            if xticklabels:
+                xaxis = pyx.graph.axis.lin(min=xrange[0],max=xrange[1],
+                                           title=xleftlabel, painter=c1x)
+                xaxis2 = pyx.graph.axis.lin(min=xrange[0],max=xrange[1],
+                                            title=xrightlabel, painter=c2x)
+            else:
+                xaxis = pyx.graph.axis.lin(min=xrange[0],max=xrange[1],
+                                           title=xleftlabel, painter=c1x,
+                                           parter=None)
+                xaxis2 = pyx.graph.axis.lin(min=xrange[0],max=xrange[1],
+                                            title=xrightlabel, painter=c2x,
+                                            parter=None)
+        if ylog:
+            if yticklabels:
+                yaxis = pyx.graph.axis.log(min=yrange[0],max=yrange[1],
+                                           title=yleftlabel, painter=c1y)
+                yaxis2 = pyx.graph.axis.log(min=yrange[0],max=yrange[1],
+                                            title=yrightlabel, painter=c2y)
+            else:
+                yaxis = pyx.graph.axis.log(min=yrange[0],max=yrange[1],
+                                           title=yleftlabel, painter=c1y,
+                                           parter=None)
+                yaxis2 = pyx.graph.axis.log(min=yrange[0],max=yrange[1],
+                                            title=yrightlabel, painter=c2y,
+                                            parter=None)
+        else:
+            if yticklabels:
+                yaxis = pyx.graph.axis.lin(min=yrange[0],max=yrange[1],
+                                           title=yleftlabel, painter=c1y)
+                yaxis2 = pyx.graph.axis.lin(min=yrange[0],max=yrange[1],
+                                            title=yrightlabel, painter=c2y)
+            else:
+                yaxis = pyx.graph.axis.lin(min=yrange[0],max=yrange[1],
+                                           title=yleftlabel, painter=c1y,
+                                           parter=None)
+                yaxis2 = pyx.graph.axis.lin(min=yrange[0],max=yrange[1],
+                                            title=yrightlabel, painter=c2y,
+                                            parter=None)
+
+        if bare_axes:
+            if ylog:
+                yaxis = pyx.graph.axis.log(min=yrange[0], max=yrange[1],
+                                           title=yleftlabel, parter=None)
+                yaxis2 = pyx.graph.axis.log(min=yrange[0], max=yrange[1],
+                                            title=yrightlabel, parter=None)
+            else:
+                yaxis = pyx.graph.axis.lin(min=yrange[0], max=yrange[1],
+                                           title=yleftlabel, parter=None)
+                yaxis2 = pyx.graph.axis.lin(min=yrange[0], max=yrange[1],
+                                            title=yrightlabel, parter=None)
+            if xlog:
+                xaxis = pyx.graph.axis.log(min=xrange[0], max=xrange[1],
+                                           title=xleftlabel, parter=None)
+                xaxis2 = pyx.graph.axis.log(min=xrange[0], max=xrange[1],
+                                            title=xrightlabel, parter=None)
+            else:
+                xaxis = pyx.graph.axis.lin(min=xrange[0], max=xrange[1],
+                                           title=xleftlabel, parter=None)
+                xaxis2 = pyx.graph.axis.lin(min=xrange[0], max=xrange[1],
+                                            title=xrightlabel, parter=None)
+
+        blank_data = pyx.graph.data.points([(-1,-1),(-0.99,-0.99)], x=1,y=2)
+        if self.canvas is None:
+            self.canvas = pyx.graph.graphxy \
+                          (width=self.figsize[0], height=self.figsize[1],
+                           x=xaxis, y=yaxis, x2=xaxis2, y2=yaxis2,
+                           xpos=pos[0], ypos=pos[1])
+            self.canvas.plot(blank_data)
+        else:
+            plot = pyx.graph.graphxy \
+                   (width=self.figsize[0], height=self.figsize[1],
+                    x=xaxis, y=yaxis, x2=xaxis2, y2=yaxis2,
+                    xpos=pos[0], ypos=pos[1])
+            plot.plot(blank_data)
+            self.canvas.insert(plot)
+        self.axes_drawn = True
+
+#=============================================================================
+
+    def axis_box_yt(self, plot, units=None, bare_axes=False, **kwargs):
+        plot._redraw_image()
+        if isinstance(plot, raven.PlotTypes.VMPlot):
+            if units == None:
+                # Determine the best units
+                astro_units = ['cm', 'rsun', 'au', 'pc', 'kpc', 'Mpc']
+                best_fit = 0
+                while plot.width*plot.pf[astro_units[best_fit]] > 1e3 and \
+                          best_fit < len(astro_units):
+                    best_fit += 1
+                units = astro_units[best_fit]
+            _xrange = (0, plot.width * plot.pf[units])
+            _yrange = (0, plot.width * plot.pf[units])
+            _xlog = False
+            _ylog = False
+            if bare_axes:
+                _xlabel = ""
+                _ylabel = ""
+            else:
+                _xlabel = '%s (%s)' % (lagos.x_names[plot.data.axis], units)
+                _ylabel = '%s (%s)' % (lagos.y_names[plot.data.axis], units)
+            _tickcolor = pyx.color.cmyk.white
+        else:
+            _xrange = plot._axes.get_xlim()
+            _yrange = plot._axes.get_ylim()
+            _xlog = plot._log_x
+            _ylog = plot._log_y
+            if bare_axes:
+                _xlabel = ""
+                _ylabel = ""
+            else:
+                _xlabel = plot._x_label
+                _ylabel = plot._y_label
+            _tickcolor = None
+        self.axis_box(xrange=_xrange, yrange=_yrange, xlabel=_xlabel,
+                      ylabel=_ylabel, tickcolor=_tickcolor, xlog=_xlog,
+                      ylog=_ylog, bare_axes=bare_axes, **kwargs)
+
+#=============================================================================
+
+    def insert_image(self, filename, pos=(0,0)):
+        """
+        Inserts a JPEG file *filename* at *pos*.
+        """
+        image = pyx.bitmap.jpegimage(filename)
+        self.canvas.insert(pyx.bitmap.bitmap(pos[0], pos[1], image,
+                                             compressmode=None,
+                                             width=self.figsize[0],
+                                             height=self.figsize[1]))
+
+#=============================================================================
+
+    def insert_image_yt(self, plot, pos=(0,0)):
+        """
+        Inserts a bitmap taken from a yt plot.  For best results, set
+        use_colorbar=False when creating the yt image.
+        """
+        # We need to remove the colorbar (if necessary), remove the
+        # axes, and resize the figure to span the entire figure
+        if plot.colorbar != None and \
+               isinstance(plot, raven.PlotTypes.VMPlot):
+            print "WARNING: Image (slices, projections, etc.) plots must not"\
+                  "have a colorbar."
+            print "Removing it."
+            plot.colorbar = None
+        if self.canvas is None:
+            self.canvas = pyx.canvas.canvas()
+        plot._redraw_image()
+        _p1 = plot._figure
+        if isinstance(plot, raven.PlotTypes.ProfilePlot):
+            # Remove colorbar
+            _p1.delaxes(_p1.axes[1])
+        _p1.axes[0].set_axis_off()  # remove axes
+        _p1.axes[0].set_position([0,0,1,1])  # rescale figure
+        _p1.set_facecolor('w')  # set background color
+        figure_canvas = FigureCanvas(_p1)
+        figure_canvas.draw()
+        size = _p1.get_size_inches() * _p1.dpi
+        image = pyx.bitmap.image(size[0], size[1], "RGB",
+                                 figure_canvas.tostring_rgb())
+        figure_canvas.print_png('test.png')
+        self.canvas.insert(pyx.bitmap.bitmap(pos[0], pos[1], image,
+                                             width=self.figsize[0],
+                                             height=self.figsize[1]))
+
+#=============================================================================
+
+    def colorbar(self, name, zrange=(0,1), label="", log=False, tickcolor=None,
+                 orientation="right", pos=[0,0], shrink=1.0):
+        """



More information about the yt-svn mailing list