| 25 | """Experimental Matplotlib GUI version of tqdm!""" |
| 26 | # TODO: @classmethod: write() on GUI? |
| 27 | def __init__(self, *args, **kwargs): |
| 28 | from collections import deque |
| 29 | |
| 30 | import matplotlib as mpl |
| 31 | import matplotlib.pyplot as plt |
| 32 | kwargs = kwargs.copy() |
| 33 | kwargs['gui'] = True |
| 34 | colour = kwargs.pop('colour', 'g') |
| 35 | super().__init__(*args, **kwargs) |
| 36 | |
| 37 | if self.disable: |
| 38 | return |
| 39 | |
| 40 | warn("GUI is experimental/alpha", TqdmExperimentalWarning, stacklevel=2) |
| 41 | self.mpl = mpl |
| 42 | self.plt = plt |
| 43 | |
| 44 | # Remember if external environment uses toolbars |
| 45 | self.toolbar = self.mpl.rcParams['toolbar'] |
| 46 | self.mpl.rcParams['toolbar'] = 'None' |
| 47 | |
| 48 | self.mininterval = max(self.mininterval, 0.5) |
| 49 | self.fig, ax = plt.subplots(figsize=(9, 2.2)) |
| 50 | # self.fig.subplots_adjust(bottom=0.2) |
| 51 | total = self.__len__() # avoids TypeError on None #971 |
| 52 | if total is not None: |
| 53 | self.xdata = [] |
| 54 | self.ydata = [] |
| 55 | self.zdata = [] |
| 56 | else: |
| 57 | self.xdata = deque([]) |
| 58 | self.ydata = deque([]) |
| 59 | self.zdata = deque([]) |
| 60 | self.line1, = ax.plot(self.xdata, self.ydata, color='b') |
| 61 | self.line2, = ax.plot(self.xdata, self.zdata, color='k') |
| 62 | ax.set_ylim(0, 0.001) |
| 63 | if total is not None: |
| 64 | ax.set_xlim(0, 100) |
| 65 | ax.set_xlabel("percent") |
| 66 | self.fig.legend((self.line1, self.line2), ("cur", "est"), |
| 67 | loc='center right') |
| 68 | # progressbar |
| 69 | self.hspan = plt.axhspan(0, 0.001, xmin=0, xmax=0, color=colour) |
| 70 | else: |
| 71 | # ax.set_xlim(-60, 0) |
| 72 | ax.set_xlim(0, 60) |
| 73 | ax.invert_xaxis() |
| 74 | ax.set_xlabel("seconds") |
| 75 | ax.legend(("cur", "est"), loc='lower left') |
| 76 | ax.grid() |
| 77 | # ax.set_xlabel('seconds') |
| 78 | ax.set_ylabel((self.unit if self.unit else "it") + "/s") |
| 79 | if self.unit_scale: |
| 80 | plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) |
| 81 | ax.yaxis.get_offset_text().set_x(-0.15) |
| 82 | |
| 83 | # Remember if external environment is interactive |
| 84 | self.wasion = plt.isinteractive() |