Plotting utilities#

The sdt.plot module contains

  • the density_scatter() function, which creates a scatter plot where data points are colored according to data point density.

  • PanelLabel for creating sub-panel labels for paper figures as well as align_panellabels() for aligning them.

Programming reference#

sdt.plot.density_scatter(x, y, ax=None, cmap='viridis', **kwargs)[source]#

Make a scatter plot with points colored according to density

Use a Gaussian kernel density estimate to calculate the density of data points and color them accordingly.

Examples

>>> x, y = numpy.random.normal(size=(2, 1000))  # create data
>>> density_scatter(x, y)
Parameters:
  • x (array_like, shape(n, )) – Input data

  • y (array_like, shape(n, )) – Input data

  • ax (None or matplotlib.axes.Axes or bokeh.plotting.Figure, optional) – Object to use for drawing. If None, use matplotlib’s current axes (gca()).

  • cmap (str or matplotlib.colors.Colormap, optional) – Name of colormap or Colormap instance to be used for mapping densities to colors. Defaults to “viridis”.

  • **kwargs – Additional keyword arguments to be passed to ax’s scatter method.

class sdt.plot.PanelLabel(label, horizontalposition='axislabel', verticalposition='top', pad=0.0, **kwargs)[source]#

(Sub-) panel label for figures

Scientific figures often consist of more than one panel. This allows for adding labels (such as a, b, c, …) to the panels.

This has been tested with figures using constrained layout.

Examples

>>> fig, ax = matplotlib.pyplot.subplots(2, 2, constrained_layout=True)
>>> pls = []
>>> for x, a in zip("abcd", ax.flatten()):
...     pl = plot.PanelLabel(x)
...     a.add_artist(pl)
...     pls.append(pl)
>>> align_panellabels(pls)
Parameters:
  • label (str) – Label text

  • horizontalposition (str) – Where to position horizontally. Can be “axislabel” (align with the y axis label) or “frame” (align with the frame of the plot).

  • verticalposition (str) – Where to position vertically. Can be “top” (align with the top of the panel), “axislabel” (align with the top x axis label) or “frame” (align with the frame of the plot).

  • pad (float | Iterable[float]) – Extra space between label and panel

  • **kwargs – Additional settings. See matplotlib.text.Annotation for details. Note that horizontalalignment is "right" by default. If there is too much horizontal space, try setting horizontalalignment="left".

sdt.plot.align_panellabels(pls)[source]#

Align panel labels row-wise and column-wise

Labels in the same row will be aligned vertically, those in the same column horizontally. For this, all axes to which the panels have been added need to share the same GridSpec.

Examples

>>> x, y = numpy.random.normal(size=(2, 1000))  # create data
>>> density_scatter(x, y)
>>> fig, ax = matplotlib.pyplot.subplots(2, 2, constrained_layout=True)
>>> pls = []
>>> for x, a in zip("abcd", ax.flatten()):
...     pl = plot.PanelLabel(x)
...     a.add_artist(pl)
...     pls.append(pl)
>>> align_panellabels(pls)
Parameters:

pls (Iterable[PanelLabel]) – Panel labels to align

See also

PanelLabel