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 output plots in a dark theme?



  • Hey guys, I'm new to backtrader, anyone knows how to plot output with a dark background? I need it because I'm building a Telegram Bot and I wish the output would be black on background.

    Thanks in advance.

    Arthur



  • This gave me decent dark background plot without adding too much code.

    import matplotlib.pyplot as plt
    
    # run backtest...
    
    plt.style.use('dark_background') # call before plotting
    
    cerebro.plot(
        loc='grey', # changes color for 'line on close' plot otherwise it will plot black on black
        grid=False # the default gridlines didn't look good w/ dark background
    )
    
    fig = plt.gcf()
    # this will make the value tags at end of the lines readable. Otherwise it will plot white on white
    for ax in fig.axes:
        for text in ax.texts:
            text.set_color('black')
    
    plt.show()
    

    Backtrader plots using matplotlib so if you're familiar with it you can always plot with cerebro.plot(), get the current figure with fig = plt.gcf(), and alter the figure's elements from there.



  • This is my whole dark theme, I´ve tried to emulate TradingView schema.

    plt.style.use('fivethirtyeight')
    
    plt.rcParams["figure.figsize"] = (10, 6)
    plt.rcParams['lines.linewidth'] = 1
    
    SIZE = 7
    plt.rcParams['axes.labelsize'] = SIZE
    plt.rcParams['ytick.labelsize'] = SIZE
    plt.rcParams['xtick.labelsize'] = SIZE
    plt.rcParams["font.size"] = SIZE
    
    COLOR = '1'
    plt.rcParams['text.color'] = COLOR
    plt.rcParams['axes.labelcolor'] = COLOR
    plt.rcParams['xtick.color'] = COLOR
    plt.rcParams['ytick.color'] = COLOR
    
    
    plt.rcParams['grid.linewidth']=0.1
    plt.rcParams['grid.color']="0.2"
    plt.rcParams['lines.color']="0.5"
    plt.rcParams['axes.edgecolor']="0.2"
    plt.rcParams['axes.linewidth']=0.5
    
    plt.rcParams['figure.facecolor']="#101622"
    plt.rcParams['axes.facecolor']="#101622"
    plt.rcParams["savefig.dpi"]=120
    dpi = plt.rcParams["savefig.dpi"]
    width = 700
    height = 1200
    plt.rcParams['figure.figsize'] = height/dpi, width/dpi
    plt.rcParams["savefig.facecolor"] ="#101622"
    plt.rcParams["savefig.edgecolor"]="#101622"
    
    plt.rcParams['legend.fontsize'] = SIZE
    plt.rcParams['legend.title_fontsize'] = SIZE + 1
    plt.rcParams['legend.labelspacing'] =0.25
    plt.rcParams['image.cmap']='tab10'

Log in to reply
 

});