cwfmoore
cwfmoore

Reputation: 235

Matplotlib / Mplfinance - how to change colour of lines and bars

Been using Mplfinance and its great. I have been struggled with changing the colours of some of the subplots.

I successfully used the 'mpf.make_addplot' method from the docs using the external axes method. And I have read the docs regarding styles. But how do you apply a style to one of the sublots and not just the plot as a whole?

I have added my code and an image of the chart that it produces, the function is passed props with tha chart data.

I have labelled the image 'A' where there is a line I would like to change the colour of, this is the df['counts'] add plot, labelled 'Tweets'. Also the part labelled 'B', i would like to change the colour of the bar and if possible make it a different colour depending on it being positive or negative.

def makeCharts_CountsOHLC(self, props):

    fig = props['fig']
    df = props['df']
    symbol = props['symbol']
    start = props['min_date']
    end = props['max_date']

    # Configure the axes
    ax1 = fig.add_subplot(5,1,(1,2))
    ax2 = fig.add_subplot(5,1,3, sharex=ax1)
    ax3 = fig.add_subplot(5,1,4, sharex=ax1)
    ax4 = fig.add_subplot(5,1,5, sharex=ax1)

    # Create add plots
    aps = [
        mpf.make_addplot(
        df['count_s'], 
        ax=ax2, 
        ylabel='Tweets',
        ),
        
        mpf.make_addplot(
        df['sentiment'], 
        type='bar',
        ax=ax3,
        ylabel='Sentiment',            
        )]
    
    ax1.tick_params(labelbottom=False)
    ax2.tick_params(labelbottom=False)
    ax3.tick_params(labelbottom=False)

    # Functions to add a day to date and format dates 
    date_p1 = lambda x: dt.strftime(pd.to_datetime(x) + td(days=1), '%d %b %y')
    fmt_date = lambda x: dt.strftime(pd.to_datetime(x), '%d %b %y')

    title = f'{symbol} Price and Volume Traded from {fmt_date(start)} until {date_p1(end)}'

    # Plot the chart
    mpf.plot(
        df,
        type='candle',
        ax = ax1,
        volume = ax4,
        addplot = aps,
        xrotation = 0,
        datetime_format = '%b %d',
        axtitle = title,
        ylabel = 'Price ($USD)',
        tight_layout = True
        )

    # Format the prive of the y axis 
    mkfunc = lambda x, pos: '%1.1fM' % (x * 1e-6) if x >= 1e6 else '%1.1fK' % (x * 1e-3) if x >= 1e3 else '%1.1f' % x
    mkformatter = matplotlib.ticker.FuncFormatter(mkfunc)
    ax1.yaxis.set_major_formatter(mkformatter)

    # Adjustments to plot and save
    plt.subplots_adjust(hspace=0)
    plt.savefig(fname=props['save_path'], dpi=self.cconf.get('RESOLUTION'))
    plt.close('all')
    fig.clear()

Chart Plot

Upvotes: 1

Views: 4880

Answers (1)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

mpf.make_addplot() has a kwarg color= which you should help.

As a general rule, all of the available kwargs are listed in the code:

Upvotes: 2

Related Questions