It would be nice to be able to produce some standard plots for extracted chromatograms and mass spectra.
The functions could go in a new module dhrmasslynxapi/plotting.py. They should be configurable so that the plot can be viewed interactively (via pyplot.show()) and/or saved to a file (via pyplot.savefig(...)).
The basic signature for plotting a chromatogram should be something like:
plot_chromatogram(t, i, xlab, show=True, savefig=None, figsize=(4, 1))
Parameterst : array-like
array of drift times or retention times
i : array-like
array of intensities
xlab : str
x axis label (e.g. "retention time (min)")
show : bool, default=True
whether to view the plot interactively using pyplot.show()savefig : None or str, default=None
if a filename is provided, save the image to file, if None do not save the image
figsize : tuple(float, float), default=(4, 1)
figure width, height in inches
The basic signature for plotting a mass spectrum should be something like:
plot_spectrum(mz, i, mz_min=None, mz_max=None, show=True, savefig=None, figsize=(4, 1))
Parametersmz : array-like
array of m/z values
i : array-like
array of intensities
mz_min : None or float, default=None
minimum m/z to display, None to use the minimum value from mz array
mz_max : None or float, default=None
maximum m/z to display, None to use the maximum value from mz array
show : bool, default=True
whether to view the plot interactively using pyplot.show()savefig : None or str, default=None
if a filename is provided, save the image to file, if None do not save the image
figsize : tuple(float, float), default=(4, 1)
figure width, height in inches
A note on implementation: For greater extensibility, it would be good to start with some "private" functions that generate the basic plots then return a matplotlib.Axes instance which can either be used by the "public" functions to just show/save the plot OR more traces/annotations/etc. can be added using that matplotlib.Axes instance. If the basic functions are implemented first with this in mind, such extended features could easily be added down the line. The "private" functions could also house some of the more granular control over things like plotting color/line width or style. The following example shows how this might be implemented for the basic plotting:
from matplotlib import pyplot as plt
def _plot_chromatogram(t, i, figsize, ...):
""" this is the "private" function"""
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(111)
# do the basic plotting and configuration ...
return ax
def plot_chromatogram(t, i, xlab, show=True, savefig=None, figsize=(4, 1)):
""" this is the "public" function """
# use the "private" function to do the actual plotting, then do any further customization that is needed
ax = _plot_chromatogram(t, i, figsize, ...)
ax.set_xlabel(xlab)
if show:
plt.show()
if savefig is not None:
plt.savefig(savefig, dpi=350, bbox_inches='tight')
plt.close()
It would be nice to be able to produce some standard plots for extracted chromatograms and mass spectra. The functions could go in a new module
dhrmasslynxapi/plotting.py
. They should be configurable so that the plot can be viewed interactively (viapyplot.show()
) and/or saved to a file (viapyplot.savefig(...)
).The basic signature for plotting a chromatogram should be something like:
Parameters
t
:array-like
array of drift times or retention timesi
:array-like
array of intensitiesxlab
:str
x axis label (e.g. "retention time (min)")show
:bool
, default=True
whether to view the plot interactively usingpyplot.show()
savefig
:None
orstr
, default=None
if a filename is provided, save the image to file, ifNone
do not save the imagefigsize
:tuple(float, float)
, default=(4, 1)
figure width, height in inchesThe basic signature for plotting a mass spectrum should be something like:
Parameters
mz
:array-like
array of m/z valuesi
:array-like
array of intensitiesmz_min
:None
orfloat
, default=None
minimum m/z to display,None
to use the minimum value frommz
arraymz_max
:None
orfloat
, default=None
maximum m/z to display,None
to use the maximum value frommz
arrayshow
:bool
, default=True
whether to view the plot interactively usingpyplot.show()
savefig
:None
orstr
, default=None
if a filename is provided, save the image to file, ifNone
do not save the imagefigsize
:tuple(float, float)
, default=(4, 1)
figure width, height in inchesA note on implementation: For greater extensibility, it would be good to start with some "private" functions that generate the basic plots then return a
matplotlib.Axes
instance which can either be used by the "public" functions to just show/save the plot OR more traces/annotations/etc. can be added using thatmatplotlib.Axes
instance. If the basic functions are implemented first with this in mind, such extended features could easily be added down the line. The "private" functions could also house some of the more granular control over things like plotting color/line width or style. The following example shows how this might be implemented for the basic plotting: