如何将多个数据框列添加到基本的 mplfinance plot()

How to add multiple dataframe columns to the basic mplfinance plot()

import yfinance as yf
msft = yf.Ticker('MSFT')
data = msft.history(period='6mo')
import mplfinance as mpf

data['30 Day MA'] = data['Close'].rolling(window=20).mean()
data['30 Day STD'] = data['Close'].rolling(window=20).std()
data['Upper Band'] = data['30 Day MA'] + (data['30 Day STD'] * 2)
data['Lower Band'] = data['30 Day MA'] - (data['30 Day STD'] * 2)

apdict = (
        mpf.make_addplot(data['Upper Band'])
        , mpf.make_addplot(data['Lower Band'])
        )
mpf.plot(data, volume=True, addplot=apdict)

我试过上面的代码。但是我收到以下错误,我不明白哪里出了问题。谁能告诉我如何修复该错误?谢谢

>>> mpf.plot(data, volume=True, addplot=apdict)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mplfinance/plotting.py", line 293, in plot
    config = _process_kwargs(kwargs, _valid_plot_kwargs())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mplfinance/_arg_validators.py", line 292, in _process_kwargs
    raise TypeError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n    '+v)
TypeError: kwarg "addplot" validator returned False for value: "({'data': Date
2020-12-15           NaN
2020-12-16           NaN
2020-12-17           NaN
2020-12-18           NaN
2020-12-21           NaN
                 ...
2021-06-08    255.290610
2021-06-09    256.115131
2021-06-10    256.886620
2021-06-11    258.091432
2021-06-14    259.828890
Name: Upper Band, Length: 124, dtype: float64, 'scatter': False, 'type': 'line', 'mav': None, 'panel': 0, 'marker': 'o', 'markersize': 18, 'color': None, 'linestyle': None, 'width': None, 'bottom': 0, 'alpha': 1, 'secondary_y': 'auto', 'y_on_right': None, 'ylabel': None, 'ylim': None, 'title': None, 'ax': None, 'yscale': None, 'stepwhere': 'pre'}, {'data': Date
2020-12-15           NaN
2020-12-16           NaN
2020-12-17           NaN
2020-12-18           NaN
2020-12-21           NaN
                 ...
2021-06-08    239.371960
2021-06-09    239.340165
2021-06-10    240.447735
2021-06-11    240.784914
2021-06-14    240.278626
Name: Lower Band, Length: 124, dtype: float64, 'scatter': False, 'type': 'line', 'mav': None, 'panel': 0, 'marker': 'o', 'markersize': 18, 'color': None, 'linestyle': None, 'width': None, 'bottom': 0, 'alpha': 1, 'secondary_y': 'auto', 'y_on_right': None, 'ylabel': None, 'ylim': None, 'title': None, 'ax': None, 'yscale': None, 'stepwhere': 'pre'})"
    'Validator'   : lambda value: isinstance(value,dict) or (isinstance(value,list) and all([isinstance(d,dict) for d in value])) },
  • 根据 Adding plots to the basic mplfinance plot(),第 部分绘制多个附加数据集
    • 除了下面的示例,对于数据框中的两列,文档显示了多种为 addplot 参数配置适当对象的方法。
    • apdict = [mpf.make_addplot(data['Upper Band']), mpf.make_addplot(data['Lower Band'])] 也适用。请注意,它是 list,而不是 tuple
  • mplfinance/examples
# add multiple additional data sets
apdict = mpf.make_addplot(data[['Upper Band', 'Lower Band']])

# plot
mpf.plot(data, volume=True, addplot=apdict)