1010 GCPeakCalculation ,
1111 LCMSMassFeatureCalculation ,
1212)
13+ from corems .encapsulation .plot_utils import _finalize_plot
1314from corems .mass_spectra .factory .chromat_data import EIC_Data
1415from corems .molecular_id .factory .EI_SQL import LowResCompoundRef
1516
@@ -594,6 +595,7 @@ def plot(
594595 self ,
595596 to_plot = ["EIC" , "MS1" , "MS2" ],
596597 return_fig = True ,
598+ path = None ,
597599 plot_smoothed_eic = False ,
598600 plot_eic_datapoints = False ,
599601 molecular_metadata = None ,
@@ -608,7 +610,11 @@ def plot(
608610 "EIC", "MS2", "MS2_mirror", and "MS1".
609611 Default is ["EIC", "MS1", "MS2"].
610612 return_fig : bool, optional
611- If True, the figure is returned. Default is True.
613+ If True, return the open figure (caller owns lifecycle).
614+ Default is True.
615+ path : str or path-like, optional
616+ If set, save the figure to this path. When ``return_fig`` is False,
617+ the figure is closed after saving and ``plt.show()`` is not called.
612618 plot_smoothed_eic : bool, optional
613619 If True, the smoothed EIC is plotted. Default is False.
614620 plot_eic_datapoints : bool, optional
@@ -623,8 +629,9 @@ def plot(
623629 Returns
624630 -------
625631 matplotlib.figure.Figure or None
626- The figure object if `return_fig` is True.
627- Otherwise None and the figure is displayed.
632+ The figure object if `return_fig` is True (left open for the caller).
633+ Otherwise None; the figure is displayed with ``plt.show()`` when
634+ ``path`` is not set.
628635 """
629636 # Adjust to_plot list if there are not spectra added to the mass features
630637 if self .mass_spectrum is None :
@@ -681,10 +688,7 @@ def plot(
681688 # Add space between subplots
682689 plt .tight_layout ()
683690
684- if return_fig :
685- # Close figure
686- plt .close (fig )
687- return fig
691+ return _finalize_plot (fig , return_fig = return_fig , path = path )
688692
689693 @property
690694 def mz (self ):
0 commit comments