Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/plotext/_figure.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

335 statements  

1from plotext._default import default_figure_class 

2from plotext._monitor import monitor_class 

3from plotext._matrix import join_matrices 

4from plotext._date import date_class 

5from plotext._doc_utils import add 

6from time import time as _time 

7import plotext._utility as ut 

8from time import time 

9import os 

10 

11# A figure is a general container of either a plot (called monitor) or another figure, when subplots are nested 

12# This creates a hierarchy of figures, where master is the main/initial global figure, and parent is the figure containing (or above) the one considered 

13# The active figure is the one that can be accessed with further plotext commands - like plot(), limitsize() etc ..  

14# If a figure has no sub figures, then it is used for plotting, otherwise its sub figures are checked 

15 

16class _figure_class(): 

17 

18 def __init__(self, master = None, parent = None): 

19 self._set_family(master, parent) # it sets master, parent and active figure 

20 self.default = default_figure_class() # default values of figure class 

21 self.date = date_class() 

22 

23 self._set_size(None, None) # no initial size for general figure 

24 self.max_or_min = max # in a matrix of subplots the maximum height/width is considered (by default) for each row/column 

25 

26 self.monitor = monitor_class() if self._is_master else self._parent.monitor.copy() # each figure has a monitor for plotting; which by default is deep copied from its parent figure monitor, so that a figure with multiple sub plots can have easily the same plot and plot settings and preferences (without rewriting code) 

27 self.monitor.set_date(self.date) # to make sure that the date settings of a figure are the same for its subplots 

28 

29 self._set_master() if self._is_master else None # it sets the master figure size and other utilities  

30 

31 self._set_slots_max(*self._master._size) # sets the maximum number of sub figures in the current figure (from master figure size) 

32 self.subplots(0, 0) # no sub figures added by default, so that the current figure is used for plotting 

33 

34 def _set_family(self, master = None, parent = None): 

35 self._parent = self if parent is None else parent # the figure just above this one 

36 self._master = self if master is None else master # the figure above all others 

37 self._is_master = self is self._master 

38 self._active = self if self._is_master else self._master._active # the active figure, such that further plotting or settings commands refer to it 

39 

40 def _set_master(self): 

41 self._limit_size(True, True) # limit size (only available for master, as sub figures are by default limited by parent figure) 

42 self._set_terminal_size(*ut.terminal_size()) # get and set terminal size 

43 self._set_master_size() # set master size to terminal 

44 self._time = None # computational time of show() method, only available for global figure 

45 self._dummy = _figure_class(self._master, self._master) # the master has a dumm container for subplots that do not actually exist (anymore due to change of size) 

46 self._master.monitor.set_size(self._size) 

47 self._dummy.monitor.set_size(self._size) 

48 self._set_interactive() # if to make the final figure interactive: every command gets directly printed 

49 

50############################################## 

51########### Size Functions ############# 

52############################################## 

53 

54 def _set_interactive(self, interactive = None): 

55 self._interactive = self.default.interactive if interactive is None else bool(interactive) 

56 

57 def _set_size(self, width = None, height = None): 

58 self._width = None if width is None else int(width) 

59 self._height = None if height is None else int(height) 

60 self._size = [self._width, self._height] 

61 

62 def _limit_size(self, width = None, height = None): 

63 self._limit_width = self.default.limit_width if width is None else bool(width) 

64 self._limit_height = self.default.limit_height if height is None else bool(height) 

65 self._limit = [self._limit_width, self._limit_height] 

66 

67 def _set_terminal_size(self, width = None, height = None): 

68 self._width_term = self.default._width_term if width is None else width 

69 extra_lines = 2 if ut.is_ipython() else 1 

70 self._height_term = self.default._height_term if height is None else max(height - extra_lines, 0) 

71 self._size_term = [self._width_term, self._height_term] 

72 

73 def _set_master_size(self): 

74 width = self._width_term if self._width is None or (self._width > self._width_term and self._limit_width) else self._width 

75 height = self._height_term if self._height is None or (self._height > self._height_term and self._limit_height) else self._height 

76 self._set_size(width, height) 

77 

78############################################## 

79######### Subplots Functions ########### 

80############################################## 

81 

82 def _set_slots_max(self, width = None, height = None): 

83 self._rows_max = height # (height + 1) // 3  

84 self._cols_max = width # (width + 1) // 3  

85 self._slots_max = [self._rows_max, self._cols_max] 

86 

87 def _set_slots(self, rows = None, cols = None): 

88 rows = 1 if rows is None else int(abs(rows)) 

89 cols = 1 if cols is None else int(abs(cols)) 

90 self._rows = min(rows, self._rows_max) 

91 self._cols = min(cols, self._cols_max) 

92 self._Rows = list(range(1, self._rows + 1)) 

93 self._Cols = list(range(1, self._cols + 1)) 

94 self._slots = [self._rows, self._cols] 

95 self._no_plots = 0 in self._slots #or self._is_master 

96 

97 def _set_subplots(self): 

98 self.subfig = [[_figure_class(self._master, self) for col in self._Cols] for row in self._Rows] 

99 

100 def _get_subplot(self, row = None, col = None): 

101 return self.subfig[row - 1][col - 1] if row in self._Rows and col in self._Cols else self._master._dummy 

102 

103 def subplot(self, row = None, col = None): 

104 row = 1 if row is None else int(abs(row)) 

105 col = 1 if col is None else int(abs(col)) 

106 active = self._get_subplot(row, col) 

107 self._active = active 

108 self._master._active = active 

109 return self._master._active 

110 

111 def subplots(self, rows = None, cols = None): 

112 self._set_slots(rows, cols) 

113 self._set_subplots() 

114 return self 

115 

116############################################## 

117####### External Set Functions ######### 

118############################################## 

119 

120 def title(self, label = None): 

121 self.monitor.set_title(label) if self._no_plots else [[self._get_subplot(row, col).title(label) for col in self._Cols] for row in self._Rows] 

122 

123 def xlabel(self, label = None, xside = None): 

124 self.monitor.set_xlabel(label = label, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xlabel(label = label, xside = xside) for col in self._Cols] for row in self._Rows] 

125 

126 def ylabel(self, label = None, yside = None): 

127 self.monitor.set_ylabel(label = label, yside = yside) if self._no_plots else [[self._get_subplot(row, col).ylabel(label = label, yside = yside) for col in self._Cols] for row in self._Rows] 

128 

129 def xlim(self, left = None, right = None, xside = None): 

130 self.monitor.set_xlim(left = left, right = right, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xlim(left = left, right = right, xside = xside) for col in self._Cols] for row in self._Rows] 

131 

132 def ylim(self, lower = None, upper = None, yside = None): 

133 self.monitor.set_ylim(lower = lower, upper = upper, yside = yside) if self._no_plots else [[self._get_subplot(row, col).ylim(lower = lower, upper = upper, yside = yside) for col in self._Cols] for row in self._Rows] 

134 

135 def xscale(self, scale = None, xside = None): 

136 self.monitor.set_xscale(scale = scale, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xscale(scale = scale, xside = xside) for col in self._Cols] for row in self._Rows] 

137 

138 def yscale(self, scale = None, yside = None): 

139 self.monitor.set_yscale(scale = scale, yside = yside) if self._no_plots else [[self._get_subplot(row, col).yscale(scale = scale, yside = yside) for col in self._Cols] for row in self._Rows] 

140 

141 def xticks(self, ticks = None, labels = None, xside = None): 

142 self.monitor.set_xticks(ticks = ticks, labels = labels, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xticks(ticks = ticks, labels = labels, xside = xside) for col in self._Cols] for row in self._Rows] 

143 

144 def yticks(self, ticks = None, labels = None, yside = None): 

145 self.monitor.set_yticks(ticks = ticks, labels = labels, yside = yside) if self._no_plots else [[self._get_subplot(row, col).yticks(ticks = ticks, labels = labels, yside = yside) for col in self._Cols] for row in self._Rows] 

146 

147 def xfrequency(self, frequency = None, xside = None): 

148 self.monitor.set_xfrequency(frequency = frequency, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xfrequency(frequency = frequency, xside = xside) for col in self._Cols] for row in self._Rows] 

149 

150 def yfrequency(self, frequency = None, yside = None): 

151 self.monitor.set_yfrequency(frequency = frequency, yside = yside) if self._no_plots else [[self._get_subplot(row, col).yfrequency(frequency = frequency, yside = yside) for col in self._Cols] for row in self._Rows] 

152 

153 def xreverse(self, reverse = None, xside = None): 

154 self.monitor.set_xreverse(reverse = reverse, xside = xside) if self._no_plots else [[self._get_subplot(row, col).xreverse(reverse = reverse, xside = xside) for col in self._Cols] for row in self._Rows] 

155 

156 def yreverse(self, reverse = None, yside = None): 

157 self.monitor.set_yreverse(reverse = reverse, yside = yside) if self._no_plots else [[self._get_subplot(row, col).yreverse(reverse = reverse, yside = yside) for col in self._Cols] for row in self._Rows] 

158 

159 def xaxes(self, lower = None, upper = None): 

160 self.monitor.set_xaxes(lower = lower, upper = upper) if self._no_plots else [[self._get_subplot(row, col).xaxes(lower = lower, upper = upper) for col in self._Cols] for row in self._Rows] 

161 

162 def yaxes(self, left = None, right = None): 

163 self.monitor.set_yaxes(left = left, right = right) if self._no_plots else [[self._get_subplot(row, col).yaxes(left = left, right = right) for col in self._Cols] for row in self._Rows] 

164 

165 def frame(self, frame = None): 

166 self.monitor.set_frame(frame = frame) if self._no_plots else [[self._get_subplot(row, col).frame(frame = frame) for col in self._Cols] for row in self._Rows] 

167 

168 def grid(self, horizontal = None, vertical = None): 

169 self.monitor.set_grid(horizontal = horizontal, vertical = vertical) if self._no_plots else [[self._get_subplot(row, col).grid(horizontal = horizontal, vertical = vertical) for col in self._Cols] for row in self._Rows] 

170 

171 def canvas_color(self, color = None): 

172 self.monitor.set_canvas_color(color) if self._no_plots else [[self._get_subplot(row, col).canvas_color(color) for col in self._Cols] for row in self._Rows] 

173 

174 def axes_color(self, color = None): 

175 self.monitor.set_axes_color(color) if self._no_plots else [[self._get_subplot(row, col).axes_color(color) for col in self._Cols] for row in self._Rows] 

176 

177 def ticks_color(self, color = None): 

178 self.monitor.set_ticks_color(color) if self._no_plots else [[self._get_subplot(row, col).ticks_color(color) for col in self._Cols] for row in self._Rows] 

179 

180 def ticks_style(self, style = None): 

181 self.monitor.set_ticks_style(style) if self._no_plots else [[self._get_subplot(row, col).ticks_style(style) for col in self._Cols] for row in self._Rows] 

182 

183 def theme(self, theme = None): 

184 self.monitor.set_theme(theme) if self._no_plots else [[self._get_subplot(row, col).theme(theme) for col in self._Cols] for row in self._Rows] 

185 

186############################################## 

187########### Clear Functions ############ 

188###########################x################## 

189 

190 def clear_figure(self): 

191 self.__init__()# if self._no_plots else [[self._get_subplot(row, col).clear_figure() for col in self._Cols] for row in self._Rows] 

192 clf = clear_figure 

193 

194 def clear_data(self): 

195 self.monitor.data_init() if self._no_plots else [[self._get_subplot(row, col).clear_data() for col in self._Cols] for row in self._Rows] 

196 cld = clear_data 

197 

198 def clear_color(self): 

199 self.monitor.clear_color() if self._no_plots else [[self._get_subplot(row, col).clear_color() for col in self._Cols] for row in self._Rows] 

200 clc = clear_color 

201 

202 def clear_terminal(self, lines = None): 

203 ut.clear_terminal(lines = lines) 

204 clt = clear_terminal 

205 

206############################################## 

207########### Plot Functions ############# 

208############################################## 

209 

210 def _draw(self, *args, **kwargs): 

211 self.monitor.draw(*args, **kwargs) if self._no_plots else [[self._get_subplot(row, col)._draw(*args, **kwargs) for col in self._Cols] for row in self._Rows] 

212 

213 def scatter(self, *args, marker = None, color = None, style = None, fillx = None, filly = None, xside = None, yside = None, label = None): 

214 self._draw(*args, xside = xside, yside = yside, lines = False, marker = marker, color = color, style = style, fillx = fillx, filly = filly, label = label) 

215 

216 def plot(self, *args, marker = None, color = None, style = None, fillx = None, filly = None, xside = None, yside = None, label = None): 

217 self._draw(*args, xside = xside, yside = yside, lines = True, marker = marker, color = color, fillx = fillx, filly = filly, label = label) 

218 

219 def bar(self, *args, marker = None, color = None, fill = None, width = None, orientation = None, minimum = None, reset_ticks = None, xside = None, yside = None, label = None): 

220 self.monitor.draw_bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum, reset_ticks = reset_ticks) if self._no_plots else [[self._get_subplot(row, col).bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum, reset_ticks = reset_ticks) for col in self._Cols] for row in self._Rows] 

221 

222 def multiple_bar(self, *args, marker = None, color = None, fill = None, width = None, orientation = None, minimum = None, reset_ticks = None, xside = None, yside = None, labels = None): 

223 self.monitor.draw_multiple_bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, labels = labels, minimum = minimum, reset_ticks = reset_ticks) if self._no_plots else [[self._get_subplot(row, col).multiple_bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum, reset_ticks = reset_ticks) for col in self._Cols] for row in self._Rows] 

224 

225 def stacked_bar(self, *args, marker = None, color = None, fill = None, width = None, orientation = None, minimum = None, reset_ticks = None, xside = None, yside = None, labels = None): 

226 self.monitor.draw_stacked_bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, labels = labels, minimum = minimum, reset_ticks = reset_ticks) if self._no_plots else [[self._get_subplot(row, col).stacked_bar(*args, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum, reset_ticks = reset_ticks) for col in self._Cols] for row in self._Rows] 

227 

228 def hist(self, data, bins = None, marker = None, color = None, fill = None, norm = None, width = None, orientation = None, minimum = None, xside = None, yside = None, label = None): 

229 self.monitor.draw_hist(data, bins = bins, norm = norm, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum) if self._no_plots else [[self._get_subplot(row, col).hist(data, bins = bins, norm = norm, xside = xside, yside = yside, marker = marker, color = color, fill = fill, width = width, orientation = orientation, label = label, minimum = minimum) for col in self._Cols] for row in self._Rows] 

230 

231 def candlestick(self, dates, data, colors = None, orientation = None, xside = None, yside = None, label = None): 

232 self.monitor.draw_candlestick(dates, data, xside = xside, yside = yside, orientation = orientation, colors = colors, label = label) if self._no_plots else [[self._get_subplot(row, col).candlestick(dates, data, orientation = orientation, colors = colors, label = label) for col in self._Cols] for row in self._Rows] 

233 

234 def box(self, *args, quintuples = None, colors = None, fill = None, width = None, orientation = None, minimum = None, reset_ticks = None, xside = None, yside = None, label = None): 

235 self.monitor.draw_box(*args, xside = xside, yside = yside, orientation = orientation, colors = colors, label = label, fill = fill, width = width, minimum = minimum, reset_ticks = reset_ticks, quintuples = quintuples) if self._no_plots else [[self._get_subplot(row, col).box(*args, orientation = orientation, colors = colors, label = label, fill = fill, width = width, minimum = minimum, reset_ticks = reset_ticks, quintuples = quintuples) for col in self._Cols] for row in self._Rows] 

236 

237############################################## 

238########### Plotting Tools ############# 

239############################################## 

240 

241 def error(self, *args, xerr = None, yerr = None, color = None, xside = None, yside = None, label = None): 

242 self.monitor.draw_error(*args, xerr = xerr, yerr = yerr, xside = xside, yside = yside, color = color, label = label) if self._no_plots else [[self._get_subplot(row, col).error(*args, xerr = xerr, yerr = yerr, xside = xside, yside = yside, color = color, label = label) for col in self._Cols] for row in self._Rows] 

243 

244 def event_plot(self, data, marker = None, color = None, orientation = None, side = None): 

245 self.monitor.draw_event_plot(data, orientation = orientation, marker = marker, color = color, side = side) if self._no_plots else [[self._get_subplot(row, col).event_plot(data, orientation = orientation, marker = marker, color = color, side = side) for col in self._Cols] for row in self._Rows] 

246 eventplot = event_plot 

247 

248 def vertical_line(self, coordinate, color = None, xside = None): 

249 self.monitor.draw_vertical_line(coordinate, color = color, xside = xside) if self._no_plots else [[self._get_subplot(row, col).vertical_line(coordinate, color = color, xside = xside) for col in self._Cols] for row in self._Rows] 

250 vline = vertical_line 

251 

252 def horizontal_line(self, coordinate, color = None, yside = None): 

253 self.monitor.draw_horizontal_line(coordinate, color = color, yside = yside) if self._no_plots else [[self._get_subplot(row, col).horizontal_line(coordinate, color = color, yside = yside) for col in self._Cols] for row in self._Rows] 

254 hline = horizontal_line 

255 

256 def text(self, label, x, y, color = None, background = None, style = None, orientation = None, alignment = None, xside = None, yside = None): 

257 self.monitor.draw_text(label, x, y, xside = xside, yside = yside, color = color, background = background, style = style, orientation = orientation, alignment = alignment) if self._no_plots else [[self._get_subplot(row, col).text(label, x, y, xside = xside, yside = yside, color = color, background = background, style = style, orientation = orientation, alignment = alignment) for col in self._Cols] for row in self._Rows] 

258 

259 def rectangle(self, x = None, y = None, marker = None, color = None, lines = None, fill = None, xside = None, yside = None, label = None): 

260 self.monitor.draw_rectangle(x = x, y = y, xside = xside, yside = yside, lines = lines, marker = marker, color = color, fill = fill, label = label) if self._no_plots else [[self._get_subplot(row, col).rectangle(x = x, y = y, xside = xside, yside = yside, lines = lines, marker = marker, color = color, fill = fill, label = label) for col in self._Cols] for row in self._Rows] 

261 

262 def polygon(self, x = None, y = None, radius = None, sides = None, marker = None, color = None, lines = None, fill = None, xside = None, yside = None, label = None): 

263 self.monitor.draw_polygon(x = x, y = y, radius = radius, sides = sides, xside = xside, yside = yside, lines = lines, marker = marker, color = color, fill = fill, label = label) if self._no_plots else [[self._get_subplot(row, col).polygon(x = x, y = y, radius = radius, sides = sides, xside = xside, yside = yside, lines = lines, marker = marker, color = color, fill = fill, label = label) for col in self._Cols] for row in self._Rows] 

264 

265 def confusion_matrix(self, actual, predicted, color = None, style = None, labels = None): 

266 self.monitor.draw_confusion_matrix(actual, predicted, labels = labels, color = color, style = style) if self._no_plots else [[self._get_subplot(row, col).confusion_matrix(actual, predicted, labels = labels, color = color, style = style) for col in self._Cols] for row in self._Rows] 

267 

268 cmatrix = confusion_matrix 

269 

270 def indicator(self, value, label = None, color = None, style = None): 

271 self.monitor.draw_indicator(value, label = label, color = color, style = style) if self._no_plots else [[self._get_subplot(row, col).confusion_matrix(value, label = label, color = color, style = style) for col in self._Cols] for row in self._Rows] 

272 

273############################################## 

274############## 2D Plots ################ 

275##############################################  

276 

277 def matrix_plot(self, matrix, marker = None, style = None, fast = False): 

278 self.monitor.draw_matrix(matrix, marker = marker, style = style, fast = fast) if self._no_plots else [[self._get_subplot(row, col).matrix_plot(matrix, marker = marker, style = style, fast = fast) for col in self._Cols] for row in self._Rows] 

279 

280 def heatmap(self, dataframe, color = None, style = None): 

281 self.monitor.draw_heatmap(dataframe, color = color, style = style) if self._no_plots else [[self._get_subplot(row, col).heatmap(dataframe, color = color, style = style) for col in self._Cols] for row in self._Rows] 

282 

283 def image_plot(self, path, marker = None, style = None, fast = False, grayscale = False): 

284 self.monitor.draw_image(path, marker = marker, style = style, grayscale = grayscale, fast = fast) if self._no_plots else [[self._get_subplot(row, col).image_plot(path, marker = marker, style = style, grayscale = grayscale, fast = fast) for col in self._Cols] for row in self._Rows] 

285 

286############################################## 

287########### Date Functions ############# 

288############################################## 

289 

290 def date_form(self, input_form = None, output_form = None): 

291 self._master._dummy.date.date_form(input_form, output_form) 

292 if self._no_plots: 

293 self.monitor.date.date_form(input_form, output_form) 

294 else: 

295 [[self._get_subplot(row, col).date_form(input_form, output_form) for col in self._Cols] for row in self._Rows] 

296 

297 def set_time0(self, string, input_form = None): 

298 self.monitor.date.set_time0(string, form) if self._no_plots else [[self._get_subplot(row, col).set_time0(string, form) for col in self._Cols] for row in self._Rows] 

299 

300 def today_datetime(self): 

301 return self.monitor.date.today_datetime() 

302 

303 def today_string(self, output_form = None): 

304 return self.monitor.date.today_string(output_form) 

305 

306 def datetime_to_string(self, datetime, output_form = None): 

307 return self.monitor.date.datetime_to_string(datetime, output_form = output_form) 

308 

309 def datetimes_to_strings(self, datetimes, output_form = None): 

310 return self.monitor.date.datetimes_to_strings(datetimes, output_form = output_form) 

311 

312 def string_to_datetime(self, string, input_form = None): 

313 return self.monitor.date.string_to_datetime(string, input_form = input_form) 

314 

315 def string_to_time(self, string, input_form = None): 

316 return self.monitor.date.string_to_time(string, input_form = input_form) 

317 

318 def strings_to_time(self, string, input_form = None): 

319 return self.monitor.date.strings_to_time(string, input_form = input_form) 

320 

321############################################## 

322########### Build Functions ############ 

323############################################## 

324 

325 def show(self): # it build and shows the overall figure 

326 t = time() 

327 self.build() 

328 ut.write(self.monitor.matrix.canvas) # it prints the final canvas 

329 self._time = time() - t # computational time of build + print (it does not include any pre-processing time, which is gets more important for bar and image plots) 

330 self.main() if not self._master._interactive else None# it returns control to main figure on top level 

331 

332 def build(self): # it build the current figure without showing it 

333 self._set_sizes() 

334 self._build_matrix() 

335 self.monitor.matrix.set_canvas() if not self.monitor.fast_plot else None 

336 return self.monitor.matrix.get_canvas() 

337 

338 def _build_matrix(self): 

339 if self._no_plots: 

340 self.monitor.build_plot() if not self.monitor.fast_plot else None 

341 else: 

342 [[self._get_subplot(row, col)._build_matrix() for col in self._Cols] for row in self._Rows] 

343 matrices = [[self._get_subplot(row, col).monitor.matrix for col in self._Cols] for row in self._Rows] 

344 self.monitor.matrix = join_matrices(matrices) 

345 

346############################################## 

347######### Set Size Utilities ########### 

348############################################## 

349 

350 def _set_sizes(self): # it properly sets coherent sub figure dimensions 

351 self._set_slots_max(*self._size) 

352 self._set_slots(*self._slots) 

353 widths = self._get_widths() 

354 widths = ut.set_sizes(widths, self._width) # it sets the free subplots widths in accord with the parent figure width 

355 widths = ut.fit_sizes(widths, self._width) # it fits the subplots widths to the parent figure width  

356 heights = self._get_heights() 

357 heights = ut.set_sizes(heights, self._height) # it sets the free subplots height in accord with the parent figure height  

358 heights = ut.fit_sizes(heights, self._height) # it fits the subplots heights to the parent figure height 

359 width = sum(widths) if len(widths) > 1 else self._width 

360 height = sum(heights) if len(widths) > 1 else self._height 

361 

362 self.monitor.set_size(self._size) 

363 # self._set_size(width, height) 

364 [[self._set_subplot_size(row, col, widths[col - 1], heights[row - 1]) for col in self._Cols] for row in self._Rows] if (not self._no_plots) else None # to make sure that all sub figures have internal dimensions set as well  

365 

366 def _get_widths(self): # the subplots max/min widths for each column 

367 widths = [[self._get_subplot(row, col)._width for row in self._Rows] for col in self._Cols] 

368 widths = [self.max_or_min([sub for sub in el if sub is not None], default = None) for el in widths] 

369 return widths 

370 

371 def _get_heights(self): # the subplots max/min heights for each row 

372 heights = [[self._get_subplot(row, col)._height for col in self._Cols] for row in self._Rows] 

373 heights = [self.max_or_min([sub for sub in el if sub is not None], default = None) for el in heights] 

374 return heights 

375 

376 def _set_subplot_size(self, row = None, col = None, width = None, height = None): 

377 self._get_subplot(row, col)._set_size(width, height) 

378 self._get_subplot(row, col)._set_sizes() 

379 

380############################################## 

381###### Externally Called Utility ####### 

382############################################## 

383 

384 def _get_time(self, show = True): # it returns the computational time of latest show or build function 

385 time = ut.format_time(self._time) 

386 print(ut.format_strings("plotext time:", time, ut.title_color)) if show else None 

387 return self._time 

388 

389 def main(self): # returns the master figure and sets the active figure to the master 

390 self._master._active = self._master 

391 return self._master 

392 

393 def plot_size(self, width = None, height = None): 

394 width = self._width if width is None else width 

395 height = self._height if height is None else height 

396 self._set_size(width, height) 

397 self._set_master_size() if self._is_master else None 

398 self.monitor.size = self._size 

399 return self._width, self._height 

400 

401 plotsize = plot_size 

402 

403 def take_min(self): # in a matrix of subplots the maximum height/width will be considered for each row/column 

404 self.max_or_min = min 

405 

406 def save_fig(self, path = None, append = False, keep_colors = False): # it saves the plot as text or html, keep_colors = True preserves ansi colors for texts 

407 path = 'plotext.txt' if path is None or not ut.correct_path(path) else path 

408 _, extension = os.path.splitext(path) 

409 canvas = self.monitor.matrix.get_canvas() 

410 if extension == ".html": 

411 canvas = self.monitor.matrix.to_html() 

412 elif not keep_colors: 

413 canvas = ut.uncolorize(canvas) 

414 ut.save_text(canvas, path, append) 

415 savefig = save_fig 

416 

417############################################## 

418############ Docstrings ############### 

419############################################## 

420 add(subplots) 

421 add(subplots) 

422 add(subplot) 

423 add(main) 

424 

425 add(plot_size) 

426 add(take_min) 

427 

428 add(title) 

429 add(xlabel) 

430 add(ylabel) 

431 add(xlim) 

432 add(ylim) 

433 add(xscale) 

434 add(yscale) 

435 add(xticks) 

436 add(yticks) 

437 add(xfrequency) 

438 add(yfrequency) 

439 add(xreverse) 

440 add(yreverse) 

441 add(xaxes) 

442 add(yaxes) 

443 add(frame) 

444 add(grid) 

445 add(canvas_color) 

446 add(axes_color) 

447 add(ticks_color) 

448 add(ticks_style) 

449 add(theme) 

450 

451 add(clear_figure) 

452 add(clear_data) 

453 add(clear_color) 

454 add(clear_terminal) 

455 

456 add(scatter) 

457 add(plot) 

458 add(bar) 

459 add(multiple_bar) 

460 add(stacked_bar) 

461 add(hist) 

462 add(candlestick) 

463 add(box) 

464 

465 add(error) 

466 add(event_plot) 

467 add(vertical_line) 

468 add(horizontal_line) 

469 add(text) 

470 add(rectangle) 

471 add(polygon) 

472 add(confusion_matrix) 

473 add(indicator) 

474 

475 add(matrix_plot) 

476 add(image_plot) 

477 

478 add(show) 

479 add(build) 

480 add(save_fig) 

481 

482 add(date_form) 

483 add(set_time0) 

484 add(today_datetime) 

485 add(today_string) 

486 add(datetime_to_string) 

487 add(datetimes_to_strings) 

488 add(string_to_datetime)