向 mpf 财务图添加箭头
Adding arrows to mpf finance plots
我正在尝试在给定日期和价格上向 mpf 图添加箭头。为此,我有以下代码:
import pandas as pd
import yfinance as yf
import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd, mplfinance as mpf, matplotlib.pyplot as plt
db = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=7), end= datetime.datetime.now(), interval="5m")
db = db.dropna()
a = db['Close'][31:32]
test = mpf.make_addplot(a, type='scatter', markersize=200, marker='^')
mpf.plot(db, type='candle', style= 'charles', addplot=test)
但它产生了以下错误:
ValueError: x and y must be the same size
请问我该如何解决这个问题。
传入 mpf.make_addplot()
的数据必须与传入 mpf.plot()
的数据帧长度相同。要仅绘制一些点,其余点必须用 nan 值填充(float('nan')
,或 np.nan
)。
您可以在单元格 **In [7]**
中清楚地看到 in the documentation(并在以下单元格中使用)。看到那里生成信号数据的地方如下:
def percentB_belowzero(percentB,price):
import numpy as np
signal = []
previous = -1.0
for date,value in percentB.iteritems():
if value < 0 and previous >= 0:
signal.append(price[date]*0.99)
else:
signal.append(np.nan) # <- Make `nan` where no marker needed.
previous = value
return signal
注意:或者,可以通过首先初始化所有 nan 值,然后在您想要箭头的地方替换那些 nan 来生成信号数据:
signal = [float('nan')]*len(db)
signal[31] = db['Close'][31:32]
test = mpf.make_addplot(signal, type='scatter', markersize=200, marker='^')
...
如果您的最终目标是向问题的标题添加箭头,您可以按照@Daniel Goldfarb How to add value of hlines in y axis using mplfinance python 中所示的方式添加。我使用这个答案创建了一个符合最终目标的代码。正如您在答案中看到的那样,这样做的方法是获取轴,然后为该轴添加注释,其中 31
是 date/time 索引, a[0]
是结束价格。
import pandas as pd
import yfinance as yf
import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
db = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=7), end= datetime.datetime.now(), interval="5m")
db = db.dropna()
a = db['Close'][31:32]
#test = mpf.make_addplot(a, type='scatter', markersize=200, marker='^')
fig, axlist = mpf.plot(db, type='candle', style= 'charles', returnfig=True)#addplot=test
axlist[0].annotate('X', (31, a[0]), fontsize=20, xytext=(34, a[0]+20),
color='r',
arrowprops=dict(
arrowstyle='->',
facecolor='r',
edgecolor='r'))
mpf.show()
我正在尝试在给定日期和价格上向 mpf 图添加箭头。为此,我有以下代码:
import pandas as pd
import yfinance as yf
import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd, mplfinance as mpf, matplotlib.pyplot as plt
db = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=7), end= datetime.datetime.now(), interval="5m")
db = db.dropna()
a = db['Close'][31:32]
test = mpf.make_addplot(a, type='scatter', markersize=200, marker='^')
mpf.plot(db, type='candle', style= 'charles', addplot=test)
但它产生了以下错误:
ValueError: x and y must be the same size
请问我该如何解决这个问题。
传入 mpf.make_addplot()
的数据必须与传入 mpf.plot()
的数据帧长度相同。要仅绘制一些点,其余点必须用 nan 值填充(float('nan')
,或 np.nan
)。
您可以在单元格 **In [7]**
中清楚地看到 in the documentation(并在以下单元格中使用)。看到那里生成信号数据的地方如下:
def percentB_belowzero(percentB,price):
import numpy as np
signal = []
previous = -1.0
for date,value in percentB.iteritems():
if value < 0 and previous >= 0:
signal.append(price[date]*0.99)
else:
signal.append(np.nan) # <- Make `nan` where no marker needed.
previous = value
return signal
注意:或者,可以通过首先初始化所有 nan 值,然后在您想要箭头的地方替换那些 nan 来生成信号数据:
signal = [float('nan')]*len(db)
signal[31] = db['Close'][31:32]
test = mpf.make_addplot(signal, type='scatter', markersize=200, marker='^')
...
如果您的最终目标是向问题的标题添加箭头,您可以按照@Daniel Goldfarb How to add value of hlines in y axis using mplfinance python 中所示的方式添加。我使用这个答案创建了一个符合最终目标的代码。正如您在答案中看到的那样,这样做的方法是获取轴,然后为该轴添加注释,其中 31
是 date/time 索引, a[0]
是结束价格。
import pandas as pd
import yfinance as yf
import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import mplfinance as mpf
import matplotlib.pyplot as plt
db = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=7), end= datetime.datetime.now(), interval="5m")
db = db.dropna()
a = db['Close'][31:32]
#test = mpf.make_addplot(a, type='scatter', markersize=200, marker='^')
fig, axlist = mpf.plot(db, type='candle', style= 'charles', returnfig=True)#addplot=test
axlist[0].annotate('X', (31, a[0]), fontsize=20, xytext=(34, a[0]+20),
color='r',
arrowprops=dict(
arrowstyle='->',
facecolor='r',
edgecolor='r'))
mpf.show()