Exemplo n.º 1
0
    def _plot_all(self):
        # def subplots(self, rows=2, figsize=(25, 10)):
        #     import matplotlib.pyplot as plt
        #     import matplotlib.dates as mdates
        #
        #     _, axes = plt.subplots(rows, 1,
        #                         sharex=True,
        #                         gridspec_kw={"height_ratios": [3, *([1] * (rows - 1))]},
        #                         figsize=figsize)
        #
        #     for ax in axes if isinstance(axes, Iterable) else [axes]:
        #         ax.xaxis_date()
        #         ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
        #
        #     return axes
        #
        # def plot(self, rows=2, cols=1, figsize=(18, 10), main_height_ratio=4):
        #     pass # return TaPlot(self.df, figsize, rows, cols, main_height_ratio)
        df = self.df
        hr = self.h_ratio
        wr = self.w_ratio
        mh = self.main_height
        rows = len(self.plots) + 1
        cols = 2 if self.plot_dist else 1

        height = mh  # + max(len(self.plots) - 1, 0) * (mh / sum(h_ratio) / h_ratio[1])
        grid_spec = {}

        if len(self.plots) > 1:
            grid_spec = {'height_ratios': [hr[0]] + [hr[1] for _ in range(1, len(self.plots))] + [1]}
        else:
            grid_spec = {'height_ratios': [hr[0], 1]}

        if self.plot_dist:
            grid_spec['width_ratios'] = wr

        # create subplots grid
        fig = plt.figure(figsize=(self.width, height), constrained_layout=True)
        gs = fig.add_gridspec(nrows=rows, ncols=cols, **grid_spec)
        sp = []

        for r in range(rows):
            if r == 0 or r >= rows - 1:
                shared_ax = [fig.add_subplot(gs[r, c]) for c in range(cols)]
                if isinstance(df.index, pd.DatetimeIndex):
                    shared_ax[0].xaxis_date()
                    shared_ax[0].xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))

                sp.append(shared_ax)
            else:
                sp.append([fig.add_subplot(gs[r, c], sharex=shared_ax[c]) if c == 0 else fig.add_subplot(gs[r, c]) for c in range(cols)])

        ax = np.array(sp).squeeze()
        self.fig = fig
        self.ax = ax

        # plot range slider
        range_ax = ax[-1, 0] if ax.ndim > 1 else ax[-1]
        range_ax.plot(df.index, df[self.range_slider_price].values)
        span_selector = SpanSelector(range_ax, onselect=self._plot_subplots, direction='horizontal',
                                     rectprops=dict(alpha=0.5, facecolor='red'), span_stays=True)

        self.widgets["selector"].append(span_selector)
        if self.cursor:
            self.widgets["cursor"].append(MultiCursor(fig.canvas, ax, horizOn=True, useblit=True, alpha=0.2))

        # initial span selection
        if self.subset is not None:
            sdf = df[self.subset].index
            xmin, xmax = sdf[0], sdf[-1]

            if isinstance(df.index, pd.DatetimeIndex):
                xmin, xmax = mdates.date2num(xmin), mdates.date2num(xmax)

            span_selector.stay_rect.set_bounds(xmin, 0, xmax - xmin, 1)
            span_selector.stay_rect.set_visible(True)
            span_selector.onselect(xmin, xmax)
        else:
            self._plot_subplots()

        # show figure now
        self.fig.show()