Skip to content

Matrix plot#

The class MatshowPlot in puma.matshow is designed to plot matrixes based on matplotlib's matshow.

Basic plot#

The class can plot a matrix stored in a np.ndarray:

matrix_plotter = MatshowPlot()
mat = np.random.rand(4, 3)
matrix_plotter.draw(mat)
matrix_plotter.savefig("path/to/save_dir/vanilla_mat.png")

Plot customization#

Various aspects of the plot appearance can be customized using the class' arguments: - x_ticklabels: Names of the matrix's columns; - x_ticks_rotation: Rotation of the columns' names with respect to the horizontal direction; - y_ticklabels: Names of the matrix's rows; - show_entries: wether to show or not the matrix entries as text over the matrix's pixels (bins); - show_percentage: If True, the entries are formatted as percentages (i.e. numbers in [0,1] are multiplied by 100 and the percentage symbol is appended). - text_color_threshold: threshold on the relative luminance of the background color (i.e. the color of the matrix pixel) after which the overlapped text color switches to black, to allow better readability on lighter background colors. By default is set to 0.408, as per W3C standards; - colormap: pyplot.cm colormap for the plot; - cbar_label: Label of the colorbar;

Example#

Example without any customization:

Example with some customization:

Code to obtain previous examples:

from __future__ import annotations

import numpy as np
from matplotlib import pyplot as plt

from puma.matshow import MatshowPlot

# seeded PRNG for reproducibility
prng = np.random.default_rng(seed=0)

# A random matrix
mat = prng.random(size=(4, 3))

# Declaring the plot class
matrix_plotter = MatshowPlot()
matrix_plotter.draw(mat)
# Saving the plot
matrix_plotter.savefig("vanilla_mat.png")

# Some possible customizations
# Matrix's column names
x_ticks = ["a", "b", "c"]
# Matrix's rows names
y_ticks = ["d", "e", "f", "g"]

# Declaring the plot class with custom style
matrix_plotter_custom = MatshowPlot(
    x_ticklabels=x_ticks,
    x_ticks_rotation=45,
    y_ticklabels=y_ticks,
    show_entries=True,
    show_percentage=True,
    text_color_threshold=0.6,
    colormap=plt.cm.PiYG,
    cbar_label="Scalar values as percentages",
    atlas_tag_outside=True,
    fontsize=15,
)
matrix_plotter_custom.draw(mat)
# Saving the plot
matrix_plotter_custom.savefig("mat_custumized.png")