'Removing gaps between data on a Plotly graph

I was trying to make a graph plotting time series data on stock prices. However markets are only open 9:30-16:00 but the graph automatically plots times where the market isn't open. This leads to there being large lines on the graph between data points, this should be more apparent in the picture that I've attached. If anybody could help resolve this issue I'd appreciate it.

import pandas as pd
import datetime as dt
import yfinance as yf
import plotly.graph_objects as go

#Initial data & get dataframe 
start = dt.date(2022,3,1)
end = dt.date(2022,3,7)
ticker = 'SPY'
df = yf.download(ticker,start,end,progress=False,interval='1m')

#Make Graph
fig = go.Figure()
fig.add_trace(go.Scatter(
    x=df.index,
    y=df['Adj Close'],
    mode='lines'))
fig.show()

enter image description here



Solution 1:[1]

If you insert nans into the dataframe where there are lengthy time gaps, this will probably give what you want.

import pandas as pd
import datetime as dt
import yfinance as yf
import plotly.graph_objects as go
import numpy as np

#Initial data & get dataframe 
start = dt.date(2022,3,1)
end = dt.date(2022,3,7)
ticker = 'SPY'
df = yf.download(ticker,start,end,progress=False,interval='1m')

# Specify minimum time gap in nanoseconds. 
TIME_GAP = 60000000000

# get an index array where there is a time gap
gap_idx = np.where(np.diff(df.index.astype(int)) > TIME_GAP)[0]

df = df.reset_index()

# use numpy to insert nans
df = pd.DataFrame(
    columns = df.columns, 
    data = np.insert(df.values, gap_idx+1, values=np.nan, axis=0)
    )

df = df.set_index('Datetime')

#Make Graph
fig = go.Figure()
fig.add_trace(go.Scatter(
    x=df.index,
    y=df['Adj Close'],
    mode='lines'))
fig.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1