Slartibartfast
Slartibartfast

Reputation: 1190

How to skip weekends and other gaps from MPL finance plot

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

Upvotes: 1

Views: 1222

Answers (3)

r-beginners
r-beginners

Reputation: 35155

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

Upvotes: 2

Corralien
Corralien

Reputation: 120429

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

Upvotes: 1

keramat
keramat

Reputation: 4543

Maybe a hack:

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

enter image description here

Upvotes: 2

Related Questions