pylab_aux - auxiliary functions for plotting with pylab¶
This module serves as a collection of useful routines for data plotting with matplotlib.
Generic plotting¶
plot_line()
- plot a line.
scatter_trend()
- plot a scatter plot with a regression line.
plot_function_3d()
- plot a 3D graph of a function of two variables.
plot_function_contour()
- plot a contour graph of a function of two variables.
Spectrum visualization¶
plot_spectrum()
- plot a single spectrum (m/z vs intensity).
annotate_spectrum()
- plot and annotate peaks in MS/MS spectrum.
mirror()
- create a mirror plot of two spectra (usingspectrum_utils
).
FDR control¶
plot_qvalue_curve()
- plot the dependence of q-value on the amount of PSMs (similar to a ROC curve).
Dependencies¶
This module requires matplotlib
. Optional dependencies: adjustText
, spectrum_utils
.
-
class
pyteomics.pylab_aux.
SpectrumUtilsColorScheme
(colors)[source]¶ Bases:
object
Context manager that temporarily changes spectrum_utils.plot.colors.
-
pyteomics.pylab_aux.
annotate_spectrum
(spectrum, peptide, *args, **kwargs)[source]¶ Plot a spectrum and annotate matching fragment peaks.
Parameters: - spectrum (dict) – A spectrum as returned by Pyteomics parsers. Needs to have ‘m/z array’ and ‘intensity array’ keys.
- peptide (str) – A modX sequence.
- backend (str, keyword only, optional) – One of {‘default’, ‘spectrum_utils’, ‘spectrum_utils.iplot’}.
The spectrum_utils backend requires installing
spectrum_utils
. The spectrum_utils.iplot backend requires installingspectrum_utils[iplot]
. - ion_types (Container, keyword only, optional) – Ion types to be considered for annotation. Default is (‘b’, ‘y’).
- precursor_charge (int, keyword only, optional) – If not specified, an attempt is made to extract it from spectrum.
- maxcharge (int, keyword only, optional) – Maximum charge state for fragment ions to be considered. Default is precursor_charge - 1.
- colors (dict, keyword only, optional) – Keys are ion types, values are colors to plot the annotated peaks with. Default depends on backend.
- ftol (float, keyword only, optional) – A fixed m/z tolerance value for peak matching. Alternative to rtol.
- rtol (float, keyword only, optional) – A relative m/z error for peak matching. Default is 10 ppm.
- aa_mass (dict, keyword only, optional) – A dictionary of amino acid residue masses.
- text_kw (dict, keyword only, optional) – Keyword arguments for
pylab.text()
. - xlabel (str, keyword only, optional) – Label for the X axis. Default is “m/z”. Does not work with spectrum_utils.iplot backend.
- ylabel (str, keyword only, optional) – Label for the Y axis. Default is “intensity”. Does not work with spectrum_utils.iplot backend.
- title (str, keyword only, optional) – The title. Empty by default. Does not work with spectrum_utils.iplot backend.
- ax (matplotlib.pyplot.Axes, keyword only, optional) – Axes to draw the spectrum. Does not work with spectrum_utils.iplot backend.
- *args – Passed to the plotting backend.
- **kwargs – Passed to the plotting backend.
- centroided (bool, keyword only, optional) – Passed to
plot_spectrum()
. Only works with default backend. - ion_comp (dict, keyword only, optional) – A dictionary defining ion compositions to override
pyteomics.mass.std_ion_comp
. Only works with default backend. - mass_data (dict, keyword only, optional) – A dictionary of element masses to override
pyteomics.mass.nist_mass
. Only works with default backend. - adjust_text (bool, keyword only, optional) – Adjust the overlapping text annotations using
adjustText
. Only works with default backend. - adjust_kw (dict, keyword only, optional) – Keyword arguments for
adjust_text()
. Only works with default backend. - remove_precursor_peak (bool, keyword only, optional) – Remove precursor peak from spectrum before annotation. Default is
False
. Only works with spectrum_utils backend. - min_intensity (float, keyword only, optional) – Remove low-intensity peaks; this is a factor of maximum peak intensity. Default is 0 (no filtering). Only works with spectrum_utils and spectrum_utils.iplot backends.
- max_num_peaks (int or None, keyword only, optional) – Remove low-intensity peaks; this is the number of peaks to keep. Default is
None
(no filtering). Only works with spectrum_utils and spectrum_utils.iplot backends. - scaling (one of {‘root’, ‘log’, ‘rank’} or None, keyword only, optional) – Scaling to apply to peak intensities. Only works with spectrum_utils and spectrum_utils.iplot backends.
- max_intensity (float or None, keyword only, optional) – Intensity of the most intense peak relative to which the peaks will be scaled
(the default is
None
, which means that no scaling relative to the most intense peak will be performed). Only works with spectrum_utils and spectrum_utils.iplot backends. - aa_comp (dict, keyword only, optional) – Amino acid compositions, including modified ones. If given, will be used for conversion from modX to ProForma.
- mod_names (dict or callable, keyword only, optional) – If given, will be used for conversion from modX to ProForma.
- prefix (str, keyword only, optional) – If given, will be used for conversion from modX to ProForma.
Returns: out
Return type: matplotlib.pyplot.Axes
-
pyteomics.pylab_aux.
mirror
(spec_top, spec_bottom, peptide=None, spectrum_kws=None, ax=None, **kwargs)[source]¶ Create a mirror plot of two (possible annotated) spectra using spectrum_utils.
Parameters: - spec_top (dict) – A spectrum as returned by Pyteomics parsers. Needs to have ‘m/z array’ and ‘intensity array’ keys.
- spec_bottom (dict) – A spectrum as returned by Pyteomics parsers. Needs to have ‘m/z array’ and ‘intensity array’ keys.
- peptide (str or None, optional) – A modX sequence or ProForma. If provided, the peaks will be annotated as peptide fragments.
- spectrum_kws (dict or None, optional) – Passed to
spectrum_utils.plot.mirror()
. - backend (str, keyword only, optional) –
One of {‘spectrum_utils’, ‘spectrum_utils.iplot’}. Default is ‘spectrum_utils’.
Note
Requires
spectrum_utils
orspectrun_utils[iplot]
, respectively. - ax (matplotlib.pyplot.Axes or None, optional) – Passed to
spectrum_utils.plot.mirror()
. Works only for the ‘spectrum_utils’ backend. - xlabel (str, keyword only, optional) – Label for the X axis. Default is “m/z”. Works only for the ‘spectrum_utils’ backend.
- ylabel (str, keyword only, optional) – Label for the Y axis. Default is “intensity”. Works only for the ‘spectrum_utils’ backend.
- title (str, keyword only, optional) – The title. Empty by default. Works only for the ‘spectrum_utils’ backend.
- **kwargs (same as for
annotate_spectrum()
for spectrum_utils backends.) –
Returns: out
Return type: matplotlib.pyplot.Axes
-
pyteomics.pylab_aux.
plot_function_3d
(x, y, function, **kwargs)[source]¶ Plot values of a function of two variables in 3D.
More on 3D plotting in pylab:
http://www.scipy.org/Cookbook/Matplotlib/mplot3D
Parameters: - x (array_like of float) – The plotting range on X axis.
- y (array_like of float) – The plotting range on Y axis.
- function (function) – The function to plot.
- plot_type ({'surface', 'wireframe', 'scatter', 'contour', 'contourf'}, keyword only, optional) – The type of a plot, see scipy cookbook for examples. The default value is ‘surface’.
- num_contours (int) – The number of contours to plot, 50 by default.
- xlabel (str, keyword only, optional) – The X axis label. Empty by default.
- ylabel (str, keyword only, optional) – The Y axis label. Empty by default.
- zlabel (str, keyword only, optional) – The Z axis label. Empty by default.
- title (str, keyword only, optional) – The title. Empty by default.
- **kwargs – Passed to the respective plotting function.
-
pyteomics.pylab_aux.
plot_function_contour
(x, y, function, **kwargs)[source]¶ Make a contour plot of a function of two variables.
Parameters: - y (x,) – The positions of the nodes of a plotting grid.
- function (function) – The function to plot.
- filling (bool) – Fill contours if True (default).
- num_contours (int) – The number of contours to plot, 50 by default.
- ylabel (xlabel,) – The axes labels. Empty by default.
- title (str, optional) – The title. Empty by default.
- **kwargs – Passed to
pylab.contour()
orpylab.contourf()
.
-
pyteomics.pylab_aux.
plot_line
(a, b, xlim=None, *args, **kwargs)[source]¶ Plot a line y = a * x + b.
Parameters: Returns: out – The line object.
Return type: matplotlib.lines.Line2D
-
pyteomics.pylab_aux.
plot_qvalue_curve
(qvalues, *args, **kwargs)[source]¶ Plot a curve with q-values on the X axis and corresponding PSM number (starting with
1
) on the Y axis.Parameters: - qvalues (array-like) – An array of q-values for sorted PSMs.
- xlabel (str, keyword only, optional) – Label for the X axis. Default is “q-value”.
- ylabel (str, keyword only, optional) – Label for the Y axis. Default is “# of PSMs”.
- title (str, keyword only, optional) – The title. Empty by default.
- *args – Given to
pylab.plot()
after x and y. - **kwargs – Given to
pylab.plot()
.
Returns: out
Return type: matplotlib.lines.Line2D
-
pyteomics.pylab_aux.
plot_spectrum
(spectrum, *args, **kwargs)[source]¶ Plot a spectrum, assuming it is a dictionary containing “m/z array” and “intensity array”.
Parameters: - spectrum (dict) – A dictionary, as returned by pyteomics MS data parsers. Must contain “m/z array” and “intensity array” keys with decoded arrays.
- backend (str, keyword only, optional) – One of {‘default’, ‘spectrum_utils’, ‘spectrum_utils.iplot’}.
The spectrum_utils backend requires installing
spectrum_utils
. The spectrum_utils.iplot backend requires installingspectrum_utils[iplot]
. - xlabel (str, keyword only, optional) – Label for the X axis. Default is “m/z”.
- ylabel (str, keyword only, optional) – Label for the Y axis. Default is “intensity”.
- title (str, keyword only, optional) – The title. Empty by default.
- centroided (bool, keyword only, optional) – Works only for the default backend.
If
True
(default), peaks of the spectrum are plotted usingpylab.bar()
. IfFalse
, the arrays are simply plotted usingpylab.plot()
. - *args – When using default backend: given to
pylab.plot()
orpylab.bar()
(depending on centroided). - **kwargs – When using default backend: given to
pylab.plot()
orpylab.bar()
(depending on centroided). - min_intensity (float, keyword only, optional) – Remove low-intensity peaks; this is a factor of maximum peak intensity. Default is 0 (no filtering). Only works with spectrum_utils and spectrum_utils.iplot backends.
- max_num_peaks (int or None, keyword only, optional) – Remove low-intensity peaks; this is the number of peaks to keep. Default is
None
(no filtering). Only works with spectrum_utils and spectrum_utils.iplot backends. - scaling (one of {‘root’, ‘log’, ‘rank’} or None, keyword only, optional) – Scaling to apply to peak intensities. Only works with spectrum_utils and spectrum_utils.iplot backends.
- max_intensity (float or None, keyword only, optional) – Intensity of the most intense peak relative to which the peaks will be scaled
(the default is
None
, which means that no scaling relative to the most intense peak will be performed). Only works with spectrum_utils and spectrum_utils.iplot backends.
Returns: out
Return type: matplotlib.pyplot.Axes
-
pyteomics.pylab_aux.
scatter_trend
(x, y=None, **kwargs)[source]¶ Make a scatter plot with a linear regression.
Parameters: - x (array_like of float) – 1-D array of floats. If y is omitted, x must be a 2-D array of shape (N, 2).
- y (array_like of float, optional) – 1-D arrays of floats. If y is omitted or
None
, x must be a 2-D array of shape (N, 2). - plot_trend (bool, optional) – If
True
then plot a trendline (default). - plot_sigmas (bool, optional) – If
True
then plot confidence intervals of the linear fit.False
by default. - show_legend (bool, optional) – If
True
, a legend will be shown with linear fit equation, correlation coefficient, and standard deviation from the fit. Default isTrue
. - title (str, optional) – The title. Empty by default.
- ylabel (xlabel,) – The axes labels. Empty by default.
- alpha_legend (float, optional) – Legend box transparency. 1.0 by default
- scatter_kwargs (dict, optional) – Keyword arguments for
pylab.scatter()
. Empty by default. - plot_kwargs (dict, optional) – Keyword arguments for
plot_line()
. By default, sets xlim and label. - legend_kwargs (dict, optional) – Keyword arguments for
pylab.legend()
. Default is{'loc': 'upper left'}
. - sigma_kwargs (dict, optional) – Keyword arguments for
pylab.plot()
used for sigma lines. Default is{'color': 'red', 'linestyle': 'dashed'}
. - sigma_values (iterable, optional) – Each value will be multiplied with standard error of the fit, and the line
shifted by the resulting value will be plotted. Default is
range(-3, 4)
. - regression (callable, optional) – Function to perform linear regression. Will be given
x
andy
as arguments. Must return a 4-tuple: (a, b, r, stderr). Default ispyteomics.auxiliary.linear_regression()
.
Returns: out – A (scatter_plot, trend_line, sigma_lines, legend) tuple.
Return type: