Skip to content

happy.plots

Jan Kukačka edited this page Mar 9, 2022 · 1 revision

happy.plots

Module for plotting functions.

Fancy plotting

Colormaps and colors

Axes setup


boxplot(data, ax=None, edge_color=None, fill_color=None, **kwargs):

Extends the functionality of standard pyplot boxplot function by specifying colors.

Arguments:

  • data: array or sequence of vectors. (Same as original)
  • ax: axes object. If None, plt.gca() is used.
  • edge_color: Color or list of colors for lines and box borders
  • fill_color: Color or list of colors for filling the boxes
  • kwargs: Passed to pyplot.boxplot

Retuns:

  • dict of boxplot artists (same as original).

cmap(*keypoints):

Returns a linear segmented colormap from given keypoints.

Arguments:

  • keypoints: List of keypoints which can be either a color or a 2-tuple of (index, color), where index is in range [0;1] and provides the location of the keypoint. If the index is not given, keypoints will be distributed equally on the range. color is any color format accepted by matplotlib, and 't' can be used for transparent color. Colors and 2-tuples cannot be mixed together.

Returns:

  • colormap

Examples:

Colormap ranging from transparent to red

cmap = happy.plots.cmap('t', 'r')
## Old format (still compatible)
cmap = happy.plots.cmap(['t', 'r'])

Colormap with positions specified and transparent upper end

cmap = happy.plots.cmap((0, 'black'), (0.2, 'xkcd: green'), (254/255, 'r'), (1, 't'))

cmap_clip(colors=None, clip_up=True, clip_down=True):

Creates colormap with gradient of given colors and transparent color for max and min value. Ideal for overlays on images.

Arguments:

  • colors: List of colors of length at least 2. If None, 'red' and 'blue' are used.
  • clip_up: Bool. If True, highest values will be clipped (transparent). Default True.
  • clip_down: Bool. If True, lowest values will be clipped (transparent). Default True.

hide_ticks(axes, axis='both'):

Hide axes ticks and their labels.

Arguments:

  • axes: axes object to hide ticks on
  • axis: 'x', 'y', 'both' (default). Which axis will be affected.

imshow(axes, img, alpha, cmap=None, norm=None, **kwargs):

Extending pyplot.imshow with ability to give alpha as an array

Arguments:

  • axes: axes object.
  • img: array of shape (height, width) or (height, width, channels) where channels is either 1, 3 or 4. If it is 4 (alpha is given too), then the original alpha and given alpha are multiplied.
  • alpha: array of shape (height, width) or scalar (behaves like normal imshow then)
  • cmap: colormap to use. Defaults to rcParams default.
  • norm: normalization function to use. Defaults to matplotlib.colors.Normalize.
  • kwargs: passed to pyplot.imshow.

lighten_color(color, amount=0.5):

Lightens the given color by multiplying (1-luminosity) by the given amount. Input can be matplotlib color string, hex string, or RGB tuple.

Arguments:

  • color: matplotlib color (string, rgb-tuple, rgba-tuple, hex-code)
  • amount: float in range (0;1)

Examples:

lighten_color('g', 0.3)
lighten_color('#F034A3', 0.6)
lighten_color((.3,.55,.1), 0.5)

matlab_cmap():

Returns matlab default colormap.


normalize_color_list(colors, list_len):

Ensures that the variable "colors" contains a list of colors of length list_len. If colors is a single color, it is repeated. If it is a list of colors already, it is expanded to the right length by repeating the last element or trimmed if it is too long. Also ensures that if colors is a single color represented as a list, it will be correctly understood.

Arguments:

  • colors: Color or list of colors
  • list_len: positive int. Length of the color list to be produced.

Returns:

  • color_list: List of colors of lenght list_len.

scalebar(axes, res, ax_shape, len_cm=.5, color='w'):

Displays scalebar on the plot.

Arguments:

  • axes: axes object to display scalebar on.
  • res: image resolution in horizontal dimension in microns
  • ax_shape: (height, width) of the displayed image
  • len_cm: float. Lenght of the scalebar in centimeters
  • color: valid matplotlib color for the scalebar.

set_spine_thickness(axes=None, thickness=1):

Allows setting thickness of spines of given axes.

Arguments:

  • axes: axes object. Default is plt.gca()
  • thickness: positive float. Desired thickness.

set_ticks_cm(axes, resolution, shape):

This function performs standard setup of axes to display size scale in cm.

Arguments:

  • axes: pyplot axes object to setup. If using functional pyplot interface, can be obtained via plt.gca().
  • resolution: Integer. Size of one image pixel in microns (μm).
  • shape: Tuple of ints. Shape of the displayed image in pixels (height, width).

set_ticks_wavelengths(axes):

Sets x-axis to show wavelenghts from 700 to 970 nm:

  • sets ticks and tick labels
  • sets axis label
  • sets axis limits (0,27)
  • sets grid on

Arguments:

  • axes: axes object to work on. Can be obtained via plt.gca().

smooth_plot(axes, x, y=None, smoothing=5, **kwargs):

Smoothed version of pyplot.plot(). Plots single line on the given axes.

Arguments:

  • axes: axes obejct.
  • x, (y): arrays of shape (n,) with x and y coordinates. If y is missing, x is considered y and range(0,n) is considered x.
  • smoothing: positive int. How many samples should be interpolated between each pair of points? Default 5.
  • kwargs: passed to normal plot.

Returns:

  • list of Line objects

smooth_fill_between(x, y1, y2=0, smoothing=5, axes=None, **kwargs):

Smoothed version of pyplot.fill_between(). Plots single shaded region on the given axes.

Arguments:

  • x, (y1, y2): arrays of shape (n,) with x and y coordinates. If y2 is missing, it is considered to be zeros.
  • smoothing: positive int. How many samples should be interpolated between each pair of points? Default 5.
  • axes: axes obejct. If None, plt.gca() is used.
  • kwargs: passed to normal fill_between.

Returns:

  • A PolyCollection with plotted polygons