Beispiel #1
0
    def setlocators(self, data):
        ax = list(self.pinf.daxis.values())[-1]

        comp = getattr(data, '_compression', 1)
        tframe = getattr(data, '_timeframe', TimeFrame.Days)

        if tframe == TimeFrame.Years:
            fmtmajor = '%Y'
            fmtminor = '%Y'
            fmtdata = '%Y'
        elif tframe == TimeFrame.Months:
            fmtmajor = '%Y'
            fmtminor = '%b'
            fmtdata = '%b'
        elif tframe == TimeFrame.Weeks:
            fmtmajor = '%b'
            fmtminor = '%d'
            fmtdata = '%d'
        elif tframe == TimeFrame.Days:
            fmtmajor = '%b'
            fmtminor = '%d'
            fmtdata = '%Y-%m-%d'
        elif tframe == TimeFrame.Minutes:
            fmtmajor = '%d %b'
            fmtminor = '%H:%M'
            fmtdata = '%Y-%m-%d %H:%M'

        fordata = mdates.IndexDateFormatter(self.pinf.xreal, fmt=fmtdata)
        for dax in self.pinf.daxis.values():
            dax.fmt_xdata = fordata

        locmajor = mticker.AutoLocator()
        locminor = mticker.AutoMinorLocator()

        ax.xaxis.set_minor_locator(locminor)
        ax.xaxis.set_major_locator(locmajor)

        formajor = mdates.IndexDateFormatter(self.pinf.xreal, fmt=fmtmajor)
        forminor = mdates.IndexDateFormatter(self.pinf.xreal, fmt=fmtminor)

        ax.xaxis.set_minor_formatter(forminor)
        ax.xaxis.set_major_formatter(formajor)
Beispiel #2
0
    def plot(self, strategy, numfigs=1):
        if not strategy.datas:
            return

        self.pinf = PInfo(self.p.scheme)
        self.sortdataindicators(strategy)
        self.calcrows(strategy)

        slen = len(strategy)
        d, m = divmod(slen, numfigs)
        pranges = list()
        for i in range(numfigs):
            a = d * i
            if i == (numfigs - 1):
                d += m  # add remainder to last stint
            b = a + d

            pranges.append([a, b, d])

        for numfig in range(numfigs):
            # prepare a figure
            fig = self.pinf.newfig(numfig)

            self.pinf.pstart, self.pinf.pend, self.pinf.psize = pranges[numfig]
            self.pinf.xstart = self.pinf.pstart
            self.pinf.xend = self.pinf.pend

            self.pinf.clock = strategy._clock
            self.pinf.xreal = strategy._clock.datetime.plot(
                self.pinf.pstart, self.pinf.psize)
            self.pinf.xlen = len(self.pinf.xreal)
            self.pinf.x = list(range(self.pinf.xlen))

            # Do the plotting
            # Things that go always at the top (observers)
            for ptop in self.dplotstop:
                self.plotind(ptop, subinds=self.dplotsover[ptop])

            # Create the rest on a per data basis
            for data in strategy.datas:
                for ind in self.dplotsup[data]:
                    self.plotind(
                        ind,
                        subinds=self.dplotsover[ind],
                        upinds=self.dplotsup[ind],
                        downinds=self.dplotsdown[ind])

                self.plotdata(data, self.dplotsover[data])

                for ind in self.dplotsdown[data]:
                    self.plotind(
                        ind,
                        subinds=self.dplotsover[ind],
                        upinds=self.dplotsup[ind],
                        downinds=self.dplotsdown[ind])

            cursor = MultiCursor(
                fig.canvas, list(self.pinf.daxis.values()),
                useblit=True,
                horizOn=True, vertOn=True,
                horizMulti=False, vertMulti=True,
                horizShared=True, vertShared=False,
                color='black', lw=1, ls=':')

            self.pinf.cursors.append(cursor)

            lastax = list(self.pinf.daxis.values())[-1]
            # Date formatting for the x axis - only the last one needs it
            if False:
                locator = mticker.AutoLocator()
                lastax.xaxis.set_major_locator(locator)
                # lastax.xaxis.set_major_formatter(MyDateFormatter(self.pinf.xreal))
                formatter = mdates.IndexDateFormatter(self.pinf.xreal,
                                                      fmt='%Y-%m-%d')
                lastax.xaxis.set_major_formatter(formatter)
            else:
                self.setlocators(strategy._clock)

            # Put the subplots as indicated by hspace
            fig.subplots_adjust(hspace=self.pinf.sch.plotdist,
                                top=0.98, left=0.05, bottom=0.05, right=0.95)

            # Applying fig.autofmt_xdate if the data axis is the last one
            # breaks the presentation of the date labels. why?
            # Applying the manual rotation with setp cures the problem
            # but the labels from all axis but the last have to be hidden
            if False:
                fig.autofmt_xdate(bottom=0.25, rotation=0)
            elif True:
                for ax in self.pinf.daxis.values():
                    mpyplot.setp(ax.get_xticklabels(), visible=False)
                    # ax.autoscale_view(tight=True)
                mpyplot.setp(lastax.get_xticklabels(),
                             visible=True,
                             rotation=self.pinf.sch.tickrotation)

            # Things must be tight along the x axis (to fill both ends)
            axtight = 'x' if not self.pinf.sch.ytight else 'both'
            mpyplot.autoscale(enable=True, axis=axtight, tight=True)