import pandas as pd
import datetime as dt
'# !pip install yfinance
import yfinance as yf
import plotly.graph_objects as go
import numpy as np
# Initial data & get dataframe
start = dt.date(2023, 4, 15)
end = dt.date(2023, 4, 21)
ticker = 'SPY'
df = yf.download(ticker, start, end, progress=False, interval='1m')
def calc_rangebreak(time_series: pd.Series):
""" Caculate the bounds of missing data
returns: list of dictionaries suitable for plotly rangebreak
"""
timedeltas = time_series.diff()
if len(time_series) < 2:
return []
# find index of the gaps where it is 50% greater than the median time interval
missing_times = np.where([timedeltas > timedeltas.median() * 1.5])[1]
# Tiny offset to account for numerical precision
off = pd.Timedelta(seconds=0.0001)
rb = [{'bounds': [str((time_series.iloc[t - 1] + off)), str((time_series.iloc[t] - off))]} for t in missing_times]
return rb
# Make Graph
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df.index,
y=df['Adj Close'],
mode='lines'))
rangebreak_bounds = calc_rangebreak(df.index.to_series())
fig.update_xaxes(rangebreaks=rangebreak_bounds)
fig.show()
this solution greatly help me solve issues with gaps in plotly charts,
i should say that i found this solution and it works as said,
thanks to every one doing the great job of helping each other
New contributor
AZLIN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.