plotly-resampler icon indicating copy to clipboard operation
plotly-resampler copied to clipboard

Bug: Resampling not working when using xaxis type='date'

Open Jeremy38100 opened this issue 2 years ago • 2 comments

To improve performance I would like to use epoch timestamp in ms as x axis data, but show date in string format. When I use the parameter type="date" it breaks the resampler I can no longer zoom in the chart

Code to reproduce :

import pandas as pd
import plotly.graph_objs as go
from plotly_resampler import FigureResampler
import random
import datetime

# Generate data
count = 100000
df = pd.DataFrame({
    'dt': pd.date_range(start='2021-01-01', periods=count, freq='S'),
})
df['value'] = df['dt'].apply(lambda x: random.randint(1, 100))
df['timestamp'] = (df['dt'] - datetime.datetime(1970, 1, 1)) // datetime.timedelta(milliseconds=1)

fig = FigureResampler(go.Figure())

fig.add_trace(go.Scatter(x = df['timestamp'], y = df['value'], mode = 'lines'))

fig.update_xaxes(type="date")
fig.show_dash(mode='inline')


Jeremy38100 avatar Mar 31 '23 14:03 Jeremy38100

Hey @Jeremy38100,

Thx for submitting this issue together with the reproducible code! :+1:

I am able to reproduce this bug on my machine - I narrowed the underlying issue down to plotly-resampler internally using a stateful version of the xaxes type (but as you change this to type "date" - the internal value does not get changed & thus remains "linear" instead of "date")

I hope to solve this bug in the near future :)

jvdd avatar Apr 24 '23 14:04 jvdd

Hi @Jeremy38100,

I looked into this, and for me it doesn't make 100% sense why you would convert your timestamp (and possible timezone aware array) into unix ms- epochs?

You mentioned that you had performance issues. Can these be tackled by adding your trace data not via the go.Scatter / go.Scattergl its x and y properties, but via the hf_x and hhf_y properties, as shown below? This can be found aswell in the docs

Using 10M datapoints; and settign the data via hf_x and hf_y, I get the same type of visualization as above, and this runs <1s on a consumer pc.

import pandas as pd; import plotly.graph_objs as go; import numpy as np
from plotly_resampler import FigureResampler

# Generate data
count = 10_000_000
df = pd.DataFrame({"dt": pd.date_range(start="2021-01-01", periods=count, freq="S")})
df["value"] = np.random.randn(df.shape[0])

fig = FigureResampler(go.Figure())
fig.add_trace(go.Scatter(mode="lines"), hf_x=df["dt"], hf_y=df["value"])
fig

Please let me know if this helps you any further. Kind regards, Jonas

jonasvdd avatar May 11 '23 14:05 jonasvdd