1

I have managed to colour coordinate most of my mplfinance chart but I cannot seeem to figure out how to set the colour of the tick and labels. enter image description here

I currently have the following code:

market_colours = mpf.make_marketcolors(up="g", down="r", edge=BACKGROUND_COLOUR, wick=LINE_COLOUR)
style = mpf.make_mpf_style(marketcolors=market_colours, facecolor=BACKGROUND_COLOUR, edgecolor=LINE_COLOUR,
                           figcolor=BACKGROUND_COLOUR, gridcolor=LINE_COLOUR, gridstyle="--")

mpf.plot(df, type="candle", style=style)

This is my code in matplotlib to do this usually:

ax.xaxis.label.set_color(TEXT_COLOUR)
ax.yaxis.label.set_color(TEXT_COLOUR)
for axis in ["left"]:  # modify borders
    ax.spines[axis].set_color(LINE_COLOUR)
    ax.spines[axis].set_linewidth(3)
for axis in ["top", "right", "bottom"]:  # remove borders
    ax.spines[axis].set_linewidth(0)
for axis in ["x", "y"]:
    ax.tick_params(axis=axis, colors=LINE_COLOUR, which="both", width=2)

The answer to this post shows the possible kwargs for mpf.plot but I cannot find anything to do this, or in the styling documentation for mplfinance.

Edit: Using Mr.T's solution I found also the rcparam axes.labelcolor and set it to my TEXT_COLOUR in the dictionary. Complete solution:

market_colours = mpf.make_marketcolors(up="g", down="r",
                                       edge=BACKGROUND_COLOUR,
                                       wick=LINE_COLOUR)

STYLE_DICT = {"xtick.color": LINE_COLOUR,
              "ytick.color": LINE_COLOUR,
              "xtick.labelcolor": TEXT_COLOUR,
              "ytick.labelcolor": TEXT_COLOUR,
              "axes.spines.top": False,
              "axes.spines.right": False,
              "axes.labelcolor": TEXT_COLOUR}

style = mpf.make_mpf_style(marketcolors=market_colours,
                           facecolor=BACKGROUND_COLOUR,
                           edgecolor=LINE_COLOUR,
                           figcolor=BACKGROUND_COLOUR,
                           gridcolor=LINE_COLOUR,
                           gridstyle="--",
                           rc=STYLE_DICT)

mpf.plot(df, type="candle", style=style)

Final effect: enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54

2 Answers2

1

Disclaimer: I am not very familiar with mplfinance, so there might be better ways to address this problem. I think Daniel Goldfarb who maintains mplfinance checks these questions regularly and might enlighten us about better ways.

I also did not find a direct way to define these image aspects, and unfortunately, mplfinance does not return the axes objects it creates, so we cannot retrospectively change image elements. However, here it says that you can provide rcparams as a dictionary. Matplotlib uses rcparams to generate the style used for the standard or current plot. If we do this, we can modify certain image elements as you did in your matplotlib code:

import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
df = pd.read_csv('examples/data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
df.index.name = 'Date'

BACKGROUND_COLOUR="black"
LINE_COLOUR = "blue"
TEXT_COLOUR = "orange"

style_dic = {"xtick.color": LINE_COLOUR, 
             "ytick.color": LINE_COLOUR, 
             "xtick.labelcolor": TEXT_COLOUR, 
             "ytick.labelcolor": TEXT_COLOUR,
             "axes.spines.top": False, 
             "axes.spines.right": False}

market_colours = mpf.make_marketcolors(up="g", down="r", edge=BACKGROUND_COLOUR, wick=LINE_COLOUR)
style = mpf.make_mpf_style(marketcolors=market_colours, facecolor=BACKGROUND_COLOUR, edgecolor=LINE_COLOUR,
                           figcolor=BACKGROUND_COLOUR, gridcolor=LINE_COLOUR, gridstyle="--", rc=style_dic)

mpf.plot(df, type="candle", style=style)

plt.show()

Sample output: enter image description here

I have only included in this sample code a style_dic dictionary with easily found rcparams, and I have the impression that you will not be able to alter all desired image aspects with this strategy. A list of the rcparams can be found here in the matplotlib tutorials.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
0

Since mplfinance provides a style function, it is easy to select your favorite style from the default styles and customize it based on the selected style.

  • 'mike' Style settings
style = dict(style_name    = 'mike',
             base_mpl_style= 'dark_background', 
             marketcolors  = {'candle'  : {'up':'#000000', 'down':'#0080ff'},
                              'edge'    : {'up':'#ffffff', 'down':'#0080ff'},
                              'wick'    : {'up':'#ffffff', 'down':'#ffffff'},
                              'ohlc'    : {'up':'#ffffff', 'down':'#ffffff'},
                              'volume'  : {'up':'#7189aa', 'down':'#7189aa'},
                              'vcdopcod': False, # Volume Color Depends On Price Change On Day
                              'alpha'   : 1.0,
                             },
             mavcolors     = ['#ec009c','#78ff8f','#fcf120'],
             y_on_right    = True,
             gridcolor     = None,
             gridstyle     = None,
             facecolor     = None,
             rc            = [ ('axes.edgecolor'  , 'white'   ),
                               ('axes.linewidth'  ,  1.5      ),
                               ('axes.labelsize'  , 'large'   ),
                               ('axes.labelweight', 'semibold'),
                               ('axes.grid'       , True      ),
                               ('axes.grid.axis'  , 'both'    ),
                               ('axes.grid.which' , 'major'   ),
                               ('grid.alpha'      ,  0.9      ),
                               ('grid.color'      , '#b0b0b0' ),
                               ('grid.linestyle'  , '--'      ),
                               ('grid.linewidth'  ,  0.8      ),
                               ('figure.facecolor', '#0a0a0a' ),
                               ('patch.linewidth' ,  1.0      ),
                               ('lines.linewidth' ,  1.0      ),
                               ('font.weight'     , 'medium'  ),
                               ('font.size'       ,  10.0     ),
                               ('figure.titlesize', 'x-large' ),
                               ('figure.titleweight','semibold'),
                             ],
             base_mpf_style= 'mike'
            )

EDIT

Change the color of the ticks and labels, which is your choice based on this style.

import mplfinance as mpf
import yfinance as yf

df = yf.download("AAPL", start="2021-10-01", end="2021-12-31")

my_style = mpf.make_mpf_style(base_mpf_style='mike',
                              base_mpl_style='dark_background',
                              marketcolors=mpf.make_marketcolors(up='g',down='r',
                                                                 edge='white',
                                                                 wick={'up':'g','down':'r'},
                                                                 volume='gray',
                                                                 ohlc='white'),
                              gridcolor='white',
                              gridstyle="--",
                              rc={'xtick.color':'yellow',
                                  'ytick.color':'yellow',
                                  'axes.labelcolor':'yellow' 
                                 }
                             )

mpf.plot(df, type="candle", style=my_style, figratio=(9,6))

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • The aspect ratio of the graph can be set to your liking in `figratio`. Please try to create a style that you like. If my answers were helpful to you, please click the check mark next to the answer to accept the answer. – r-beginners Jan 22 '22 at 04:07