pltutils

Collection of utility functions for matplotlib

Functions

_is_jupyter_notebook()

_is_notebook_inline_backend()

_make_style()

_reset_notebook_inline_backend()

add_margin([margin, axis, ax])

Adjust the axis length to include a margin (after autoscale)

align(x, y)

Return text alignment based on (x, y) numbers

annotate_box(s, xy[, fontcolor, ax])

Annotate with a box around the text

axes(ax)

A context manager that sets the active Axes instance to ax

backend(new_backend)

Change the backend within this context

blend_colors(color, bg, factor)

Blend color with background

cm2inch(*values)

Convert from centimeter to inch

colorbar([mappable, ax, cax, label, powerlimits])

Custom colorbar with modified style and optional label

despine([trim, ax])

Remove the top and right spines

despine_all([ax])

Remove all spines, axes labels and ticks

direct_cmap_norm(data, colors[, blend])

Colormap with direct mapping: data[i] -> colors[i]

get_palette([name, num_colors, start])

Get a color palette from matplotlib's colormap database

legend(*args[, reverse, facecolor, lw, ax])

Custom legend with modified style and option to reverse label order

plot_color(x, y, z[, ax, cmap])

Function to plot the values as changing colors in a plot.

plot_vectors(vectors[, position, name, ...])

Visualize vectors, alternative to plt.quiver()

respine([ax])

Redraw all spines, opposite of despine()

set_min_axis_length(length[, axis, ax])

Set minimum axis length

set_min_axis_ratio(ratio[, ax])

Set minimum ratio between axes limits

set_palette([name, num_colors, start, ax])

Set the active color palette

use_style([style])

Shortcut for matplotlib.style.use() with pybinding style applied by default

cm2inch(*values) Tuple[int, int]

Convert from centimeter to inch

Parameters:
*values
Returns:
tuple

Examples

>>> cm2inch(2.54, 5.08)
(1.0, 2.0)
colorbar(mappable: ScalarMappable | None = None, ax: Axes | None = None, cax: Axes | None = None, label='', powerlimits=(0, 0), **kwargs)

Custom colorbar with modified style and optional label

Changes default pad and aspect argument values and turns on rasterization for a nicer looking colorbar with smaller size in vector formats (pdf, svg).

Parameters:
mappableOptional[ScalarMappable]

The image or contour set over which the colorbar will be drawn.

axOptional[plt.Axes]

Axis to add the colorbar to.

caxOptional[plt.Axes]

Axis to draw the colorbar in.

labelstr

Color data label.

powerlimitsTuple[int, int]

Sets size thresholds for scientific notation.

**kwargs

Forwarded to matplotlib.pyplot.colorbar().

despine(trim: bool = False, ax: Axes | None = None) None

Remove the top and right spines

Parameters:
trimbool

Trim spines so that they don’t extend beyond the last major ticks.

axOptional[plt.Axes]

Axes to despine.

despine_all(ax: Axes | None = None) None

Remove all spines, axes labels and ticks

Parameters:
axOptional[plt.Axes]

Axes to despine.

get_palette(name=None, num_colors=8, start=0)

Get a color palette from matplotlib’s colormap database

Parameters:
namestr, optional

Name of the palette to get. If None, get the active palette.

num_colorsint

Number of colors to retrieve.

startint

Staring from this color number.

Returns:
List[color]
legend(*args, reverse=False, facecolor='0.98', lw=0, ax: Axes | None = None, **kwargs)

Custom legend with modified style and option to reverse label order

Parameters:
reversebool

Reverse the label order.

facecolorcolor

Legend background color.

lwfloat

Frame width.

axOptional[plt.Axes]

Axis to add the legend to.

*args, **kwargs

Forwarded to matplotlib.pyplot.legend().

respine(ax: Axes | None = None) None

Redraw all spines, opposite of despine()

Parameters:
axOptional[plt.Axes]

Axes to respine.

set_palette(name=None, num_colors=8, start=0, ax: Axes | None = None)

Set the active color palette

Parameters:
namestr, optional

Name of the palette. If None, modify the active palette.

num_colorsint

Number of colors to retrieve.

startint

Staring from this color number.

axOptional[plt.Axes]

Axis to set the palette to.

use_style(style=pb_style)

Shortcut for matplotlib.style.use() with pybinding style applied by default

Parameters:
styledict

A matplotlib style specification.