Ticket #10847: trac-10847-matrix_plot-subdivisions.patch

File trac-10847-matrix_plot-subdivisions.patch, 5.6 KB (added by jason, 9 years ago)
  • sage/plot/matrix_plot.py

    # HG changeset patch
    # User Jason Grout <jason.grout@drake.edu>
    # Date 1298572637 21600
    # Node ID b36b2cd0d322cd5c5b4f6642857158e899d310df
    # Parent  8438b7c20d79c02a2ece3e1c3f7224a772ff8f07
    #10847: matrix_plot learns how to plot subdivisions of a matrix
    
    diff -r 8438b7c20d79 -r b36b2cd0d322 sage/plot/matrix_plot.py
    a b  
    1919#                  http://www.gnu.org/licenses/
    2020#*****************************************************************************
    2121from sage.plot.primitive import GraphicPrimitive
    22 from sage.misc.decorators import options, rename_keyword
     22from sage.misc.decorators import options, suboptions, rename_keyword
    2323from sage.plot.colors import to_mpl_color, get_cmap
    2424
    2525class MatrixPlot(GraphicPrimitive):
     
    135135                'norm': "The normalization function",
    136136                'vmin': "The minimum value",
    137137                'vmax': "The maximum value",
    138                 'origin': "If 'lower', draw the matrix with the first row on the bottom of the graph"}
     138                'origin': "If 'lower', draw the matrix with the first row on the bottom of the graph",
     139                'subdivisions': "If True, draw subdivisions of the matrix",
     140                'subdivision_options': "Options (boundaries and style) of the subdivisions"}
    139141
    140142    def _repr_(self):
    141143        """
     
    165167            import matplotlib
    166168            norm=matplotlib.colors.NoNorm()
    167169           
     170        if options['subdivisions']:
     171            subdiv_options=options['subdivision_options']
     172            if isinstance(subdiv_options['boundaries'], (list, tuple)):
     173                rowsub,colsub=subdiv_options['boundaries']
     174            else:
     175                rowsub=subdiv_options['boundaries']
     176                colsub=subdiv_options['boundaries']
     177            if isinstance(subdiv_options['style'], (list, tuple)):
     178                rowstyle,colstyle=subdiv_options['style']
     179            else:
     180                rowstyle=subdiv_options['style']
     181                colstyle=subdiv_options['style']
     182            if rowstyle is None:
     183                rowstyle=dict()
     184            if colstyle is None:
     185                colstyle=dict()
     186               
     187            # Make line objects for subdivisions
     188            from line import line2d
     189            lim=self.get_minmax_data()
     190            # First draw horizontal lines representing row subdivisions
     191            for y in rowsub:
     192                l=line2d([(lim['xmin'],y-0.5), (lim['xmax'],y-0.5)], **rowstyle)[0]
     193                l._render_on_subplot(subplot)
     194            for x in colsub:
     195                l=line2d([(x-0.5, lim['ymin']), (x-0.5, lim['ymax'])], **colstyle)[0]
     196                l._render_on_subplot(subplot)
     197           
    168198        if hasattr(self.xy_data_array, 'tocoo'):
    169199            # Sparse matrix -- use spy
    170200            opts=options.copy()
     
    184214        elif origin=='lower':
    185215            subplot.xaxis.tick_bottom()
    186216
     217@suboptions('subdivision',boundaries=None, style=None)
    187218@options(cmap='gray',marker='.',frame=True, axes=False, norm=None,
    188          vmin=None, vmax=None, origin='upper',ticks_integer=True)
     219         vmin=None, vmax=None, origin='upper',ticks_integer=True,
     220         subdivisions=False)
    189221def matrix_plot(mat, **options):
    190222    r"""
    191223    A plot of a given matrix or 2D array.
     
    226258      is on the top of the graph.  If 'lower', the first row is on the
    227259      bottom of the graph.
    228260
     261    - ``subdivisions`` - If True, plot the subdivisions of the matrix as lines.
     262
     263    - ``subdivision_boundaries`` - a list of lists in the form ``[row_subdivisions, column_subdivisions]``, which specifies the row and column subdivisions to use.  If not specified, defaults to the matrix subdivisions
     264   
     265    - ``subdivision_style`` - a dictionary of properties passed on to the :func:`~sage.plot.line.line2d` command for plotting subdivisions.  If this is a two-element list or tuple, then it specifies the styles of row and column divisions, respectively.
     266
    229267    EXAMPLES:
    230268   
    231269    A matrix over `\ZZ` colored with different grey levels::
     
    258296   
    259297        sage: matrix_plot(random_matrix(ZZ,10)*.05, norm='value')
    260298
     299    Matrix subdivisions can be plotted as well::
     300   
     301        sage: m=random_matrix(RR,10)
     302        sage: m.subdivide([2,4],[6,8])
     303        sage: matrix_plot(m, subdivisions=True, subdivision_style=dict(color='red',thickness=3))
     304   
     305    You can also specify your own subdivisions and separate styles for row or column subdivisions::
     306   
     307        sage: m=random_matrix(RR,10)
     308        sage: matrix_plot(m, subdivisions=True, subdivision_boundaries=[[2,4],[6,8]], subdivision_style=[dict(color='red',thickness=3),dict(linestyle='--',thickness=6)])
     309
    261310    Generally matrices are plotted with the (0,0) entry in the upper
    262311    left.  However, sometimes if we are plotting an image, we'd like
    263312    the (0,0) entry to be in the lower left.  We can do that with the
     
    341390    from sage.plot.plot import Graphics
    342391    from sage.matrix.all import is_Matrix
    343392    from sage.rings.all import RDF
     393    orig_mat=mat
    344394    if is_Matrix(mat):
    345395        sparse = mat.is_sparse()
    346396        if sparse:
     
    376426    xrange = (0, xy_data_array.shape[1])
    377427    yrange = (0, xy_data_array.shape[0])
    378428
     429    if options['subdivisions'] and options['subdivision_options']['boundaries'] is None:
     430        options['subdivision_options']['boundaries']=orig_mat.subdivisions
     431
    379432    g = Graphics()
    380433    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
    381434    g.add_primitive(MatrixPlot(xy_data_array, xrange, yrange, options))