plastid.plotting.plots module

Sommon common plots that are not directly implemented in matplotlib, as well as some specific plots used by plastid. Demos of these appear in Plotting tools.

General plots

stacked_bar()

Create a stacked bar graph

kde_plot()

Plot a kernel density estimate (continuous histogram) of data

scatterhist_x(), scatterhist_y(), and scatterhist_xy()

Create scatter plots with kernel density estimates of the marginal distributions along side them

profile_heatmap()

Plot a heatmap, with a columnwise median (or other profile summarizing the heatmap) in an upper panel above it, with aligned coordinates

triangle_plot()

Plot data lying on the plane x + y + z = k (e.g. codon phasing) in a homogeneous 2D representation

Plots for genomics

ma_plot()

Plot fold changes between x and y (\(\log_{2} (y/x)\)) as a function of the mean of x and y (\(0.5*(x+y)\)).

phase_plot()

For ribosome profiling. Plot sub-codon phasing of ribosome-protected foorpints stratified by read length, as well as the fraction of total reads represented by each length.

plastid.plotting.plots.get_color_cycle(ax)[source]

Get color cycle iterator from multiple versions of matplotlib axes

Parameters
axmatplotlib.axes.Axes
Returns
iterator

Iterator over colors, passable to matplotlib color keyword

plastid.plotting.plots.kde_plot(data, axes=None, color=None, label=None, alpha=0.7, vert=False, log=False, base=10, points=500, bw_method='scott', rescale=False, zorder=None, fill=True)[source]

Plot a kernel density estimate of data on axes.

Parameters
datanumpy.ndarray

Array of data

axesmatplotlib.axes.Axes or None, optional

Axes in which to place plot. If None, a new figure is generated. (Default: None)

colormatplotlib colorspec, optional

Color to use for plotting (Default: use next in matplotlibrc)

labelstr, optional

Name of data series (used for legend; default: None)

alphafloat, optional

Amount of alpha transparency to use (Default: 0.7)

vertbool, optional

If true, plot kde vertically

logbool, optional

If True, data is log-transformed before the kde is estimated. Data are converted back to non-log space afterwards.

base2, 10, or numpy.e, optional

If log is True, this serves as the base of the log space. If log is False, this is ignored. (Default: 2)

pointsint

Number of points over which to evaluate kde. (Default: 100)

bw_methodstr

Bandwith estimation method. See documentation for scipy.stats.gaussian_kde. (Default: “scott”)

Returns
matplotlib.figure.Figure

Parent figure of axes

matplotlib.axes.Axes

Axes containing plot

plastid.plotting.plots.ma_plot(x, y, axes=None, color=None, label=None, xlabel=None, ylabel=None, title=None, right_width=0.2, log='xy', min_x=- inf, max_x=inf, min_y=- inf, max_y=inf, scargs={'alpha': 0.7, 'facecolor': 'none', 'marker': 'o', 'rasterized': True, 's': 8}, mask_invalid=True, kdalpha=0.7)[source]

Plot fold changes (\(\log_{2} (y/x)\)) as a function of the mean of x and y (\(0.5*(x+y)\)).

Parameters
x, ynumpy.ndarray or list

Pair arrays or lists of corresponding numbers

colormatplotlib colorspec, optional

Color to use in plot

labelstr or None, optional

If not None, a label for plotting

xlabelstr or None, optional

If not None, an x-axis label

ylabelstr or None, optional

If not None, a y-axis label

right_widthfloat, optional

fraction of axes width to use in right panel containing marginal distribution (Default: 0.2)

axesmatplotlib.axes.Axes, dict, or None, optional

If a matplotlib.axes.Axes, an axes in which to place plot. This axes will be split into relevant panels.

If a dict, this is expected to be a group of pre-split axes.

If None, a new figure is generated, and axes are split. (Default: None)

mask_invalidbool, optional

If True mask out any `nan`s or `inf`s, as these mess up kernel density estimates and histograms in matplotlib, even if in masked arrays

min_x, min_y, max_x, max_ynumber, optional

If supplied, set values below min_x to min_x, values larger than max_x to max_x and so for min_y and max_y

logstr, “”, “x”, “xy”, or “xy”, optional

Plot these axes on a log scale (Default: “xy”)

scargsKeyword arguments, optional

Arguments to pass to scatter() (Default: plastid_default_scatter ). Recommend: set rasterized to True

kdalphafloat, optional

Alpha level (transparency) for marginal distributions (Default: 0.7)

Returns
matplotlib.figure.Figure

Figure

dict

Dictionary of axes. ‘orig’ refers to ax. The central panel is ‘main’. Other panels will be mapped to ‘top’, ‘left’ et c, if they are created.

plastid.plotting.plots.phase_plot(counts, labels=None, cmap=None, color=None, lighten_by=0.2, fig={}, line={}, bar={})[source]

Phasing plot for ribosome profiling

Creates a two-panel plot:

  • the top panel is a line graph indicating the fraction of reads as a function of read length

  • the bottom panel is a stacked bar graph, showing the fraction of reads in each codon position for each read length, with codon position 2 stacked above position 1 stacked above position 0

Parameters
countsnumpy.ndarray

Nx3 array of raw counts, where each row represents a read length, and each column a codon phase

labelslist, optional

Labels for each stack. If None, stacks are labeled sequentially by number. (Default: None)

lighten_byfloat, optional

Amount by which to lighten sequential blocks in each stack. (Default: 0.10)

cmapmatplotlib.colors.Colormap, optional

Colormap from which to generate bar colors. If supplied, will override any color attribute in **kwargs.

colormatplotlib colorspec, or list of these

Colors to use in plot. Overridden if cmap is supplied.

figdict

Keyword arguments to matplotlib.pylot.figure()

linedict

Keyword arguments to pass to matplotlib.pyplot.plot() in top panel

bardict

Keyword arguments to pass to matplotlib.pyplot.bar() in bottom panel

Returns
matplotlib.figure.Figure

Figure

tuple

Tuple of matplotlib.axes.Axes; the first corresponding to the line graph (top panel), the second, the bar graph (bottom).

plastid.plotting.plots.profile_heatmap(data, profile=None, x=None, axes=None, sort_fn=<function sort_max_position>, cmap=None, nancolor='#666666', im_args={}, plot_args={})[source]

Create a dual-paned plot in which profile is displayed in a top panel, above a heatmap showing the intensities of each row of data, optionally sorted top-to-bottom by sort_fn.

Parameters
datanumpy.ndarray

Array of data, in which each row is an individual aligned vector of data for region of interest, and each column a position in that vector

profilenumpy.ndarray or None

Reduced profile of data, often a column-wise median. If not supplied, it will be calculated.

xnumpy.ndarray

Array of values for X-axis

axesmatplotlib.axes.Axes or None, optional

Axes in which to place plot. If None, a new figure is generated. (Default: None)

sort_fnfunction, optional

Sort rows in data by this function before plotting. Function must return a numpy.ndarray of indices corresponding to rows in data (Default: sort by ascending argmax of each row)

cmapColormap, optional

Colormap to use in heatmap. It not None, overrides any value in im_args. (Default: None)

nancolorstr or matplotlib colorspec

Color used for plotting nan and other illegal or masked values

im_argsdict

Keyword arguments to pass to matplotlib.pyplot.imshow()

plot_argsdict

Keyword arguments to pass to matplotlib.pyplot.plot() for plotting metagene average

Returns
matplotlib.figure.Figure

Parent figure of axes

dict

Dictionary of matplotlib.axes.Axes. “top” refers to the panel containing the summary profile. “main” refers to the heatmap of individual values

plastid.plotting.plots.scatterhist_x(x, y, color=None, axes=None, label=None, top_height=0.2, mask_invalid=True, log='', min_x=- inf, min_y=- inf, max_x=inf, max_y=inf, scargs={'alpha': 0.7, 'facecolor': 'none', 'marker': 'o', 'rasterized': True, 's': 8}, bw_method='scott', kdalpha=0.7)[source]

Produce a scatter plot with a kernel density estimate of the marginal x distribution

Parameters
x, ynumpy.ndarray or list

Pair arrays or lists of corresponding numbers

colormatplotlib colorspec, optional

Color to use in plot

labelstr, or None

If not None, a label for plotting

axesmatplotlib.axes.Axes, dict, or None, optional

If a matplotlib.axes.Axes, an axes in which to place plot. This axes will be split into relevant panels.

If a dict, this is expected to be a group of pre-split axes.

If None, a new figure is generated, and axes are split. (Default: None)

top_heightfloat, optional

fraction of axes height to use in top panel containing marginal distribution (Default: 0.2)

mask_invalidbool, optional

If True mask out any `nan`s or `inf`s, as these mess up kernel density estimates and histograms in matplotlib, even if in masked arrays

min_x, min_y, max_x, max_ynumber, optional

If supplied, set values below min_x to min_x, values larger than max_x to max_x and so for min_y and max_y

logstr, “”, “x”, “xy”, or “xy”, optional

Plot these axes on a log scale (Default: “” for no log axes)

scargsKeyword arguments, optional

Arguments to pass to scatter() (Default: plastid_default_scatter). We highly recommend setting rasterized to True!

kdalphafloat, optional

Alpha level (transparency) for marginal distributions (Default: 0.7)

bw_methodstr

Bandwith estimation method. See documentation for scipy.stats.gaussian_kde. (Default: “scott”)

Returns
matplotlib.figure.Figure

Figure

dict

Dictionary of axes. ‘orig’ refers to ax. The central panel is ‘main’. Other panels will be mapped to ‘top’, ‘left’ et c, if they are created.

plastid.plotting.plots.scatterhist_xy(x, y, color=None, axes=None, label=None, top_height=0.2, right_width=0.2, mask_invalid=True, log='xy', min_x=- inf, min_y=- inf, max_x=inf, max_y=inf, scargs={'alpha': 0.7, 'facecolor': 'none', 'marker': 'o', 'rasterized': True, 's': 8}, kdalpha=0.7, bw_method='scott')[source]

Produce a scatter plot with kernel density estimate of the marginal x and y distributions

Parameters
x, ynumpy.ndarray or list

Pair arrays or lists of corresponding numbers

colormatplotlib colorspec, optional

Color to use in plot

labelstr, or None

If not None, a label for plotting top_height : float, optional fraction of axes height to use in top panel containing marginal distribution (Default: 0.2)

right_widthfloat, optional

fraction of axes width to use in right panel containing marginal distribution (Default: 0.2)

top_heightfloat, optional

fraction of axes height to use in top panel containing marginal distribution (Default: 0.2)

axesmatplotlib.axes.Axes, dict, or None, optional

If a matplotlib.axes.Axes, an axes in which to place plot. This axes will be split into relevant panels.

If a dict, this is expected to be a group of pre-split axes.

If None, a new figure is generated, and axes are split. (Default: None)

mask_invalidbool, optional

If True mask out any `nan`s or `inf`s, as these mess up kernel density estimates and histograms in matplotlib, even if in masked arrays

min_x, min_y, max_x, max_ynumber, optional

If supplied, set values below min_x to min_x, values larger than max_x to max_x and so for min_y and max-y

logstr, “”, “x”, “xy”, or “xy”, optional

Plot these axes on a log scale (Default: “” for no log axes)

scargsKeyword arguments, optional

Arguments to pass to scatter() (Default: plastid_default_scatter). We highly recommend setting rasterized to True!

kdalphafloat, optional

Alpha level (transparency) for marginal distributions (Default: 0.7)

bw_methodstr

Bandwith estimation method. See documentation for scipy.stats.gaussian_kde. (Default: “scott”)

Returns
matplotlib.figure.Figure

Figure

dict

Dictionary of axes. ‘orig’ refers to ax. The central panel is ‘main’. Other panels will be mapped to ‘top’, ‘left’ et c, if they are created.

plastid.plotting.plots.scatterhist_y(x, y, color=None, axes=None, label=None, right_width=0.2, mask_invalid=True, log='xy', min_x=- inf, min_y=- inf, max_x=inf, max_y=inf, scargs={'alpha': 0.7, 'facecolor': 'none', 'marker': 'o', 'rasterized': True, 's': 8}, bw_method='scott', kdalpha=0.7)[source]

Produce a scatter plot with a kernel density estimate of the marginal y distribution

Parameters
x, ynumpy.ndarray or list

Pair arrays or lists of corresponding numbers

colormatplotlib colorspec, optional

Color to use in plot

labelstr, or None

If not None, a label for plotting

axesmatplotlib.axes.Axes, dict, or None, optional

If a matplotlib.axes.Axes, an axes in which to place plot. This axes will be split into relevant panels.

If a dict, this is expected to be a group of pre-split axes.

If None, a new figure is generated, and axes are split. (Default: None)

right_widthfloat, optional

fraction of axes width to use in right panel containing marginal distribution (Default: 0.2)

mask_invalidbool, optional

If True mask out any `nan`s or `inf`s, as these mess up kernel density estimates and histograms in matplotlib, even if in masked arrays

min_x, min_y, max_x, max_ynumber, optional

If supplied, set values below min_x to min_x, values larger than max_x to max_x and so for min_y and max_y

logstr, “”, “x”, “xy”, or “xy”, optional

Plot these axes on a log scale (Default: “” for no log axes)

scargsKeyword arguments, optional

Arguments to pass to scatter() (Default: plastid_default_scatter). We highly recommend setting rasterized to True!

kdalphafloat, optional

Alpha level (transparency) for marginal distributions (Default: 0.7)

bw_methodstr

Bandwith estimation method. See documentation for scipy.stats.gaussian_kde. (Default: “scott”)

Returns
matplotlib.figure.Figure

Figure

dict

Dictionary of axes. ‘orig’ refers to ax. The central panel is ‘main’. Other panels will be mapped to ‘top’, ‘left’ et c, if they are created.

plastid.plotting.plots.sort_max_position(data)[source]

Generate indices that sort rows in data by column in which the row’s maximal value is attained

Parameters
datanumpy.ndarray
Returns
numpy.ndarray

Indices of rows that sort data by max position

plastid.plotting.plots.stacked_bar(data, axes=None, labels=None, lighten_by=0.1, cmap=None, **kwargs)[source]

Create a stacked bar graph

Parameters
datanumpy.ndarray

Array of data, in which each row is a stack, each column a value in that stack.

axesmatplotlib.axes.Axes or None, optional

Axes in which to place plot. If None, a new figure is generated. (Default: None)

labelslist, optional

Labels for each stack. If None, stacks are labeled sequentially by number. (Default: None)

lighten_byfloat, optional

Amount by which to lighten sequential blocks in each stack. (Default: 0.10)

cmapmatplotlib.colors.Colormap, optional

Colormap from which to generate bar colors. If supplied, will override any color attribute in **kwargs. (Default: None)

**kwargskeyword arguments

Other keyword arguments to pass to matplotlib.pyplot.bar()

Returns
matplotlib.figure.Figure

Parent figure of axes

matplotlib.axes.Axes

Axes containing plot

plastid.plotting.plots.triangle_plot(data, axes=None, fn='scatter', vertex_labels=None, grid=None, clip=True, do_setup=True, **kwargs)[source]

Plot data lying in a plane x + y + z = k in a homogenous triangular space.

Parameters
datanumpy.ndarray

Mx2 or Mx3 list or array of points in triangular space, where the first column is the first coordinate, the second column the second, and the third, the third.

axesmatplotlib.axes.Axes or None, optional

Axes in which to place plot. If None, a new figure is generated. (Default: None)

fnstr, optional

Name of plotting function. Must correspond to an attribute of a matplotlib.axes.Axes (e.g. ‘scatter’, ‘plot’, ‘hexbin’et c.), that is be able to take an Nx2 numpy.ndarray in Cartesian space as input (e.g. ‘plot’, ‘scatter’, ‘hexbin’; Default: ‘scatter’).

vertex_labelslist or None, optional

Labels for vertex. If None, vertices aren’t labeled. (Default: None)

gridnumpy.ndarray or None, optional

If not None, draw gridlines at intervals specified in grid, as long as the grid coordinate is > 0.33333 (center of triangle) and <= 1.0 (border).

clipbool, optional

If True clipping masks corresponding to the triangle boundaries will be applied to all plot elements (Default: True)

do_setupbool, optional

If True, the plot area will be prepared. A triangle will be drawn, gridlines drawn, et c. Specify False if plotting a second dataset ontop of an already-prepared axes (Default: True)

**kwargskeyword arguments

Other keyword arguments to pass to function specified by fn.

Returns
matplotlib.figure.Figure

Parent figure of axes

matplotlib.axes.Axes

Axes containing plot

plastid.plotting.plots.trianglize(data)[source]

Convert points from triangular space to Cartesian space for plotting. Called internally by triangle_plot().

Parameters
dataclass:numpy.ndarray

Mx2 or Mx3 list or array of points in triangular space, where the first column is the first coordinate, the second column the second, and the third, the third.

Returns
numpy.ndarray

Corresponding Nx2 array of points in Cartesian space, for plotting on a standard axis

plastid.plotting.plots.plastid_default_scatter = {'alpha': 0.7, 'facecolor': 'none', 'marker': 'o', 'rasterized': True, 's': 8}

Default parameters for scatter plots