I am trying to plot h-line along the low prices, here is what I achieved:
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf # pip install yfinance
# downloads data
df = yf.download('reliance.ns', '2019-01-01', '2021-01-01')
df.rename(columns= {'Adj Close': 'Adj_close'}, inplace= True)
def low_in(n= 30):
n = n * -1
all_lows = []
for x in range(1, len(df.index) + 1):
slice = df.Adj_close[n * x: n*x - n]
if len(slice) == -n:
low = min(slice.tolist())
all_lows.append(low)
else:
pass
return all_lows
x= low_in()
df1 = df[df.Adj_close.isin(x)]
df1 = df1[['Adj_close']]
fig, ax1 = plt.subplots(figsize= (15, 6))
ax1.plot(df.index, df.Adj_close)
ax1.scatter(df1.index, df1.Adj_close, s= 30, c= 'k')
for x in range(len(df1.Adj_close)):
xmin = x / len(df1.index)
ax1.axhline(df1.Adj_close[x], xmin= xmin, c= 'm')
ax1.grid(True)
ax1.set_xlim(df.index[0], df.index[-1])
fig.autofmt_xdate()
fig.tight_layout()
This is the result I get:
you will notice that the h-line doesn't start exactly from the markers, which is what I desire. Anyway, to fix this?