I am trying to produce interactive plot using plotly resampler because of high data quantity. Without using the plotly-resampler wrapper plotly correctly recognizes the x axis as being date. With resampler it gets converted to some integer representation despite being datetime64[ns].
def plot_interactive_timeseries(
main_series: TimeSeries,
additional_series: dict = None,
title: str = "Interactive Time Series Plot",
yaxis_title: str = "Value"
):
"""
Plot interactive time series from darts.TimeSeries using Plotly.
Parameters:
-----------
main_series : TimeSeries
The primary time series to plot.
additional_series : dict, optional
Dictionary of additional TimeSeries to be plotted.
Keys are legend names, values are TimeSeries instances.
Example: {"Forecast": forecast_series, "True": true_series}
title : str
Plot title
yaxis_title : str
Y-axis label
Returns:
--------
fig : plotly.graph_objs.Figure
Interactive plotly figure.
"""
if additional_series is None:
additional_series = {}
# Convert main series to pandas DataFrame (time as index)
df_main = main_series.to_series()
# Initialize Plotly figure
fig = FigureWidgetResampler(go.Figure())
# Add main series trace
fig.add_trace(go.Scatter(
x= df_main.index,
y=df_main,
mode='lines+markers',
name='Main Series',
line=dict(width=2, color='#FD5108'),
marker=dict(size=5),
hovertemplate='Time: %{x}<br>Value:{y:.2f}<extra>Main</extra>'
))
# Add additional series
colors = ['red', 'green', 'orange', 'purple', 'brown', 'blue', 'magenta', 'lime']
for i, (label, series) in enumerate(additional_series.items()):
df = series.pd_dataframe()
# Make sure it has only one column - if multiple, just take the first.
y_col = df.columns[0]
fig.add_trace(go.Scatter(
x=df.index,
y=df[y_col],
mode='lines+markers',
name=label,
line=dict(width=2, color=colors[i % len(colors)]),
marker=dict(size=5),
hovertemplate='Time: %{x}<br>Value: %{y:.2f}<extra>{label}</extra>'.format(label)
))
# Update layout for professional look
fig.update_layout(
title=title,
xaxis_title='Time',
yaxis_title=yaxis_title,
hovermode='x unified',
legend=dict(
title="Series",
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
),
margin=dict(l=60, r=20, t=60, b=60),
template='plotly_white',
font=dict(family="Arial, sans-serif", size=12),
xaxis=dict(
showspikes=True,
spikemode='across',
spikesnap='cursor',
showline=True,
mirror=True,
linewidth=1,
linecolor='black',
gridcolor='lightgrey'
),
yaxis=dict(
showspikes=True,
spikemode='across',
spikesnap='cursor',
showline=True,
mirror=True,
linewidth=1,
linecolor='black',
gridcolor='lightgrey'
),
)
# Enable zoom, pan (enabled by default in Plotly)
fig.update_xaxes(rangeslider_visible=True)
return fig
This happen even with minimal working example where the behavior is exactly the same. When changing the xaxis type to date, the x axis is showing properly as date but there is no data even when sliding to the expected date range
I am trying to produce interactive plot using plotly resampler because of high data quantity. Without using the plotly-resampler wrapper plotly correctly recognizes the x axis as being date. With resampler it gets converted to some integer representation despite being datetime64[ns].
def plot_interactive_timeseries(
main_series: TimeSeries,
additional_series: dict = None,
title: str = "Interactive Time Series Plot",
yaxis_title: str = "Value"
):
"""
Plot interactive time series from darts.TimeSeries using Plotly.