Matrix Plots

class sage.plot.matrix_plot.MatrixPlot(xy_data_array, xrange, yrange, options)

Bases: sage.plot.primitive.GraphicPrimitive

Primitive class for the matrix plot graphics type. See matrix_plot? for help actually doing matrix plots.

INPUT:

  • xy_data_array - list of lists giving matrix values corresponding to the grid
  • xrange - tuple of 2 floats indicating range for horizontal direction (number of columns in the matrix)
  • yrange - tuple of 2 floats indicating range for vertical direction (number of rows in the matrix)
  • options - dict of valid plot options to pass to constructor

EXAMPLES:

Note this should normally be used indirectly via matrix_plot:

sage: from sage.plot.matrix_plot import MatrixPlot
sage: M = MatrixPlot([[1,3],[2,4]],(1,2),(2,3),options={'cmap':'winter'})
sage: M
MatrixPlot defined by a 2 x 2 data grid
sage: M.yrange
(2, 3)
sage: M.xy_data_array
[[1, 3], [2, 4]]
sage: M.options()
{'cmap': 'winter'}

Extra options will get passed on to show(), as long as they are valid:

sage: matrix_plot([[1, 0], [0, 1]], fontsize=10)
sage: matrix_plot([[1, 0], [0, 1]]).show(fontsize=10) # These are equivalent

Extra options will get passed on to show(), as long as they are valid:

sage: matrix_plot([[1, 0], [0, 1]], fontsize=10)
sage: matrix_plot([[1, 0], [0, 1]]).show(fontsize=10) # These are equivalent

TESTS:

We test creating a matrix plot:

sage: M = matrix_plot([[mod(i,5)^j for i in range(5)] for j in range(1,6)])
get_minmax_data()

Returns a dictionary with the bounding box data.

EXAMPLES:

sage: m = matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))[0]
sage: list(sorted(m.get_minmax_data().items()))
[('xmax', 4), ('xmin', 0), ('ymax', 3), ('ymin', 0)]
sage.plot.matrix_plot.matrix_plot(*args, **kwds)

A plot of a given matrix or 2D array.

If the matrix is dense, each matrix element is given a different color value depending on its relative size compared to the other elements in the matrix. If the matrix is sparse, colors only indicate whether an element is nonzero or zero, so the plot represents the sparsity pattern of the matrix.

The tick marks drawn on the frame axes denote the row numbers (vertical ticks) and the column numbers (horizontal ticks) of the matrix.

INPUT:

  • mat - a 2D matrix or array

The following input must all be passed in as named parameters, if default not used:

  • cmap - a colormap (default: ‘gray’), the name of a predefined colormap, a list of colors, or an instance of a matplotlib Colormap. Type: import matplotlib.cm; matplotlib.cm.datad.keys() for available colormap names.

EXAMPLES:

A matrix over \ZZ colored with different grey levels:

sage: matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))

Here we make a random matrix over \RR and use cmap='hsv' to color the matrix elements different RGB colors:

sage: matrix_plot(random_matrix(RDF, 50), cmap='hsv')

Another random plot, but over \GF{389}:

sage: m = random_matrix(GF(389), 10)
sage: matrix_plot(m, cmap='Oranges')

It also works if you lift it to the polynomial ring:

sage: matrix_plot(m.change_ring(GF(389)['x']), cmap='Oranges')

Here we plot a random sparse matrix:

sage: sparse = matrix(dict([((randint(0, 10), randint(0, 10)), 1) for i in xrange(100)]))
sage: matrix_plot(sparse)
sage: A=random_matrix(ZZ,100000,density=.00001,sparse=True)
sage: matrix_plot(A,marker=',')

Plotting lists of lists also works:

sage: matrix_plot([[1,3,5,1],[2,4,5,6],[1,3,5,7]])

As does plotting of NumPy arrays:

sage: import numpy
sage: matrix_plot(numpy.random.rand(10, 10))

TESTS:

sage: P.<t> = RR[]
sage: matrix_plot(random_matrix(P, 3, 3))
...
TypeError: cannot coerce nonconstant polynomial to float
sage: matrix_plot([1,2,3])
...
TypeError: mat must be a Matrix or a two dimensional array
sage: matrix_plot([[sin(x), cos(x)], [1, 0]])
...
ValueError: can not convert array entries to floating point numbers

Previous topic

Step function plots

Next topic

Plotting fields

This Page