Home > database >  Pandas Series of dates to vlines kwarg in mplfinance plot
Pandas Series of dates to vlines kwarg in mplfinance plot

Time:10-16

import numpy as np
import pandas as pd

df = pd.DataFrame({'dt': ['2021-2-13', '2022-2-15'],
                   'w': [5, 7],
                   'n': [11, 8]})
df.reset_index()
print(list(df.loc[:,'dt'].values))

gives: ['2021-2-13', '2022-2-15']

NEEDED: [('2021-2-13'), ('2022-2-15')]

Important (at comment's Q): "NEEDED" is the way "mplfinance" accepts vlines argument for plot (checked) - I need to draw vertical lines for specified dates at x-axis of chart

import mplfinance as mpf

RES['Date'] = RES['Date'].dt.strftime('%Y-%m-%d')
my_vlines=RES.loc[:,'Date'].values  # NOT WORKS

fig, axlist = mpf.plot( ohlc_df, type="candle",  vlines= my_vlines, xrotation=30, returnfig=True, figsize=(6,4))

will only work if explcit my_vlines= [('2022-01-18'), ('2022-02-25')]

SOLVED: Oh, it really appears to be so simple after all

my_vlines=list(RES.loc[:,'Date'].values) 

CodePudding user response:

Your question asks for a list of Numpy arrays but your desired output looks like Tuples. If you need Tuples, note that it's the comma that makes the tuple not the parentheses, so you'd do something like this:

desired_format = [(x,) for x in list(df.loc[:,'dt'].values)]

If you want numpy arrays, you could do this

desired_format = [np.array(x) for x in list(df.loc[:,'dt'].values)]

CodePudding user response:

I think I understand your problem. Please see the example code below and let me know if this resolves your problem. I expanded on your dataframe to meet mplfinance plot criteria.

import pandas as pd
import numpy as np
import mplfinance as mpf

df = pd.DataFrame({'dt': ['2021-2-13', '2022-2-15'],'Open': [5,7],'Close': [11, 8],'High': [21,30],'Low': [7, 3]})

df['dt']=pd.to_datetime(df['dt'])
df.set_index('dt', inplace = True)

mpf.plot(df, vlines = dict(vlines = df.index.tolist()))
  • Related