1212 setup_plotstyle ,
1313 setup_tex_fonts ,
1414)
15+ from maxplotlib .backends .plotext import PlotextFigure , create_plotext_figure
1516from maxplotlib .colors .colors import Color
1617from maxplotlib .linestyle .linestyle import Linestyle
1718from maxplotlib .subfigure .line_plot import LinePlot
@@ -199,6 +200,7 @@ def __init__(
199200 self ._plotted = False
200201 self ._matplotlib_fig = None
201202 self ._matplotlib_axes = None
203+ self ._plotext_figure = None
202204 self ._suptitle : str | None = None
203205 self ._suptitle_kwargs : dict = {}
204206
@@ -681,7 +683,6 @@ def savefig(
681683 if self ._plotted :
682684 self ._matplotlib_fig .savefig (full_filepath )
683685 else :
684-
685686 fig , axs = self .plot (
686687 backend = "matplotlib" ,
687688 savefig = True ,
@@ -690,6 +691,33 @@ def savefig(
690691 fig .savefig (full_filepath )
691692 if verbose :
692693 print (f"Saved { full_filepath } " )
694+ elif backend == "plotext" :
695+ if layer_by_layer :
696+ layers = []
697+ for layer in self .layers :
698+ layers .append (layer )
699+ figure = self .plot (
700+ backend = "plotext" ,
701+ savefig = False ,
702+ layers = layers ,
703+ )
704+ _fn = f"{ filename_no_extension } _{ layers } .{ extension } "
705+ figure .savefig (_fn )
706+ print (f"Saved { _fn } " )
707+ else :
708+ if layers is None :
709+ layers = self .layers
710+ full_filepath = filename
711+ else :
712+ full_filepath = f"{ filename_no_extension } _{ layers } .{ extension } "
713+ figure = self .plot (
714+ backend = "plotext" ,
715+ savefig = False ,
716+ layers = layers ,
717+ )
718+ figure .savefig (full_filepath )
719+ if verbose :
720+ print (f"Saved { full_filepath } " )
693721
694722 def plot (
695723 self ,
@@ -709,6 +737,12 @@ def plot(
709737 )
710738 elif backend == "plotly" :
711739 return self .plot_plotly (savefig = savefig )
740+ elif backend == "plotext" :
741+ return self .plot_plotext (
742+ savefig = savefig ,
743+ layers = layers ,
744+ verbose = verbose ,
745+ )
712746 elif backend == "tikzfigure" :
713747 return self .plot_tikzfigure (savefig = savefig )
714748 else :
@@ -733,6 +767,14 @@ def show(
733767 # self._matplotlib_fig.show()
734768 elif backend == "plotly" :
735769 self .plot_plotly (savefig = False )
770+ elif backend == "plotext" :
771+ figure = self .plot_plotext (
772+ savefig = False ,
773+ layers = layers ,
774+ verbose = verbose ,
775+ )
776+ figure .show ()
777+ return figure
736778 elif backend == "tikzfigure" :
737779 fig = self .plot_tikzfigure (savefig = False , verbose = verbose )
738780 # TikzFigure handles all rendering (single or multi-subplot)
@@ -862,7 +904,7 @@ def plot_tikzfigure(
862904 else None
863905 ),
864906 grid = line_plot ._grid ,
865- caption = line_plot ._title or f"Subplot { col + 1 } " ,
907+ caption = line_plot ._title or f"Subplot { col + 1 } " ,
866908 width = 0.45 ,
867909 )
868910
@@ -890,6 +932,36 @@ def plot_tikzfigure(
890932
891933 return fig
892934
935+ def plot_plotext (
936+ self ,
937+ savefig : bool = False ,
938+ layers : list | None = None ,
939+ verbose : bool = False ,
940+ ) -> PlotextFigure :
941+ if verbose :
942+ print ("Generating plotext figure..." )
943+
944+ figure = create_plotext_figure (self .nrows , self .ncols )
945+
946+ for row , col , subplot in self .iter_subplots ():
947+ ax = (
948+ figure
949+ if (self .nrows , self .ncols ) == (1 , 1 )
950+ else figure .subplot (row + 1 , col + 1 )
951+ )
952+ if isinstance (subplot , TikzFigure ):
953+ raise NotImplementedError (
954+ "tikzfigure subplots cannot be rendered with the plotext backend."
955+ )
956+ subplot .plot_plotext (ax , layers = layers )
957+
958+ wrapped = PlotextFigure (figure = figure , suptitle = self ._suptitle )
959+ if savefig and isinstance (savefig , str ):
960+ wrapped .savefig (savefig )
961+
962+ self ._plotext_figure = wrapped
963+ return wrapped
964+
893965 def plot_plotly (self , show = True , savefig = None , usetex = False ):
894966 """
895967 Generate and optionally display the subplots using Plotly.
0 commit comments