pytools.viz.matrix.MatrixMatplotStyle#

class pytools.viz.matrix.MatrixMatplotStyle(*, ax=None, colors=None, font_family=None, colormap_normalize=None, colorbar_major_formatter=None, colorbar_minor_formatter=None, cell_format=None, nan_substitute=None)[source]#

Matplot style for matrices.

Numerical values of matrix cells are rendered as colors from a color map, with a color bar attached as a legend.

Matrix cells will be annotated with their values if a cell format is specified. Cell formats can be defined in different ways:

  • a new-style Python format string, e.g., {:.3g}

  • a function accepting the value to be formatted as its only positional argument, e.g., lambda x: f"{x * 100:.3g}%"

  • a Formatter instance

Bases

ColorbarMatplotStyle, MatrixStyle

Metaclasses

ABCMeta

Parameters
  • ax (Optional[Axes]) – optional axes object to draw on; create a new figure if not specified

  • colors (Optional[MatplotColorScheme]) – the color scheme to be used by this drawing style (default: FacetLightColorScheme)

  • font_family (Union[str, Iterable[str], None]) – name of one or more fonts to use for all text, in descending order of preference; defaults to a monospaced font if undefined, or if none of the given fonts is available

  • colormap_normalize (Optional[Normalize]) – the Normalize object that maps values to color indices; unless otherwise specified, use a plain Normalize object with linear autoscaling

  • colorbar_major_formatter (Optional[Formatter]) – major tick formatter for the color bar (optional)

  • colorbar_minor_formatter (Optional[Formatter]) – minor tick formatter for the color bar (optional; requires that also a major formatter is specified)

  • cell_format (Union[str, Formatter, Callable[Any, str], None]) – format for annotating each matrix cell with its value if sufficient space is available (optional – do not annotate cells if omitted); if no colorbar major formatter is specified, use the cell format also as the colorbar major formatter

  • nan_substitute (Optional[float]) – value to look up in the colormap for undefined matrix cells (default: 0.0)

Method summary

apply_color_scheme

Apply this style's color scheme to the given Axes.

color_for_value

Get the color(s) associated with the given value(s), based on the color map and normalization defined for this style.

draw_matrix

Draw the matrix.

finalize_drawing

Finalize the matrix plot.

get_default_style_name

Get the name of the default style associated with this style class.

get_named_styles

Get a mapping of names to default instances of this style class.

get_renderer

Get the renderer used by this style's Axes object (see ax).

start_drawing

Initialize the matrix plot.

Attribute summary

ax

The matplotlib Axes object on which to draw.

cell_formatter

Formatter for annotating each matrix cell with its value; if None, no cells are annotated.

colorbar

The colorbar associated with this style; set when ColorbarMatplotStyle.start_drawing() is called.

colors

The color scheme used by this style.

nan_substitute

The value to look up in the colormap for undefined matrix cells.

Definitions

apply_color_scheme(ax)#

Apply this style’s color scheme to the given Axes.

Style implementations can use this to apply the color scheme to sub-axes. Does not need to be applied to main axes, as this is already done in method start_drawing().

Parameters

ax (Axes) – the axes to apply the color scheme to

Return type

None

color_for_value(z)#

Get the color(s) associated with the given value(s), based on the color map and normalization defined for this style.

Parameters

z (Union[int, float, ndarray[Any, dtype[float64]]]) – the scalar to be color-encoded

Return type

Union[RgbaColor, ndarray[Any, dtype[float64]]]

Returns

the resulting color for a single value as an RGBA tuple, or an array of shape (n, 4) if called with n values

draw_matrix(data, *, names, weights)[source]#

Draw the matrix.

Parameters
Return type

None

finalize_drawing(name_labels=None, weight_label=None, **kwargs)#

Finalize the matrix plot.

Parameters
  • name_labels (Optional[tuple[Optional[str], Optional[str]]]) – the labels for the row and column axes

  • weight_label (Optional[str]) – the label for the weight axis

  • kwargs (Any) – additional drawer-specific arguments

Return type

None

classmethod get_default_style_name()#

Get the name of the default style associated with this style class.

The default style is obtained by instantiating this style class without parameters.

Common examples for default style names are matplot and text.

Return type

str

Returns

the name of the default style

classmethod get_named_styles()#

Get a mapping of names to default instances of this style class.

Return type

dict[str, Callable[..., MatrixMatplotStyle]]

Returns

a dictionary mapping of names to default instances of this style class

get_renderer()#

Get the renderer used by this style’s Axes object (see ax).

Return type

RendererBase

Returns

the renderer

start_drawing(*, title, name_labels=(None, None), weight_label=None, **kwargs)[source]#

Initialize the matrix plot.

Parameters
  • title (str) – the title of the matrix

  • name_labels (tuple[Optional[str], Optional[str]]) – the labels for the row and column axes

  • weight_label (Optional[str]) – the label for the weight axis

  • kwargs (Any) – additional drawer-specific arguments

Return type

None

property ax: matplotlib.axes.Axes#

The matplotlib Axes object on which to draw.

Return type

Axes

cell_formatter: Optional[matplotlib.ticker.Formatter] = None#

Formatter for annotating each matrix cell with its value; if None, no cells are annotated.

property colors: pytools.viz.T_ColorScheme#

The color scheme used by this style.

Return type

TypeVar(T_ColorScheme, bound= ColorScheme)

nan_substitute: float#

The value to look up in the colormap for undefined matrix cells.