sporco.plot

Plotting/visualisation functions

Functions

attach_keypress(fig[, scaling]) Attach a key press event handler that configures keys for closing a
attach_zoom(ax[, scaling]) Attach an event handler that supports zooming within a plot using the mouse scroll wheel.
plot(y[, x, ptyp, xlbl, ylbl, title, lgnd, lglc]) Plot points or lines in 2D.
surf(z[, x, y, elev, azim, xlbl, ylbl, …]) Plot a 2D surface in 3D.
contour(z[, x, y, v, xlbl, ylbl, title, …]) Contour plot of a 2D surface.
imview(img[, title, copy, fltscl, intrp, …]) Display an image.
close([fig]) Close figure(s).
set_ipython_plot_backend([backend]) Set matplotlib backend within an ipython shell.
set_notebook_plot_backend([backend]) Set matplotlib backend within a Jupyter Notebook shell.
config_notebook_plotting() Configure plotting functions for inline plotting within a Jupyter Notebook shell.

Function Descriptions

sporco.plot.attach_keypress(fig, scaling=1.1)[source]

Attach a key press event handler that configures keys for closing a figure and changing the figure size. Keys ‘e’ and ‘c’ respectively expand and contract the figure, and key ‘q’ closes it.

Note: Resizing may not function correctly with all matplotlib backends (a bug has been reported).

Parameters:
fig : matplotlib.figure.Figure object

Figure to which event handling is to be attached

scaling : float, optional (default 1.1)

Scaling factor for figure size changes

Returns:
press : function

Key press event handler function

sporco.plot.attach_zoom(ax, scaling=2.0)[source]

Attach an event handler that supports zooming within a plot using the mouse scroll wheel.

Parameters:
ax : matplotlib.axes.Axes object

Axes to which event handling is to be attached

scaling : float, optional (default 2.0)

Scaling factor for zooming in and out

Returns:
zoom : function

Mouse scroll wheel event handler function

sporco.plot.plot(y, x=None, ptyp='plot', xlbl=None, ylbl=None, title=None, lgnd=None, lglc=None, **kwargs)[source]

Plot points or lines in 2D. If a figure object is specified then the plot is drawn in that figure, and fig.show() is not called. The figure is closed on key entry ‘q’.

Parameters:
y : array_like

1d or 2d array of data to plot. If a 2d array, each column is plotted as a separate curve.

x : array_like, optional (default None)

Values for x-axis of the plot

ptyp : string, optional (default ‘plot’)

Plot type specification (options are ‘plot’, ‘semilogx’, ‘semilogy’, and ‘loglog’)

xlbl : string, optional (default None)

Label for x-axis

ylbl : string, optional (default None)

Label for y-axis

title : string, optional (default None)

Figure title

lgnd : list of strings, optional (default None)

List of legend string

lglc : string, optional (default None)

Legend location string

**kwargs : matplotlib.lines.Line2D properties or figure properties, optional

Keyword arguments specifying matplotlib.lines.Line2D properties, e.g. lw=2.0 sets a line width of 2, or properties of the figure and axes. If not specified, the defaults for line width (lw) and marker size (ms) are 1.5 and 6.0 respectively. The valid figure and axes keyword arguments are listed below:

kwarg Accepts Description
fgsz tuple (width,height) Specify figure dimensions in inches
fgnm integer Figure number of figure
fig matplotlib.figure.Figure object Draw in specified figure instead of creating one
ax matplotlib.axes.Axes object Plot in specified axes instead of current axes of figure
Returns:
fig : matplotlib.figure.Figure object

Figure object for this figure

ax : matplotlib.axes.Axes object

Axes object for this plot

sporco.plot.surf(z, x=None, y=None, elev=None, azim=None, xlbl=None, ylbl=None, zlbl=None, title=None, lblpad=8.0, cntr=None, cmap=None, fgsz=None, fgnm=None, fig=None, ax=None)[source]

Plot a 2D surface in 3D. If a figure object is specified then the surface is drawn in that figure, and fig.show() is not called. The figure is closed on key entry ‘q’.

Parameters:
z : array_like

2d array of data to plot

x : array_like, optional (default None)

Values for x-axis of the plot

y : array_like, optional (default None)

Values for y-axis of the plot

elev : float

Elevation angle (in degrees) in the z plane

azim : foat

Azimuth angle (in degrees) in the x,y plane

xlbl : string, optional (default None)

Label for x-axis

ylbl : string, optional (default None)

Label for y-axis

zlbl : string, optional (default None)

Label for z-axis

title : string, optional (default None)

Figure title

lblpad : float, optional (default 8.0)

Label padding

cntr : int or sequence of ints, optional (default None)

If not None, plot contours of the surface on the lower end of the z-axis. An int specifies the number of contours to plot, and a sequence specifies the specific contour levels to plot.

cmap : matplotlib.colors.Colormap object, optional (default None)

Colour map for surface. If none specifed, defaults to cm.coolwarm

fgsz : tuple (width,height), optional (default None)

Specify figure dimensions in inches

fgnm : integer, optional (default None)

Figure number of figure

fig : matplotlib.figure.Figure object, optional (default None)

Draw in specified figure instead of creating one

ax : matplotlib.axes.Axes object, optional (default None)

Plot in specified axes instead of creating one

Returns:
fig : matplotlib.figure.Figure object

Figure object for this figure

ax : matplotlib.axes.Axes object

Axes object for this plot

sporco.plot.contour(z, x=None, y=None, v=5, xlbl=None, ylbl=None, title=None, cfntsz=10, lfntsz=None, intrp='bicubic', alpha=0.5, cmap=None, vmin=None, vmax=None, fgsz=None, fgnm=None, fig=None, ax=None)[source]

Contour plot of a 2D surface. If a figure object is specified then the plot is drawn in that figure, and fig.show() is not called. The figure is closed on key entry ‘q’.

Parameters:
z : array_like

2d array of data to plot

x : array_like, optional (default None)

Values for x-axis of the plot

y : array_like, optional (default None)

Values for y-axis of the plot

v : int or sequence of ints, optional (default 5)

An int specifies the number of contours to plot, and a sequence specifies the specific contour levels to plot.

xlbl : string, optional (default None)

Label for x-axis

ylbl : string, optional (default None)

Label for y-axis

title : string, optional (default None)

Figure title

cfntsz : int or None, optional (default 10)

Contour label font size. No contour labels are displayed if set to 0 or None.

lfntsz : int, optional (default None)

Axis label font size. The default font size is used if set to None.

intrp : string, optional (default ‘bicubic’)

Specify type of interpolation used to display image underlying contours (see interpolation parameter of matplotlib.axes.Axes.imshow)

alpha : float, optional (default 0.5)

Underlying image display alpha value

cmap : matplotlib.colors.Colormap, optional (default None)

Colour map for surface. If none specifed, defaults to cm.coolwarm

vmin, vmax : float, optional (default None)

Set upper and lower bounds for the colour map (see the corresponding parameters of matplotlib.axes.Axes.imshow)

fgsz : tuple (width,height), optional (default None)

Specify figure dimensions in inches

fgnm : integer, optional (default None)

Figure number of figure

fig : matplotlib.figure.Figure object, optional (default None)

Draw in specified figure instead of creating one

ax : matplotlib.axes.Axes object, optional (default None)

Plot in specified axes instead of current axes of figure

Returns:
fig : matplotlib.figure.Figure object

Figure object for this figure

ax : matplotlib.axes.Axes object

Axes object for this plot

sporco.plot.imview(img, title=None, copy=True, fltscl=False, intrp='nearest', norm=None, cbar=False, cmap=None, fgsz=None, fgnm=None, fig=None, ax=None)[source]

Display an image. Pixel values are displayed when the pointer is over valid image data. If a figure object is specified then the image is drawn in that figure, and fig.show() is not called. The figure is closed on key entry ‘q’.

Parameters:
img : array_like, shape (Nr, Nc) or (Nr, Nc, 3) or (Nr, Nc, 4)

Image to display

title : string, optional (default None)

Figure title

copy : boolean, optional (default True)

If True, create a copy of input img as a reference for displayed pixel values, ensuring that displayed values do not change when the array changes in the calling scope. Set this flag to False if the overhead of an additional copy of the input image is not acceptable.

fltscl : boolean, optional (default False)

If True, rescale and shift floating point arrays to [0,1]

intrp : string, optional (default ‘nearest’)

Specify type of interpolation used to display image (see interpolation parameter of matplotlib.axes.Axes.imshow)

norm : matplotlib.colors.Normalize object, optional (default None)

Specify the matplotlib.colors.Normalize instance used to scale pixel values for input to the colour map

cbar : boolean, optional (default False)

Flag indicating whether to display colorbar

cmap : matplotlib.colors.Colormap, optional (default None)

Colour map for image. If none specifed, defaults to cm.Greys_r for monochrome image

fgsz : tuple (width,height), optional (default None)

Specify figure dimensions in inches

fgnm : integer, optional (default None)

Figure number of figure

fig : matplotlib.figure.Figure object, optional (default None)

Draw in specified figure instead of creating one

ax : matplotlib.axes.Axes object, optional (default None)

Plot in specified axes instead of current axes of figure

Returns:
fig : matplotlib.figure.Figure object

Figure object for this figure

ax : matplotlib.axes.Axes object

Axes object for this plot

sporco.plot.close(fig=None)[source]

Close figure(s). If a figure object reference or figure number is provided, close the specified figure, otherwise close all figures.

Parameters:
fig : matplotlib.figure.Figure object or integer, optional (default None)

Figure object or number of figure to close

sporco.plot.set_ipython_plot_backend(backend='qt')[source]

Set matplotlib backend within an ipython shell. Ths function has the same effect as the line magic %matplotlib [backend] but is called as a function and includes a check to determine whether the code is running in an ipython shell, so that it can safely be used within a normal python script since it has no effect when not running in an ipython shell.

Parameters:
backend : string, optional (default ‘qt’)

Name of backend to be passed to the %matplotlib line magic command

sporco.plot.set_notebook_plot_backend(backend='inline')[source]

Set matplotlib backend within a Jupyter Notebook shell. Ths function has the same effect as the line magic %matplotlib [backend] but is called as a function and includes a check to determine whether the code is running in a notebook shell, so that it can safely be used within a normal python script since it has no effect when not running in a notebook shell.

Parameters:
backend : string, optional (default ‘inline’)

Name of backend to be passed to the %matplotlib line magic command

sporco.plot.config_notebook_plotting()[source]

Configure plotting functions for inline plotting within a Jupyter Notebook shell. This function has no effect when not within a notebook shell, and may therefore be used within a normal python script.