如何在 mplfinance 中设置刻度和标签颜色?

How do I set the tick and label colours in mplfinance?

我已经设法对我的 mplfinance 图表的大部分颜色进行了颜色协调,但我似乎无法弄清楚如何设置刻度和标签的颜色。

我目前有以下代码:

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)

这是我在 matplotlib 中的代码,通常用于执行此操作:

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)

shows the possible kwargs for mpf.plot but I cannot find anything to do this, or in the styling documentation for mplfinance的答案。

编辑: 使用 Mr.T 的解决方案,我还找到了 rcparam axes.labelcolor 并将其设置为我在字典中的 TEXT_COLOUR 。完整的解决方案:

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)

最终效果:

免责声明:我对mplfinance不是很熟悉,所以可能有更好的方法来解决这个问题。我认为维护 mplfinance 的 Daniel Goldfarb 会定期检查这些问题,并可能启发我们更好的方法。

我也没有找到定义这些图像方面的直接方法,不幸的是,mplfinance 没有 return 它创建的轴对象,因此我们无法追溯更改图像元素。但是,here 它表示您可以提供 rcparams 作为字典。 Matplotlib 使用 rcparams 生成用于标准或当前绘图的样式。如果我们这样做,我们可以像您在 matplotlib 代码中所做的那样修改某些图像元素:

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()

示例输出:

我在此示例代码中只包含一个 style_dic 字典,很容易找到 rcparams,我的印象是您无法使用此策略更改所有所需的图像方面。可以找到 rcparams 的列表 here in the matplotlib tutorials

由于 mplfinance 提供了样式功能,因此很容易从默认样式中 select 您喜欢的样式,并根据 selected 样式进行自定义。

  • 'mike' 样式设置
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'
            )

编辑

更改刻度和标签的颜色,这是您根据此样式选择的颜色。

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))