Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    How to save figures generated by cerebro.plot?

    General Code/Help
    2
    2
    2370
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      AliAskari last edited by

      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?

      Thanks

      1 Reply Last reply Reply Quote 0
      • O
        ovix last edited by ovix

        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)
            else:
                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)
                    figs.append(rfig)
        
                # this blocks code execution
                # plotter.show()
        
            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.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors