22
33import re
44from pathlib import Path
5+ from typing import Any
56
6- from plotext . _figure import _figure_class
7+ from plotext import figure as _plotext_figure
78
89_ANSI_ESCAPE_RE = re .compile (r"\x1B\[[0-?]*[ -/]*[@-~]" )
910
@@ -12,20 +13,115 @@ def strip_ansi(text: str) -> str:
1213 return _ANSI_ESCAPE_RE .sub ("" , text )
1314
1415
15- def create_plotext_figure (nrows : int = 1 , ncols : int = 1 ) -> _figure_class :
16- figure = _figure_class ()
16+ class _Plotext6Axes :
17+ """Small compatibility surface for maxplotlib's plotext renderer."""
18+
19+ def __init__ (self , figure ):
20+ self ._figure = figure
21+
22+ def _draw (self , signal , label = None ):
23+ if label is not None :
24+ signal .label (label )
25+ self ._figure .draw (signal )
26+
27+ def plot (self , x , y , ** kwargs ):
28+ signal = self ._figure .signal (x , y , marker = kwargs .get ("marker" ))
29+ self ._draw (signal , kwargs .get ("label" ))
30+
31+ scatter = plot
32+
33+ def bar (self , * args , ** kwargs ):
34+ label = kwargs .pop ("label" , None )
35+ kwargs .pop ("color" , None )
36+ kwargs .pop ("fill" , None )
37+ signal = self ._figure .bar (* args , ** kwargs )
38+ self ._draw (signal , label )
39+
40+ def error (self , x , y , * , xerr = None , yerr = None , color = None , label = None ):
41+ signal = self ._figure .error (x , y , yerr , xerr , pixel = color )
42+ self ._draw (signal , label )
43+
44+ def matrix_plot (self , data , ** kwargs ):
45+ signal = self ._figure .heatmap (data , symbol = kwargs .get ("marker" ))
46+ self ._draw (signal )
47+
48+ def text (self , label , x , y , ** kwargs ):
49+ # Plotext 6 keeps text colour in its marker object; the renderer only
50+ # needs the portable text/alignment arguments here.
51+ kwargs .pop ("color" , None )
52+ signal = self ._figure .text (x , y , label , ** kwargs )
53+ self ._figure .draw (signal )
54+
55+ def title (self , label ):
56+ self ._figure .title (label )
57+
58+ def xlabel (self , label ):
59+ self ._figure .label (label , axis = 0 )
60+
61+ def ylabel (self , label ):
62+ self ._figure .label (label , axis = 1 )
63+
64+ def grid (self , active = True , * _args ):
65+ self ._figure .ruler ("x" ).grid (active )
66+ self ._figure .ruler ("y" ).grid (active )
67+
68+ def xlim (self , lower = None , upper = None ):
69+ self ._figure .ruler ("x" ).lim (lower , upper )
70+
71+ def ylim (self , lower = None , upper = None ):
72+ self ._figure .ruler ("y" ).lim (lower , upper )
73+
74+ def xscale (self , scale ):
75+ self ._figure .ruler ("x" ).scale (scale )
76+
77+ def yscale (self , scale ):
78+ self ._figure .ruler ("y" ).scale (scale )
79+
80+ def xticks (self , positions = None , labels = None ):
81+ self ._figure .ruler ("x" ).ticks (positions , labels )
82+
83+ def yticks (self , positions = None , labels = None ):
84+ self ._figure .ruler ("y" ).ticks (positions , labels )
85+
86+ def plotsize (self , width = None , height = None ):
87+ self ._figure .plot_size (width , height )
88+
89+ def horizontal_line (self , position , ** kwargs ):
90+ kwargs .pop ("color" , None )
91+ self ._figure .line (position , orientation = 0 , ** kwargs )
92+
93+ def vertical_line (self , position , ** kwargs ):
94+ kwargs .pop ("color" , None )
95+ self ._figure .line (position , orientation = 1 , ** kwargs )
96+
97+ def subplots (self , rows = None , cols = None ):
98+ self ._figure .subplots (rows , cols )
99+ return self
100+
101+ def subplot (self , row = None , col = None ):
102+ return _Plotext6Axes (self ._figure .subplot (row , col ))
103+
104+ def __getattr__ (self , name ):
105+ return getattr (self ._figure , name )
106+
107+
108+ def create_plotext_figure (nrows : int = 1 , ncols : int = 1 ):
109+ # Plotext 6 exposes the figure through its public API rather than the
110+ # removed private ``plotext._figure._figure_class``. It is a singleton,
111+ # so reset it before handing it to maxplotlib as a fresh canvas.
112+ figure = _Plotext6Axes (_plotext_figure .clear ())
17113 if nrows > 1 or ncols > 1 :
18114 figure .subplots (nrows , ncols )
19115 return figure
20116
21117
22118class PlotextFigure :
23- def __init__ (self , figure : _figure_class , suptitle : str | None = None ):
119+ def __init__ (self , figure : Any , suptitle : str | None = None ):
24120 self .figure = figure
25121 self .suptitle = suptitle
26122
27123 def build (self , keep_colors : bool = True ) -> str :
28- output = self .figure .build ()
124+ output = str ( self .figure .build () )
29125 if self .suptitle :
30126 output = f"{ self .suptitle } \n { output } "
31127 return output if keep_colors else strip_ansi (output )
0 commit comments