Issue
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.
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)
Solution
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()
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.
Answered By - Mr. T
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.