Ticket #5128: trac_5128-matplotlib-plot.3.patch

File trac_5128-matplotlib-plot.3.patch, 5.0 KB (added by jason, 12 years ago)

apply instead of previous patches

  • sage/plot/all.py

    # HG changeset patch
    # User Jason Grout <jason-sage@creativetrax.com>
    # Date 1233870931 21600
    # Node ID f61bcbafa0a80ec20313566540f42b4afaf59f0e
    # Parent  3b1e17c09ce5172481efd9b24fc8921fcf33ab1b
    [mq]: matplotlib_plot
    
    diff -r 3b1e17c09ce5 -r f61bcbafa0a8 sage/plot/all.py
    a b  
    2020from contour_plot import contour_plot, implicit_plot, region_plot
    2121from density_plot import density_plot
    2222from complex_plot import complex_plot
     23from matplotlib_plot import matplotlib_plot
    2324
    2425from animate import Animation as animate
    2526
  • new file sage/plot/matplotlib_plot.py

    diff -r 3b1e17c09ce5 -r f61bcbafa0a8 sage/plot/matplotlib_plot.py
    - +  
     1r"""
     2Implements a wrapper class for Matplotlib artists to be integrated in
     3with the Sage graphics system.  See the documentation for matplotlib_plot.
     4"""
     5
     6#*****************************************************************************
     7#       Copyright (C) 2009 Jason Grout <jason-sage@creativetrax.com>
     8#
     9#  Distributed under the terms of the GNU General Public License (GPL)
     10#
     11#    This code is distributed in the hope that it will be useful,
     12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14#    General Public License for more details.
     15#
     16#  The full text of the GPL is available at:
     17#
     18#                  http://www.gnu.org/licenses/
     19#*****************************************************************************
     20
     21from sage.plot.primitive import GraphicPrimitive
     22from sage.plot.plot import  Graphics
     23from matplotlib.figure import Figure
     24from matplotlib.axes import Axes
     25
     26
     27class Matplotlib_Primitive(GraphicPrimitive):
     28    """
     29    Primitive class that wraps a Matplotlib artist
     30    """
     31    def __init__(self, axes, options=None):
     32        """
     33        sage: from matplotlib import axes
     34        sage: g = hist(randn(1000), 100)
     35        sage: gg=Matplotlib_Primitive(g)
     36        sage: type(gg)
     37        """
     38        if isinstance(axes, Figure):
     39            self.axes = axes.axes
     40        elif isinstance(axes, Axes):
     41            self.axes = [axes]
     42#        for ax in self.axes:
     43#            ax.figure=None
     44
     45        GraphicPrimitive.__init__(self, options)       
     46
     47    def get_minmax_data(self):
     48        """
     49        Returns a dictionary with the bounding box data.
     50               
     51        EXAMPLES:
     52            sage: from pylab import imshow
     53            sage: A=random_matrix(RDF,100)
     54            sage: A.numpy()
     55            sage: B=A.numpy().astype(float)
     56            sage: im = imshow(B/numpy.max(B),  origin='upper',alpha=0.6)
     57            sage: print Matplotlib_Primitive(im)
     58
     59            sage: m = matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))[0]
     60            sage: list(sorted(m.get_minmax_data().items()))
     61            [('xmax', 4), ('xmin', 0), ('ymax', 3), ('ymin', 0)]
     62
     63        """
     64        d = {}
     65        xlims = [axis.get_xbound() for axis in self.axes]
     66        ylims = [axis.get_ybound() for axis in self.axes]
     67        d['xmin'] = min(i[0] for i in xlims)
     68        d['xmax'] = max(i[1] for i in xlims)
     69        d['ymin'] = min(i[0] for i in ylims)
     70        d['ymax'] = max(i[1] for i in ylims)
     71        return d
     72
     73
     74    def _allowed_options(self):
     75        """
     76        No options are allowed.
     77
     78        EXAMPLE
     79            sage: from pylab import randn, hist
     80            sage: g = hist(randn(1000), 100)
     81            sage: gg = Matplotlib_Primitive(g)
     82            sage: gg._allowed_options()
     83
     84        """
     85        return {}
     86
     87    def _repr_(self):
     88        """
     89       
     90        """
     91        return "MatplotlibPlotPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col)
     92
     93    def _render_on_subplot(self, subplot):
     94        """
     95        Draw a matplotlib artist onto another artist
     96        """
     97        print type(subplot.get_figure())
     98        print type(subplot)
     99        print subplot.get_figure()
     100        for ax in self.axes:
     101            print ax
     102            print ax.get_figure(), ax.get_figure() is subplot.get_figure()
     103        for i,ax in enumerate(self.axes):
     104            ax.set_figure(subplot.get_figure())
     105            subplot.get_figure().add_axes(ax, label=str(i))
     106
     107def matplotlib_plot(mat):
     108    """
     109    Wraps a matplotlib artist in a Sage Graphics object.
     110
     111    EXAMPLE
     112        sage: from pylab import randn, hist
     113        sage: g = hist(randn(1000), 100)
     114        sage: matplotlib_plot(g)
     115    """
     116    g = Graphics()
     117    g.add_primitive(Matplotlib_Primitive(mat))
     118    return g
  • sage/plot/plot.py

    diff -r 3b1e17c09ce5 -r f61bcbafa0a8 sage/plot/plot.py
    a b  
    5757
    5858-  :func:`graphics_array`
    5959
     60-  :func:`matplotlib_plot` <sage.plot.matplotlib_plot.matplotlib_plot>
     61
    6062
    6163The following miscellaneous Graphics functions are included:
    6264