1

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:

enter image description here

you will notice that the h-line doesn't start exactly from the markers, which is what I desire. Anyway, to fix this?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Rockoos
  • 37
  • 8

1 Answers1

2
ax1 = df.plot(y='Adj_close', figsize=(15, 6))
ax1.scatter(df1.index, df1.Adj_close, s=30, c='k')

xmax = df.index.max()
xmin = df.index.min()
  
ax1.hlines(y=df1.Adj_close, xmin=df1.index, xmax=xmax, color='m')

ax1.grid(True)

ax1.set_xlim(xmin, xmax)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158