From c64c79505ac5cff165f805f3df87a0695910655a Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 16 Feb 2023 21:42:54 -0800 Subject: [PATCH 1/3] src/sage/matrix/operation_table.py: Move plot imports into method --- src/sage/matrix/operation_table.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/sage/matrix/operation_table.py b/src/sage/matrix/operation_table.py index 9b59fa8dcee..08260e44ec6 100644 --- a/src/sage/matrix/operation_table.py +++ b/src/sage/matrix/operation_table.py @@ -14,12 +14,10 @@ # https://www.gnu.org/licenses/ # **************************************************************************** +from copy import copy + from sage.structure.sage_object import SageObject -from matplotlib.cm import gist_rainbow, Greys -from sage.plot.matrix_plot import matrix_plot from sage.matrix.constructor import Matrix -from sage.plot.text import text -from copy import copy class OperationTable(SageObject): @@ -985,6 +983,9 @@ def color_table(self, element_names=True, cmap=gist_rainbow_copy, **options): OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) sphinx_plot(OTa.color_table(), figsize=(3.0,3.0)) """ + from matplotlib.cm import gist_rainbow, Greys + from sage.plot.matrix_plot import matrix_plot + from sage.plot.text import text # Base matrix plot object, without text plot = matrix_plot(Matrix(self._table), cmap=cmap, From 30858af9c5e3b16a1180fd70b24d990d03a62379 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Fri, 17 Feb 2023 11:07:51 -0800 Subject: [PATCH 2/3] src/sage/matrix/operation_table.py: Fix more modularization violations, add/update docstrings --- src/sage/matrix/operation_table.py | 51 +++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/sage/matrix/operation_table.py b/src/sage/matrix/operation_table.py index 08260e44ec6..a5ac1a03db5 100644 --- a/src/sage/matrix/operation_table.py +++ b/src/sage/matrix/operation_table.py @@ -948,45 +948,37 @@ def matrix_of_variables(self): for i in range(self._n) for j in range(self._n)] return MS(entries) - # documentation hack - # makes the cmap default argument look nice in the docs - # by copying the gist_rainbow object and overriding __repr__ - gist_rainbow_copy=copy(gist_rainbow) - class ReprOverrideLinearSegmentedColormap(gist_rainbow_copy.__class__): - def __repr__(self): - return "gist_rainbow" - gist_rainbow_copy.__class__=ReprOverrideLinearSegmentedColormap - - - def color_table(self, element_names=True, cmap=gist_rainbow_copy, **options): + def color_table(self, element_names=True, cmap=None, **options): r""" - Returns a graphic image as a square grid where entries are color coded. + Return a graphic image as a square grid where entries are color coded. INPUT: - ``element_names`` - (default : ``True``) Whether to display text with element names on the image - - ``cmap`` - (default : ``gist_rainbow``) colour map for plot, see matplotlib.cm + - ``cmap`` - (default: :obj:`matplotlib.cm.gist_rainbow`) color map for plot, see :mod:`matplotlib.cm` - - ``**options`` - passed on to matrix_plot call + - ``**options`` - passed on to :func:`~sage.plot.matrix_plot.matrix_plot` EXAMPLES:: sage: from sage.matrix.operation_table import OperationTable - sage: OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) - sage: OTa.color_table() + sage: OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) # optional - sage.plot, sage.groups + sage: OTa.color_table() # optional - sage.plot, sage.groups Graphics object consisting of 37 graphics primitives .. PLOT:: from sage.matrix.operation_table import OperationTable OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) - sphinx_plot(OTa.color_table(), figsize=(3.0,3.0)) + sphinx_plot(OTa.color_table(), figsize=(3.0, 3.0)) """ - from matplotlib.cm import gist_rainbow, Greys from sage.plot.matrix_plot import matrix_plot from sage.plot.text import text + if cmap is None: + from matplotlib.cm import gist_rainbow as cmap + # Base matrix plot object, without text plot = matrix_plot(Matrix(self._table), cmap=cmap, frame=False, **options) @@ -1019,6 +1011,29 @@ def color_table(self, element_names=True, cmap=gist_rainbow_copy, **options): return plot def gray_table(self, **options): + r""" + Return a graphic image as a square grid where entries are displayed in grayscale. + + INPUT: + + - ``element_names`` - (default: ``True``) Whether to display text with element names on the image + + - ``**options`` - passed on to :func:`~sage.plot.matrix_plot.matrix_plot` + + EXAMPLES:: + + sage: from sage.matrix.operation_table import OperationTable + sage: OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) # optional - sage.plot, sage.groups + sage: OTa.gray_table() # optional - sage.plot, sage.groups + Graphics object consisting of 37 graphics primitives + + .. PLOT:: + + from sage.matrix.operation_table import OperationTable + OTa = OperationTable(SymmetricGroup(3), operation=operator.mul) + sphinx_plot(OTa.gray_table(), figsize=(3.0, 3.0)) + """ + from matplotlib.cm import Greys return self.color_table(cmap=Greys, **options) def _ascii_table(self): From fb4f6abff964b5c9f72d096ecb91b06b21916770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6ppe?= Date: Fri, 17 Feb 2023 23:22:43 -0800 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Travis Scrimshaw --- src/sage/matrix/operation_table.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sage/matrix/operation_table.py b/src/sage/matrix/operation_table.py index a5ac1a03db5..79acaaf8390 100644 --- a/src/sage/matrix/operation_table.py +++ b/src/sage/matrix/operation_table.py @@ -956,9 +956,9 @@ def color_table(self, element_names=True, cmap=None, **options): - ``element_names`` - (default : ``True``) Whether to display text with element names on the image - - ``cmap`` - (default: :obj:`matplotlib.cm.gist_rainbow`) color map for plot, see :mod:`matplotlib.cm` + - ``cmap`` -- (default: :obj:`matplotlib.cm.gist_rainbow`) color map for plot, see :mod:`matplotlib.cm` - - ``**options`` - passed on to :func:`~sage.plot.matrix_plot.matrix_plot` + - ``**options`` -- passed on to :func:`~sage.plot.matrix_plot.matrix_plot` EXAMPLES:: @@ -1016,9 +1016,9 @@ def gray_table(self, **options): INPUT: - - ``element_names`` - (default: ``True``) Whether to display text with element names on the image + - ``element_names`` -- (default: ``True``) whether to display text with element names on the image - - ``**options`` - passed on to :func:`~sage.plot.matrix_plot.matrix_plot` + - ``**options`` -- passed on to :func:`~sage.plot.matrix_plot.matrix_plot` EXAMPLES::