I am trying to add an arrow on a given date and price to mpf plot. To do this i have the following code:
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)
But it is producing the following error:
ValueError: x and y must be the same size
Could you please advise how can i resolve this.
Advertisement
Answer
If your ultimate goal is to add an arrow to the title of the question, you can add it in the way shown in @Daniel Goldfarb’s How to add value of hlines in y axis using mplfinance python. I used this answer to create a code that meets the end goal. As you can see in the answer, the way to do this is to get the axis and then add an annotation for that axis, where 31
is the date/time index and a[0]
is the closing price.
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()