2.5.2. matplotlib

The scisalt.matplotlib module adds many useful wrappers for matplotlib classes and functions. In particular, it adds the color map viridis as described in a presentation on YouTube, which will become the new default in matplotlib.

class scisalt.matplotlib.Imshow_Slider(image, usecbar=False, **kwargs)[source]

Changed in version 1.2: Name changed, colorbar options added, p changed to AxesImage.

Convenience class for viewing images.

Plots image to a to an instance of matplotlib.axis.imshow(**kwargs), with sliders for controlling bounds, with **kwargs passed through to matplotlib.axes.Axes.imshow().

usecbar determines if a colorbar will be used. Color bars can slow down the viewer significantly.

AxesImage

The matplotlib.image.AxesImage from matplotlib.axes.Axes.imshow().

ax

The matplotlib.axes.Axes used for matplotlib.axes.Axes.imshow().

clim_max

Slider value for maximum

clim_min

Slider value for minimum

image

The image loaded.

imgmax

Highest value of input image.

imgmin

Lowest value of input image.

set_cmap(cmap)[source]

Sets color map to cmap.

class scisalt.matplotlib.Imshow_Slider_Array(images, usecbar=False, **kwargs)[source]

Changed in version 1.5: Name changed, colorbar options added, p changed to AxesImage.

Convenience class for viewing images.

Plots image to a to an instance of matplotlib.axis.imshow(**kwargs), with sliders for controlling bounds, with **kwargs passed through to matplotlib.axes.Axes.imshow().

usecbar determines if a colorbar will be used. Color bars can slow down the viewer significantly.

AxesImage

The matplotlib.image.AxesImage from matplotlib.axes.Axes.imshow().

ax

The matplotlib.axes.Axes used for matplotlib.axes.Axes.imshow().

clim_max

Slider value for maximum

clim_min

Slider value for minimum

image

The image loaded.

images

The array of images.

imgmax

Highest value of input image.

imgmin

Lowest value of input image.

set_cmap(cmap)[source]

Sets color map to cmap.

scisalt.matplotlib.NonUniformImage(x, y, z, ax=None, fig=None, cmap=None, alpha=None, scalex=True, scaley=True, add_cbar=True, **kwargs)[source]

Plots a set of coordinates where:

  • x and y are 1-D ndarrays of lengths N and M, respectively, specifying pixel centers
  • z is an (M, N) ndarray or masked array of values to be colormapped, or a (M, N, 3) RGB array, or a (M, N, 4) RGBA array.

**kwargs can contain keywords:

  • cmap for set the colormap
  • alpha to set transparency
  • scalex to set the x limits to available data
  • scaley to set the y limits to available data

Returns class matplotlib.image.NonUniformImage.

scisalt.matplotlib.NonUniformImage_axes(img)[source]

Returns axes x, y for a given image img to be used with scisalt.matplotlib.NonUniformImage().

class scisalt.matplotlib.RectangleSelector(ax, *args, selfunc=None, parent=None, verbose=False, **kwargs)[source]

New in version 1.2.

Add rectangle selection to an already-existing axis ax. *args and **kwargs pass through to matplotlib.widgets.RectangleSelector.

Use key A or a to toggle whether the rectangle is active or not.

verbose controls whether selections are printed to the terminal.

RectangleSelector

The instance of matplotlib.widgets.RectangleSelector.

ax

The axis used.

eclick

The starting mouse click from RectangleSelector.

erelease

The ending mouse click from RectangleSelector.

height

Height of rectangle.

selfunc

A placeholder for the function called on each mouse release.

selfunc_results

The results of selfunc(instance) where instance is this class.

verbose

Determines whether rectangle coordinates are printed to the terminal on selection.

width

Width of rectangle.

x0

Minimum x coordinate of rectangle.

x1

Maximum x coordinate of rectangle.

y0

Minimum y coordinate of rectangle.

y1

Maximum y coordinate of rectangle.

scisalt.matplotlib.addlabel(ax=None, toplabel=None, xlabel=None, ylabel=None, zlabel=None, clabel=None, cb=None, windowlabel=None, fig=None, axes=None)[source]

Adds labels to a plot.

scisalt.matplotlib.axesfontsize(ax, fontsize)[source]

Change the font size for the title, x and y labels, and x and y tick labels for axis ax to fontsize.

scisalt.matplotlib.colorbar(ax, im, fig=None, loc='right', size='5%', pad='3%')[source]

New in version 1.3.

Adds a polite colorbar that steals space so matplotlib.pyplot.tight_layout() works nicely.

scisalt.matplotlib.contour(X, ax=None, add_cbar=True, rescale_fig=True, **kwargs)[source]

New in version 1.3.

Plots an array X such that the first coordinate is the x coordinate and the second coordinate is the y coordinate, with the origin at the bottom left corner.

Optional argument ax allows an existing axes to be used.

**kwargs are passed on to matplotlib.axes.Axes.contour().

Returns matplotlib.image.AxesImage.

scisalt.matplotlib.figure(title=None, **kwargs)[source]

Creates a figure with **kwargs with a window title title.

Returns class matplotlib.figure.Figure.

scisalt.matplotlib.hist(x, bins=10, labels=None, aspect='auto', plot=True, ax=None, range=None)[source]

Creates a histogram of data x with a bins, labels = [title, xlabel, ylabel].

scisalt.matplotlib.hist2d(x, y, bins=10, labels=None, aspect='auto', plot=True, fig=None, ax=None, interpolation='none', **kwargs)[source]

Creates a 2-D histogram of data x, y with bins, labels = [title, xlabel, ylabel], aspect ration aspect. Attempts to use axis ax first, then the current axis of fig, then the last axis, to use an already-created window.

Plotting (plot) is on by default, setting false doesn’t attempt to create a figure.

interpolation sets the interpolation type of matplotlib.axis.imshow().

Returns a handle and extent as h, extent

scisalt.matplotlib.imshow(X, ax=None, add_cbar=True, rescale_fig=True, **kwargs)[source]

New in version 1.3.

Plots an array X such that the first coordinate is the x coordinate and the second coordinate is the y coordinate, with the origin at the bottom left corner.

Optional argument ax allows an existing axes to be used.

**kwargs are passed on to matplotlib.axes.Axes.imshow().

  • Returns fig, ax, im if axes aren’t specified.
  • Returns im if axes are specified.
scisalt.matplotlib.imshow_batch(images, cbar=True, show=True, pdf=None, figsize=(16, 12), rows=2, columns=2, cmap=None, **kwargs)[source]

Plots an array of images to a single window of size figsize with rows and columns.

  • cmap: Specifies color map
  • cbar: Add color bars
  • show: If false, dismisses each window after it is created and optionally saved
  • pdf: Save to a pdf of filename pdf
  • **kwargs passed to matplotlib.axis.imshow
scisalt.matplotlib.less_labels(ax, x_fraction=0.5, y_fraction=0.5)[source]

Scale the number of tick labels in x and y by x_fraction and y_fraction respectively.

scisalt.matplotlib.pcolor_axes(array, px_to_units=<function px_to_units>)[source]

Return axes x, y for array to be used with matplotlib.pyplot.color().

px_to_units is a function to convert pixels to units. By default, returns pixels.

scisalt.matplotlib.plot(*args, ax=None, **kwargs)[source]

New in version 1.4.

Plots but automatically resizes x axis.

Wrapper for matplotlib.pyplot.plot() / errorbar().

Takes options:

  • ‘error’: if true, use matplotlib.pyplot.errorbar() instead of matplotlib.pyplot.plot(). *args and **kwargs passed through here.
  • ‘fig’: figure to use.
  • ‘figlabel’: figure label.
  • ‘legend’: legend location.
  • ‘toplabel’: top label of plot.
  • ‘xlabel’: x-label of plot.
  • ‘ylabel’: y-label of plot.
scisalt.matplotlib.quiver(*args, ax=None, rescale_fig=True, **kwargs)[source]

New in version 1.3.

Plots an array X such that the first coordinate is the x coordinate and the second coordinate is the y coordinate, with the origin at the bottom left corner.

Optional argument ax allows an existing axes to be used.

**kwargs are passed on to matplotlib.axes.Axes.quiver().

Returns matplotlib.image.AxesImage.

scisalt.matplotlib.rgb2gray(image)[source]

Convert an rgb image to grayscale.

scisalt.matplotlib.savefig(filename, path='figs', fig=None, ext='eps', verbose=False, **kwargs)[source]

Save the figure fig (optional, if not specified, latest figure in focus) to filename in the path path with extension ext.

**kwargs is passed to matplotlib.figure.Figure.savefig().

scisalt.matplotlib.scaled_figsize(X, figsize=None, h_pad=None, v_pad=None)[source]

New in version 1.3.

Given an array X, determine a good size for the figure to be by shrinking it to fit within figsize. If not specified, shrinks to fit the figsize specified by the current matplotlib.rcParams.

scisalt.matplotlib.setup_axes(rows=1, cols=1, figsize=(8, 6), expand=True, **kwargs)[source]

New in version 1.2.

Sets up a figure of size figsize with a number of rows (rows) and columns (cols). **kwargs passed through to matplotlib.figure.Figure.add_subplot().

Returns fig, axes:

  • fig: The figure
  • axes: An array of all of the axes. (Unless there’s only one axis, in which case it returns an object instance matplotlib.axis.Axis.)
scisalt.matplotlib.setup_figure(rows=1, cols=1, **kwargs)[source]

Changed in version 1.3: Supports **kwargs pass-through to matplotlib.figure.Figure.

Changed in version 1.2: Changed gridspec_x to rows, gridspec_y to cols, added figsize control.

Sets up a figure with a number of rows (rows) and columns (cols), **kwargs passes through to matplotlib.figure.Figure.

Returns fig, gs:

scisalt.matplotlib.showfig(fig, aspect='auto')[source]

Shows a figure with a typical orientation so that x and y axes are set up as expected.

scisalt.matplotlib.tile()

Tile open figures. Finnicky on various OS’s, so not imported by default.