Ticket #10847: trac10847matrix_plotsubdivisions.patch
File trac10847matrix_plotsubdivisions.patch, 5.6 KB (added by , 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 19 19 # http://www.gnu.org/licenses/ 20 20 #***************************************************************************** 21 21 from sage.plot.primitive import GraphicPrimitive 22 from sage.misc.decorators import options, rename_keyword22 from sage.misc.decorators import options, suboptions, rename_keyword 23 23 from sage.plot.colors import to_mpl_color, get_cmap 24 24 25 25 class MatrixPlot(GraphicPrimitive): … … 135 135 'norm': "The normalization function", 136 136 'vmin': "The minimum value", 137 137 '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"} 139 141 140 142 def _repr_(self): 141 143 """ … … 165 167 import matplotlib 166 168 norm=matplotlib.colors.NoNorm() 167 169 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'],y0.5), (lim['xmax'],y0.5)], **rowstyle)[0] 193 l._render_on_subplot(subplot) 194 for x in colsub: 195 l=line2d([(x0.5, lim['ymin']), (x0.5, lim['ymax'])], **colstyle)[0] 196 l._render_on_subplot(subplot) 197 168 198 if hasattr(self.xy_data_array, 'tocoo'): 169 199 # Sparse matrix  use spy 170 200 opts=options.copy() … … 184 214 elif origin=='lower': 185 215 subplot.xaxis.tick_bottom() 186 216 217 @suboptions('subdivision',boundaries=None, style=None) 187 218 @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) 189 221 def matrix_plot(mat, **options): 190 222 r""" 191 223 A plot of a given matrix or 2D array. … … 226 258 is on the top of the graph. If 'lower', the first row is on the 227 259 bottom of the graph. 228 260 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 twoelement list or tuple, then it specifies the styles of row and column divisions, respectively. 266 229 267 EXAMPLES: 230 268 231 269 A matrix over `\ZZ` colored with different grey levels:: … … 258 296 259 297 sage: matrix_plot(random_matrix(ZZ,10)*.05, norm='value') 260 298 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 261 310 Generally matrices are plotted with the (0,0) entry in the upper 262 311 left. However, sometimes if we are plotting an image, we'd like 263 312 the (0,0) entry to be in the lower left. We can do that with the … … 341 390 from sage.plot.plot import Graphics 342 391 from sage.matrix.all import is_Matrix 343 392 from sage.rings.all import RDF 393 orig_mat=mat 344 394 if is_Matrix(mat): 345 395 sparse = mat.is_sparse() 346 396 if sparse: … … 376 426 xrange = (0, xy_data_array.shape[1]) 377 427 yrange = (0, xy_data_array.shape[0]) 378 428 429 if options['subdivisions'] and options['subdivision_options']['boundaries'] is None: 430 options['subdivision_options']['boundaries']=orig_mat.subdivisions 431 379 432 g = Graphics() 380 433 g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 381 434 g.add_primitive(MatrixPlot(xy_data_array, xrange, yrange, options))