Home > Blockchain >  How to skip weekends and other gaps from MPL finance plot
How to skip weekends and other gaps from MPL finance plot

Time:02-16

I have the following code which plots intraday stocks data.

import pandas as pd, mplfinance as mpf, matplotlib.pyplot as plt
from pandas_datareader import data as web
import datetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
import yfinance as yf
yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=4), end= datetime.datetime.now(), interval="5m").plot(y="Close")

This produces the following plot as you can see it is also plotting a line through dates where no data is available. Is there anyway i can skip the days where data is not available?

enter image description here

As you can see

CodePudding user response:

Maybe a hack:

df.index = df.index.astype(str)
df['Close'].plot(rot = 45)

enter image description here

CodePudding user response:

Closed to @keramat answer:

ax = df.set_index(df.index.strftime('%m-%d %H')).plot(y='Close', rot=45)
plt.tight_layout()
plt.show()

enter image description here

CodePudding user response:

If you use MPL, it will automatically handle the x-axis time series.

df = yf.download(tickers='goog', start=datetime.datetime.now()-relativedelta(days=4), end=datetime.datetime.now(), interval="5m")
mpf.plot(df, figratio=(8,4), type='line', style='yahoo')

enter image description here

  • Related