For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How to save figures generated by cerebro.plot?

  • I am running my tests on a headless server and mostly it's my CI that is running the tests. I would like to save the figures, figures generated by cerebro.plot(), but it seems the functionality has been deprecated.

    I have searched in the forums and it seems one way is to dump the cerebro instance to disk and then reload it later and save the plot manually. It's a really dirty solution for this problem specially considering that this was supported at some point in the development of the application.

    Is there any other way I could save the figures?


  • I just unwrapped the plot() method so I can have better control over what's going on:

    def processPlots(self, cerebro, numfigs=1, iplot=True, start=None, end=None,
             width=16, height=9, dpi=300, tight=True, use=None, **kwargs):
        # if self._exactbars > 0:
        #     return
        from backtrader import plot
        if cerebro.p.oldsync:
            plotter = plot.Plot_OldSync(**kwargs)
            plotter = plot.Plot(**kwargs)
        figs = []
        for stratlist in cerebro.runstrats:
            for si, strat in enumerate(stratlist):
                rfig = plotter.plot(strat, figid=si * 100,
                                    numfigs=numfigs, iplot=iplot,
                                    start=start, end=end, use=use)
            # this blocks code execution
        for fig in figs:
            for f in fig:
                f.savefig('../../static/foo.pdf', bbox_inches='tight')
        return figs

    Just pass in cerebro instance and parameters as you normally would into the plot() function. Not sure why it breaks down figures into lists the way it does, probably to handle multiple plots, so gotta implement that.

Log in to reply