### Install Requirements
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/README.md
Install all necessary requirements for running the examples.
```bash
pip install -r requirements.txt
```
--------------------------------
### Install plotly-resampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/mkdocs/getting_started.md
Install the package using pip.
```bash
pip install plotly-resampler
```
--------------------------------
### Install Dependencies with Pip and Poetry
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Install poetry if using the venv option, then use 'poetry install --all-extras' to install all project dependencies.
```bash
pip install poetry # install poetry (if you do use the venv option)
```
```bash
poetry install --all-extras # install all the dependencies
```
--------------------------------
### Setup for Time-Series Annotation
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Imports necessary widgets and defines a dictionary mapping annotation types to their Plotly trace configurations. This setup is for enabling interactive time-series annotation.
```python
from ipywidgets import Dropdown
from IPython.display import display
```
```python
# Proxy for a label class
label_dict = {
"peak": {
"type": "marker",
"trace_index": 1,
"plt_kwargs": {
"mode": "markers", "marker_symbol": "star", "marker_size": 12, "marker_color": "red"
}
},
"through": {
"type": "marker",
"trace_index": 2,
"plt_kwargs": {
"mode": "markers", "marker_symbol": "cross", "marker_size": 12, "line_color": "orange"
}
},
"rise": {
"type": "x-range",
"plt_kwargs": {
"line_width": 0, "fillcolor": "green", "opacity": 0.3
}
},
"fall": {
"type": "x-range",
"plt_kwargs": {
"line_width": 0, "fillcolor": "purple", "opacity": 0.3
}
}
}
# Create a label selector
label_selector = Dropdown()
label_selector.options = list(label_dict.keys())
```
--------------------------------
### Run Tests with Make
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Execute 'make test' to run the project tests. Ensure Google Chrome is installed for selenium tests. Refer to the Makefile for more details.
```bash
make test
```
--------------------------------
### Install Plotly Resampler
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Install the plotly-resampler package using pip. Optional extras can be installed for persistent inline mode.
```bash
pip install plotly-resampler
# Optional extras:
pip install plotly-resampler[inline_persistent] # kaleido + flask_cors for persistent inline mode
```
--------------------------------
### Prepare DataFrame for LTTB Resampling (Alternative)
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Sets up a pandas Series with a timestamp index, similar to the previous example but for a different column.
```python
s = df['x']
s.index = df['ITime']
```
--------------------------------
### Initialize Empty FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Creates an empty FigureWidgetResampler instance. This is a starting point for building a figure programmatically.
```python
fig = FigureWidgetResampler()
```
--------------------------------
### Manual Mode Plotting with FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
This example demonstrates manual plotting using `FigureWidgetResampler`, which is similar to `FigureResampler` but intended for environments like Jupyter widgets. It shows adding traces with specific sample limits and updating the layout.
```python
# Same content as above, but using the FigureWidgetResampler class
fig = FigureWidgetResampler(go.Figure())
fig.add_trace(go.Scattergl(showlegend=True), hf_x=x, hf_y=noisy_sine, max_n_samples=200)
fig.add_trace(dict(x=x, y=noisy_sine + 1, name="sp1"), max_n_samples=2000)
fig.update_layout(height=400, template="plotly_dark")
```
--------------------------------
### Plot DataFrame with Plotly Backend and PNG Renderer
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
This example demonstrates plotting a DataFrame using the Plotly backend and then rendering the figure as a PNG image. It includes unregistering the plotly-resampler to clean up resources.
```python
if USE_PNG:
fig = df.plot(backend="plotly")
unregister_plotly_resampler()
go.Figure(fig).show(renderer="png", width=1000, height=500)
unregister_plotly_resampler()
```
--------------------------------
### Initialize FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Initializes a FigureWidgetResampler instance. This is the starting point for using Plotly-Resampler with FigureWidget.
```python
FigureWidgetResampler({
'data': [{'name': '[R] 0 …'
```
--------------------------------
### Add Multiple High-Frequency Traces
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Utilizes `add_traces` to add multiple traces simultaneously, mirroring `add_trace`'s signature. Supports per-trace lists for arguments like `max_n_samples`, `downsamplers`, and `gap_handlers`. This example shows adding two `Scattergl` traces with different downsampling configurations.
```python
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly_resampler import FigureResampler
from plotly_resampler.aggregation import MinMaxLTTB, EveryNthPoint
n = 1_000_000
x = np.arange(n, dtype=float)
y1 = np.sin(x / 1000)
y2 = np.cos(x / 1000)
fig = FigureResampler(make_subplots(rows=2, cols=1))
fig.add_traces(
[
go.Scattergl(name="sin"),
go.Scattergl(name="cos"),
],
# per-trace kwargs lists:
hf_x=[x, x],
hf_y=[y1, y2],
max_n_samples=[1000, 500],
downsamplers=[MinMaxLTTB(), EveryNthPoint()],
rows=[1, 2],
cols=[1, 1],
)
fig.show_dash(mode="inline")
```
--------------------------------
### Optimizing Datetime Data Rendering in Plotly-Resampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/mkdocs/FAQ.md
This example illustrates how to efficiently add datetime data to a plotly-resampler figure. Using `hf_x` and `hf_y` arguments in `add_trace` significantly speeds up rendering compared to directly passing datetime objects to the scatter trace.
```python
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from plotly_resampler import FigureResampler
# Create the dummy dataframe
y = np.arange(1_000_000)
x = pd.date_range(start="2020-01-01", periods=len(y), freq="1s")
# Create the plotly-resampler figure
fig = FigureResampler()
# fig.add_trace(go.Scatter(x=x, y=y)) # This is slow
fig.add_trace(go.Scatter(), hf_x=x, hf_y=y) # This is fast
# ... (add more traces, etc.)
```
--------------------------------
### Display Figure with Inline Persistent Mode
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Use `FigureResampler.show_dash` with `mode='inline_persistent'` to display a figure that retains a static image when the notebook kernel is disconnected. This requires `kaleido`, `flask-cors`, and `jupyter-dash` to be installed.
```python
fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(showlegend=True), hf_x=x, hf_y=noisy_sine, max_n_samples=900)
fig.update_layout(height=400, template="plotly_white")
# Displaying the figure -> instead of `show_dash`, use IPython's `display`
fig.show_dash(mode="inline_persistent")
```
--------------------------------
### Manually Wrap Figure with FigureResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/mkdocs/getting_started.md
Wrap a Plotly Figure with `FigureResampler` for dynamic aggregation and then use `show_dash` to display it. This method is suitable for Dash applications or environments where direct notebook interaction is not possible. Ensure you are in a notebook environment for this specific example.
```python
# NOTE: this example works in a notebook environment
import plotly.graph_objects as go; import numpy as np
from plotly_resampler import FigureResampler
x = np.arange(1_000_000)
sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000
fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(name='noisy sine', showlegend=True), hf_x=x, hf_y=sin)
fig.show_dash(mode='inline')
```
--------------------------------
### Format Code with Make
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Run 'make format' to format the code using black and ruff. Refer to the Makefile for more details.
```bash
make format
```
--------------------------------
### Create and Activate Python Environment with venv
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
As an alternative, create a Python environment using 'python -m venv venv' and activate it with 'source venv/bin/activate'.
```bash
python -m venv venv
```
```bash
source venv/bin/activate
```
--------------------------------
### Initialize HoloViews and Panel Extensions
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Initializes the Bokeh extension for HoloViews and the ipywidgets communication for Panel. These are required for interactive visualizations.
```python
hv.extension("bokeh")
pn.extension(comms='ipywidgets')
```
--------------------------------
### Unregister Plotly Resampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Ensures that Plotly Resampler is unregistered before proceeding. This is a setup step to ensure a clean state.
```python
from plotly_resampler import unregister_plotly_resampler
unregister_plotly_resampler()
```
--------------------------------
### Generate Documentation with Sphinx
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Navigate to 'docs/sphinx' and run 'sphinx-autogen -o _autosummary && make clean html' to regenerate the documentation. Ensure your Python environment is active.
```bash
sphinx-autogen -o _autosummary && make clean html
```
--------------------------------
### Create and Activate Python Environment with Poetry
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Use 'poetry shell' to create and activate a new Python environment for the project. This is the advised option for dependency management.
```bash
poetry shell
```
--------------------------------
### Wrap Figure with FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Wraps a Plotly figure created with make_subplots using FigureWidgetResampler. This enables efficient downsampling and interactive features within Jupyter environments. Requires ipywidgets to be installed.
```python
# Wrap a figure with FigureWidgetResampler
fw_fig = FigureWidgetResampler(make_subplots(rows=2, shared_xaxes=False))
fw_fig.add_trace(go.Scattergl(), hf_x=x, hf_y=y * 1.000003**x, row=1, col=1)
fw_fig.add_trace(go.Scattergl(), hf_x=x, hf_y=(y + 3) * 0.999997**x, row=2, col=1)
fw_fig.update_layout(height=500, showlegend=True)
# Output this wrapped instance in a cell
fw_fig
```
--------------------------------
### Generate Dummy Data
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Creates a large dataset with a time-based index and a noisy sine wave for demonstration purposes.
```python
# Some dummy data that will be used throughout the examples
n = 2_000_000
x = np.arange(n)
x_time = pd.date_range("2020-01-01", freq="1s", periods=len(x))
noisy_sine = (3 + np.sin(x / 2000) + np.random.randn(n) / 10) * x / (n / 4)
```
--------------------------------
### Check Linting with Make
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/CONTRIBUTING.md
Run 'make lint' to check the code linting using ruff. Refer to the Makefile for more details.
```bash
make lint
```
--------------------------------
### Creating and Displaying a FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Instantiate `FigureWidgetResampler` and add traces with `max_n_samples` to control downsampling. Use `IPython.display.display` for rendering. This approach is suitable for interactive environments.
```python
import plotly.graph_objects as go
from plotly_resampler import FigureWidgetResampler
from IPython.display import display
# Assuming 'x' and 'noisy_sine' are defined numpy arrays or similar
fig = FigureWidgetResampler(go.Figure())
fig.add_trace(go.Scattergl(showlegend=True), hf_x=x, hf_y=noisy_sine, max_n_samples=900)
fig.update_layout(height=400, template="plotly_dark")
# Displaying the figure
display(fig)
```
--------------------------------
### Loading and Preparing Data for Plotting
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Loads data from parquet files and prepares a noisy sine wave dataset. This data is then used for creating various traces in a Plotly-Resampler figure.
```python
# ------------ loading the data -----------
df_gusb = pd.read_parquet("data/df_gusb.parquet")
df_data_pc = pd.read_parquet("data/df_pc_test.parquet")
# Create a noisy sine
n = 110_000_0 # 0
x = np.arange(n)
noisy_sine = (3 + np.sin(x / 200_000) + np.random.randn(n) / 10) * x / 100_000
```
--------------------------------
### Interactive Datashader Visualization with HoloViews
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/mkdocs/FAQ.md
This snippet demonstrates how to create an interactive visualization using datashader and holoviews for large datasets. It requires importing several libraries including holoviews, datashader, numpy, pandas, and panel.
```python
from holoviews.operation.datashader import datashade
import datashader as ds
import holoviews as hv
import numpy as np
import pandas as pd
import panel as pn
hv.extension("bokeh")
pn.extension(comms='ipywidgets')
# Create the dummy dataframe
n = 1_000_000
x = np.arange(n)
noisy_sine = (np.sin(x / 3_000) + (np.random.randn(n) / 10)) * x / 5_000
df = pd.DataFrame(
{"ns": noisy_sine, "ns_abs": np.abs(noisy_sine)},
)
# Visualize interactively with datashader
opts = hv.opts.RGB(width=800, height=400)
ndoverlay = hv.NdOverlay({c:hv.Curve((df.index, df[c])) for c in df.columns})
datashade(ndoverlay, cnorm='linear', aggregator=ds.count(), line_width=3).opts(opts)
```
--------------------------------
### Manual Mode Plotting with FigureResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
This snippet illustrates manual plotting using `FigureResampler`. It shows how to wrap a `go.Figure` object and add traces with `hf_x` and `hf_y` for faster rendering, along with optional layout updates and displaying the figure.
```python
# 1. Wrap the figure with the FigureResampler class
fig = FigureResampler(go.Figure())
# Add the trace using the `hf_x` & `hf_y` for faster rendering
# (this does not impact the resampling speed once rendered)
fig.add_trace(go.Scattergl(showlegend=True), hf_x=x, hf_y=noisy_sine, max_n_samples=300)
# Note how a dict input is also valid and how different # of samples per trace are used
fig.add_trace(dict(x=x, y=noisy_sine + 1, name="sp1"), max_n_samples=2000)
# Optional: update the layout
fig.update_layout(height=400, template="plotly_dark")
# 2. Call show_dash
fig.show_dash(mode="inline")
if USE_PNG:
fig.show(renderer="png", width=1000, height=500)
```
--------------------------------
### FigureResampler with Overview Panel
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Enable an interactive overview panel below the main chart by setting `create_overview=True`. This panel allows for synchronized navigation between the overview and the main figure.
```python
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly_resampler import FigureResampler
x = np.arange(2_000_000)
y1 = np.sin(x / 500) + np.random.randn(len(x)) * 0.1
y2 = np.cos(x / 500) + np.random.randn(len(x)) * 0.1
fig = FigureResampler(
make_subplots(rows=2, cols=1, shared_xaxes=True),
create_overview=True,
overview_row_idxs=[0, 0], # use row 0 of each column for the overview
overview_kwargs={"height": 100},
)
fig.add_trace(go.Scattergl(name="channel 1"), hf_x=x, hf_y=y1, row=1, col=1)
fig.add_trace(go.Scattergl(name="channel 2"), hf_x=x, hf_y=y2, row=2, col=1)
fig.show_dash(mode="inline")
```
--------------------------------
### Configuring FigureResampler and Adding Traces
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Initializes a FigureResampler with a 2x2 subplot layout and sets the default number of shown samples. Adds traces for swimming pool occupancy and a generated sine wave, specifying hover text and downsampling methods.
```python
fig = FigureResampler(
make_subplots(
rows=2,
cols=2,
specs=[[{}, {}], [{"colspan": 2}, None]],
subplot_titles=("GUSB swimming pool", "Generated sine", "Power consumption"),
vertical_spacing=0.12,
),
default_n_shown_samples=1000,
verbose=False,
)
# ------------ swimming pool data -----------
df_gusb_pool = df_gusb["zwembad"].last("4D").dropna()
fig.add_trace(
go.Scattergl(
x=df_gusb_pool.index,
y=df_gusb_pool.astype("uint16"),
mode="markers",
marker_size=5,
name="occupancy",
# showlegend=False,
),
hf_hovertext="mean last hour: "
+ df_gusb_pool.rolling("1h").mean().astype(int).astype(str),
downsampler=EveryNthPoint(),
gap_handler=NoGapHandler(),
row=1,
col=1,
)
fig.update_yaxes(title_text="Occupancy", row=1, col=1)
# ----------------- generated sine -----------
fig.add_trace(
dict(name="sin", line_color="#26b2e0"),
hf_x=x,
hf_y=noisy_sine,
row=1,
col=2,
)
```
--------------------------------
### Create Datashader Canvas and Aggregate Data
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Initializes a Datashader Canvas with specified ranges and dimensions, then aggregates the time series data for each column using the canvas. This prepares the data for rendering.
```python
%%time
cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=300, plot_width=900)
aggs= OrderedDict((c, cvs.line(df, 'ITime', c)) for c in cols)
```
--------------------------------
### Create Skin Conductance Figure with Signal Quality
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Generates a FigureResampler object and adds skin conductance traces, SCR peaks, and signal quality indicators as background fills. The signal quality is visualized using different background colors to denote good and bad quality.
```python
fig = FigureResampler(
make_subplots(
rows=2,
cols=1,
specs=[[{"secondary_y": True}], [{}]],
shared_xaxes=True,
),
default_n_shown_samples=1_000,
verbose=False,
)
fig.update_layout(height=600, title="skin conductance example", title_x=0.5)
# -------------------------------- ROW 1 --------------------------------
# Add the skin conductance signals
for c in ["EDA", "EDA_lf_cleaned", "EDA_lf_cleaned_tonic"]:
fig.add_trace(go.Scattergl(name=c), hf_x=df_gsr.index, hf_y=df_gsr[c], row=1, col=1)
# Show the Skin Conductance Response Peaks (SCR)
df_peaks = df_gsr[df_gsr["SCR_Peaks_neurokit_reduced_acc"] == 1]
fig.add_trace(
trace=go.Scattergl(
x=df_peaks.index,
y=df_peaks["EDA_lf_cleaned"],
visible="legendonly",
mode="markers",
marker_symbol="cross",
marker_size=15,
marker_color="red",
name="SCR peaks",
),
# Set limit_to_view to true so that the peaks disappear when out-of view-range
# and thus not disturb the autoscale!!!
limit_to_view=True,
)
# Display the skin conductance Signal Quality as background
df_grouped = groupby_consecutive(df_gsr["EDA_SQI"])
df_grouped["EDA_SQI"] = df_grouped["EDA_SQI"].map(bool)
df_grouped["good_sqi"] = df_grouped["EDA_SQI"].map(int)
df_grouped["bad_sqi"] = (~df_grouped["EDA_SQI"]).map(int)
for sqi_col, col_or in [
("good_sqi", "#2ca02c"),
("bad_sqi", "#d62728"),
]:
fig.add_trace(
go.Scattergl(
x=df_grouped["start"],
y=df_grouped[sqi_col],
mode="lines",
line_width=0,
fill="tozeroy",
fillcolor=col_or,
opacity=0.1 if "good" in sqi_col else 0.2,
line_shape="hv",
name=sqi_col,
showlegend=False,
),
# The most important thing here is that we do not interleave gaps
# Additionally, the limit-to-view also ensures that the autoscale is not
# disturbed.
gap_handler=NoGapHandler(),
limit_to_view=True,
secondary_y=True,
)
# -------------------------------- ROW 2 --------------------------------
# show the phasic EDA component
fig.add_trace(
{"name": "EDA_Phasic", "visible": "legendonly", "type": "scattergl"},
hf_x=df_gsr.index,
hf_y=df_gsr["EDA_Phasic"],
row=2,
col=1,
)
fig.show_dash(mode="inline_persistent", port=9022)
if USE_PNG:
fig.show(renderer="png", width=1000, height=600)
```
--------------------------------
### Generate Sample Data
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Generates a large array of sample data points for x and y axes, suitable for demonstrating plotting with large datasets.
```python
n = 1_000_000 # the nbr of data points
x = np.arange(n)
y = np.sin(x / 200) + np.random.random(len(x)) / 10
```
--------------------------------
### Initialize FigureWidget with Verbose Output
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Initializes a FigureWidgetResampler with verbose output enabled and a dark theme. It adds two scatter traces to separate subplots, demonstrating downsampling for large datasets.
```python
fw_fig = FigureWidgetResampler(make_subplots(rows=2, shared_xaxes=False), verbose=True)
fw_fig.update_layout(template='plotly_dark', height=600)
fw_fig.add_trace(go.Scattergl(), hf_x=x, hf_y=y, row=1, col=1)
fw_fig.add_trace(go.Scattergl(), hf_x=x, hf_y=y, row=2, col=1)
fw_fig
```
--------------------------------
### Configure Downsampling and Display with FigureResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Adds multiple traces to a FigureResampler, each with a different `max_n_samples` setting and one using a custom `downsampler`. It also updates the layout and displays the figure using `show_dash` and optionally `show` with a PNG renderer.
```python
fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(showlegend=True), hf_x=x, hf_y=noisy_sine, max_n_samples=100)
fig.add_trace(dict(x=x, y=noisy_sine + 1, name="sp1"), max_n_samples=2000)
fig.add_trace(
dict(x=x, y=noisy_sine + 5, name="sp5"),
max_n_samples=300,
downsampler=EveryNthPoint(),
)
# Optional: update the layout
fig.update_layout(height=400, template="plotly_dark")
# 2. Call show_dash
fig.show_dash(mode="inline_persistent")
if USE_PNG:
fig.show(renderer="png", width=1000, height=500)
```
--------------------------------
### Generate Sample Data
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/other_examples.ipynb
Generates a large array of sample data for x and a corresponding sinusoidal signal with added noise for y.
```python
# Generate some data
x = np.arange(200_000)
sin = 3 + np.sin(x / 5000) + np.random.randn(len(x)) / 30
```
--------------------------------
### Logarithmic Aggregation Algorithm
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/README.md
Implementation of a logarithmic aggregation algorithm for logarithmic x-axes.
```python
from plotly_resampler.aggregation.abstract_aggregation import AbstractAggregation
class LogLTTB(AbstractAggregation):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def aggregate(self, x, y, **kwargs):
# Implementation of the logarithmic aggregation algorithm
# This is a placeholder and would contain the actual logic
return x, y
```
--------------------------------
### Add High-Frequency Data Trace
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Demonstrates adding a trace with high-frequency data using `hf_x` and `hf_y`. Specifies downsampling parameters like `max_n_samples`, `downsampler`, and `gap_handler` for efficient rendering. Additional high-frequency properties like `hf_text` and `hf_marker_color` can also be downsampled.
```python
import numpy as np
import plotly.graph_objects as go
from plotly_resampler import FigureResampler
from plotly_resampler.aggregation import LTTB
from plotly_resampler.gap_handling import MedDiffGapHandler
n = 5_000_000
ts = pd.date_range("2020-01-01", periods=n, freq="100ms")
values = np.random.randn(n).cumsum()
fig = FigureResampler()
fig.add_trace(
go.Scatter(name="price"), # empty trace — data goes via hf_* arguments
hf_x=ts,
hf_y=values,
max_n_samples=2000, # show at most 2000 samples in viewport
downsampler=LTTB(), # use LTTB for this trace
gap_handler=MedDiffGapHandler(), # insert NaN where gaps > 4.1 × median diff
limit_to_view=False, # True → store even if n < max_n_samples
)
# Additional high-frequency properties that are downsampled in sync with x/y:
labels = np.array(["A", "B", "C"] * (n // 3 + 1))[:n]
fig.add_trace(
go.Scatter(name="events"),
hf_x=np.arange(n, dtype=float),
hf_y=values,
hf_text=labels, # downsampled alongside x/y
hf_marker_color=np.abs(values), # per-point color, also downsampled
)
fig.show_dash(mode="external")
```
--------------------------------
### Initialize and Display FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Initializes a FigureWidgetResampler and adds a trace with high-frequency data. Displays the figure's data and the figure itself. Requires `plotly.graph_objects` and `plotly_resampler.FigureWidgetResampler`.
```python
fig = FigureWidgetResampler(go.Figure())
fig.add_trace(go.Scattergl(showlegend=True, name="noisy_sine"), hf_y=noisy_sine)
display(fig.hf_data)
display(fig)
```
--------------------------------
### Dynamically Adjust Scatter Data with FigureResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/mkdocs/getting_started.md
Demonstrates how to construct high-frequency data, add it to a FigureResampler, and then dynamically update the 'y' property of the trace's high-frequency data. This is useful for interactive data visualization where trace data needs to change after initial rendering.
```python
import plotly.graph_objects as go; import numpy as np
from plotly_resampler import FigureResampler
# Note: a FigureWidgetResampler can be used here as well
# Construct the hf-data
x = np.arange(1_000_000)
sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000
fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(name='noisy sine', showlegend=True), hf_x=x, hf_y=sin)
fig.show_dash(mode='inline')
# After some time -> update the hf_data y property of the trace
# As we only have 1 trace, this needs to be mapped
fig.hf_data[-1]['y'] = - sin ** 2
```
--------------------------------
### Prepare DataFrame for LTTB Resampling
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Sets up a pandas Series with a timestamp index for use with LTTB resampling.
```python
s = df['a']
s.index = df['ITime']
s.index.name = 'timestamp'
```
--------------------------------
### Prepare Data for Categorical and Float Series
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Creates a categorical pandas Series with 'a', 'b', and 'c' categories and a high-frequency float series with sine wave and noise. This data is used for demonstrating combined trace types.
```python
# Create a categorical series, with mostly a's, but a few sparse b's and c's
cats_list = np.array(list("aaaaaaaaaa" * 1000))
cats_list[np.random.choice(len(cats_list), 100, replace=False)] = "b"
cats_list[np.random.choice(len(cats_list), 50, replace=False)] = "c"
cat_series = pd.Series(
cats_list,
dtype="category",
)
_nb_samples = 30_000
x = np.arange(_nb_samples).astype(np.uint32)
y = np.sin(x / 300).astype(np.float32) + np.random.randn(_nb_samples) / 5
float_series = pd.Series(index=x, data=y)
```
--------------------------------
### Add Traces with Default and Logarithmic Downsamplers
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Adds two traces to a FigureResampler. The first uses the default downsampler, while the second utilizes a custom LogLTTB downsampler for log-spaced data. Configure axes to logarithmic scale.
```python
from example_utils.loglttb import LogLTTB
fr.add_trace(
go.Scattergl(marker_color=y / np.max(y), **scatter_kwargs),
hf_y=y,
max_n_samples=3000,
)
fr.add_trace(
go.Scattergl(marker_color=y / np.max(y), **scatter_kwargs),
hf_x=np.arange(n), # NOTE: logLTTB requires x to be passed
hf_y=y,
max_n_samples=3000,
downsampler=LogLTTB(),
row=2,
col=1,
)
fr.update_xaxes(type="log")
fr.update_yaxes(type="log")
if USE_PNG:
fr.show(renderer="png", width=1000, height=500)
else:
fr.show_dash(mode="inline_persistent")
```
--------------------------------
### Create FigureWidget for Annotation
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/figurewidget_example.ipynb
Constructs a FigureWidgetResampler instance and adds an initial trace. It then adds additional scatter traces based on the 'label_dict' for different annotation types (markers).
```python
# Construct the figure
fw_fig = FigureWidgetResampler()
fw_fig.add_trace(go.Scattergl(name="noisy sine", opacity=0.8), hf_x=x, hf_y=y)
for k, v in label_dict.items():
if v.get("type", "").lower() == 'marker':
fw_fig.add_trace(go.Scattergl(name=k, **v.get("plt_kwargs", {})))
```
--------------------------------
### Visualize Downsampled Data with Datashader
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Demonstrates the effect of downsampling by plotting only 10,000 data points out of 1,000,000 for the 'a' column. This highlights the loss of detail compared to plotting all points.
```python
mask = (df.index % 10) == 0
tf.shade(cvs.line(df[mask][['a','ITime']], 'ITime', 'a'))
```
--------------------------------
### Displaying a FigureWidgetResampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Use `IPython.display.display` to show a `FigureWidgetResampler` object. This is the recommended method for displaying figures in environments like Jupyter notebooks.
```python
from IPython.display import display
# Assuming 'fig' is a pre-configured FigureWidgetResampler object
display(fig)
```
--------------------------------
### Import Libraries for Datashader and Plotly-Resampler
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Imports necessary libraries including pandas, numpy, xarray, datashader, panel, holoviews, and plotly-resampler for data manipulation and visualization.
```python
import datetime
import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.transfer_functions as tf
from collections import OrderedDict
import panel as pn
import holoviews as hv
from holoviews.operation.datashader import datashade
from plotly_resampler import FigureResampler, EfficientLTTB
import plotly.graph_objects as go
```
--------------------------------
### Create Figure with Combined Trace Types
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Initializes a FigureResampler with a 2x2 subplot layout and adds a categorical scatter trace, two box plots for a float series, and a histogram. This demonstrates combining different trace types within a single figure.
```python
base_fig = make_subplots(
rows=2,
cols=2,
specs=[[{}, {}], [{"colspan": 2}, None]],
)
fig = FigureResampler(base_fig, default_n_shown_samples=1000, verbose=False)
fig.add_trace(
go.Scattergl(name="cat_series"),
hf_x=cat_series.index,
hf_y=cat_series,
row=1,
col=1,
hf_hovertext="text",
)
fig.add_trace(go.Box(x=float_series.values, name="float_series"), row=1, col=2)
fig.add_trace(go.Box(x=float_series.values**2, name="float_series**2"), row=1, col=2)
# add a not hf-trace
fig.add_trace(
go.Histogram(
x=float_series,
name="float_series",
),
row=2,
col=1,
)
fig.show_dash(mode="inline_persistent", port=9032)
if USE_PNG:
fig.show(renderer="png", width=1000, height=600)
```
--------------------------------
### Fast EveryNthPoint Aggregation
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Use EveryNthPoint for quick data downsampling when visual accuracy is less critical than speed. It selects every N-th data point.
```python
import numpy as np
import plotly.graph_objects as go
from plotly_resampler import FigureResampler
from plotly_resampler.aggregation import EveryNthPoint
x = np.arange(5_000_000, dtype=float)
y = np.random.randn(len(x))
fig = FigureResampler(
go.Figure(),
default_downsampler=EveryNthPoint(),
default_n_shown_samples=500,
)
fig.add_trace(go.Scattergl(name="every-nth"), hf_x=x, hf_y=y)
fig.show_dash(mode="inline")
```
--------------------------------
### Import Necessary Libraries
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Imports pandas, numpy, plotly, and specific components from plotly-resampler for data manipulation and plotting.
```python
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from helper import groupby_consecutive
import sys
sys.path.append("..\n")
from plotly_resampler import FigureResampler, FigureWidgetResampler, EveryNthPoint
from plotly_resampler.aggregation import NoGapHandler, MedDiffGapHandler
from plotly_resampler.aggregation import MinMaxLTTB
USE_PNG = True # Set to false to use dynamic plots
```
--------------------------------
### Gap Handling in Filled Area Plots
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Demonstrates different gap handling methods for filled area plots using `fill='tozeroy'`. Shows the effect of `fill_value=0` and `line_shape='vh'` on gap visualization.
```python
# construct a series in which we will induce some time-gaps
eda_series = pd.read_parquet("data/processed_gsr.parquet")["EDA"]
td_range = eda_series.index[-1] - eda_series.index[0]
# fmt: off
# we repeat the values 10 times to make it more high-frequeny
eda_series = pd.Series(np.repeat(eda_series.values, 10))
index= pd.date_range(start=eda_series.index[0], periods=len(eda_series), freq="25ms").values
# we will insert 4 (equidistant) gaps
for gap_idx in np.linspace(0, len(eda_series), 6)[1:-1]:
index[int(gap_idx):] += pd.Timedelta(seconds= td_range.total_seconds() / 4)
eda_series.index = index
# construct a figure with 3 subplots, each showing different gap handling methods for
# filled area plots
fr = FigureResampler(
make_subplots(
rows=3,
cols=1,
subplot_titles=[
"MedDiffGapHandler",
"MedDiffGapHandler - 0",
'MedDiffGapHandler - 0 - "vh"',
],
)
)
fr.add_trace(
go.Scattergl(fill="tozeroy", showlegend=False),
hf_x=eda_series.index, hf_y=eda_series.values,
)
fr.add_trace(
go.Scattergl(fill="tozeroy", showlegend=False),
hf_x=eda_series.index, hf_y=eda_series.values,
gap_handler=MedDiffGapHandler(fill_value=0),
row=2, col=1,
)
fr.add_trace(
go.Scattergl(fill="tozeroy", showlegend=False, line_shape='vh'),
hf_x=eda_series.index, hf_y=eda_series.values,
gap_handler=MedDiffGapHandler(fill_value=0),
row=3, col=1
)
if USE_PNG:
fr.show(renderer="png", width=1000, height=500)
else:
fr.show()
```
--------------------------------
### Create a Base Plotly Figure with Multiple Traces
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Constructs a standard Plotly figure with subplots and adds various traces for different datasets, including occupancy, generated sine wave, and power consumption. This serves as the base for conversion to FigureResampler.
```python
import pandas as pd
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df_gusb = pd.read_parquet(f"data/df_gusb.parquet")
df_data_pc = pd.read_parquet(f"data/df_pc_test.parquet")
n = 110_000
x = np.arange(n)
noisy_sine = (3 + np.sin(x / 2_000) + np.random.randn(n) / 10) * x / 2_000
# construct a normal figure object instead of a figureResample object
fig = make_subplots(
rows=2,
cols=2,
specs=[[{}, {}], [{"colspan": 2}, None]],
subplot_titles=("GUSB swimming pool", "Generated sine", "Power consumption"),
vertical_spacing=0.12,
)
# ------------ swimming pool data -----------
df_gusb_pool = df_gusb["zwembad"].last("4D").dropna()
fig.add_trace(
go.Scattergl(
x=df_gusb_pool.index,
y=df_gusb_pool, # .astype("uint16"),
mode="markers",
marker_size=5,
name="occupancy",
showlegend=True,
hovertext="mean last hour: "
+ df_gusb_pool.rolling("1h").mean().astype(int).astype(str),
),
row=1,
col=1,
)
fig.update_yaxes(title_text="Occupancy", row=1, col=1)
# ----------------- generated sine -----------
fig.add_trace(
go.Scattergl(name="sin", line_color="#26b2e0", x=x, y=noisy_sine),
row=1,
col=2,
)
# ------------- Power consumption data -------------
df_data_pc = df_data_pc.last("50D")
for i, c in enumerate(df_data_pc.columns):
fig.add_trace(
go.Scattergl(
name=f"room {i+1}",
x=df_data_pc.index,
y=df_data_pc[c],
),
row=2,
col=1,
)
fig.update_layout(height=600)
fig.update_yaxes(title_text="Watt-hour (Wh)", row=2, col=1)
fig.update_layout(
title="Plotly-Resampler demo - fig base",
title_x=0.5,
legend_traceorder="normal",
legend=dict(orientation="h", y=1.11, xanchor="left", x=0),
)
if USE_PNG:
fig.show(renderer="png", width=1200, height=800)
else:
fig.show()
```
--------------------------------
### Create Plotly-Resampler Figure with Multiple Traces
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/datashader.ipynb
Initializes a FigureResampler and adds traces for all columns defined in 'cols'. This demonstrates Plotly-Resampler's capability to handle multiple time series interactively.
```python
%%time
fr = FigureResampler(default_n_shown_samples=2_000)
for c in cols:
fr.add_trace(
go.Scattergl(name=c, marker_color='darkblue', opacity=.15, line_width=1),
hf_x=df.Time, hf_y=df[c]
)
fr.update_layout(template='plotly_white')
fr.show_dash(mode='inline', port=8048)
```
--------------------------------
### MinMaxLTTB aggregator
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
`MinMaxLTTB` is the default aggregation algorithm. For inputs larger than 10 M points it first pre-selects `minmax_ratio × n_out` representative points using the fast Rust MinMax algorithm (optionally in parallel), then passes that reduced set to LTTB. This combines the visual fidelity of LTTB with the speed of MinMax.
```APIDOC
## MinMaxLTTB aggregator
`MinMaxLTTB` is the default aggregation algorithm. For inputs larger than 10 M points it first pre-selects `minmax_ratio × n_out` representative points using the fast Rust MinMax algorithm (optionally in parallel), then passes that reduced set to LTTB. This combines the visual fidelity of LTTB with the speed of MinMax.
```python
import numpy as np
from plotly_resampler.aggregation import MinMaxLTTB
from plotly_resampler import FigureResampler
import plotly.graph_objects as go
# Use directly as a standalone aggregator
agg = MinMaxLTTB(minmax_ratio=4, nan_policy="omit", parallel=True)
x = np.linspace(0, 100, 50_000_000)
y = np.sin(x) + np.random.randn(len(x)) * 0.1
indices = agg.arg_downsample(x, y, n_out=1000)
x_agg, y_agg = x[indices], y[indices]
print(f"Reduced {len(y):,} \u2192 {len(y_agg)} samples")
# Use as per-trace downsampler inside FigureResampler
fig = FigureResampler(go.Figure(), default_downsampler=MinMaxLTTB(parallel=True))
fig.add_trace(go.Scattergl(name="huge signal"), hf_x=x, hf_y=y)
fig.show_dash(mode="inline")
```
```
--------------------------------
### Initialize FigureResampler with Subplots for Log Scaling
Source: https://github.com/predict-idlab/plotly-resampler/blob/main/examples/basic_example.ipynb
Initializes a FigureResampler with two subplots configured for log scaling on both x and y axes. It prepares scatter plot arguments for subsequent trace additions, utilizing a custom `loglTTB` downsampler.
```python
# construct the data
n = 100_000
y = np.abs(np.sin(np.arange(n) / 2_000) + np.random.randn(n) / 10)
fr = FigureResampler(
make_subplots(rows=2, cols=1, subplot_titles=["LTTB", "LTTB: log(x)-space"])
)
scatter_kwargs = dict(mode="lines+markers", line_width=0.2, showlegend=False)
```
--------------------------------
### Use FigureResampler with Dash
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Wrap a go.Figure with FigureResampler to enable dynamic resampling in a Dash web application. Pass high-frequency data via hf_x/hf_y for optimal performance. The server re-aggregates data on zoom/pan events.
```python
import numpy as np
import plotly.graph_objects as go
from plotly_resampler import FigureResampler
from plotly_resampler.aggregation import MinMaxLTTB, NoGapHandler
# Generate 2 M samples of two noisy sinusoids
x = np.arange(2_000_000)
noisy_sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000
# Wrap an empty go.Figure with FigureResampler
fig = FigureResampler(
go.Figure(),
default_n_shown_samples=1000, # max data points sent to browser per trace
default_downsampler=MinMaxLTTB(), # default aggregation algorithm
show_mean_aggregation_size=True, # show ~bin_size suffix in legend
create_overview=False, # set True to add a range-slider overview panel
verbose=False,
)
# Add traces — pass raw data via hf_x / hf_y for best performance
fig.add_trace(
go.Scattergl(name="signal_A"),
hf_x=x,
hf_y=noisy_sin,
max_n_samples=1500, # override global default for this trace
)
fig.add_trace(
go.Scattergl(name="signal_B"),
hf_x=x,
hf_y=noisy_sin * 1.000002**x,
downsampler=MinMaxLTTB(minmax_ratio=6, parallel=True),
gap_handler=NoGapHandler(), # disable gap detection for this trace
)
# Launch the Dash app (mode: "external" | "inline" | "inline_persistent" | "jupyterlab")
fig.show_dash(mode="inline", port=8050)
# Later — stop the server programmatically
fig.stop_server()
```
--------------------------------
### Replace Figure with Aggregation Preservation
Source: https://context7.com/predict-idlab/plotly-resampler/llms.txt
Use the `replace` method to swap the current figure's layout and traces with a new `go.Figure` instance. This operation preserves all existing aggregation configurations.
```python
import numpy as np
import plotly.graph_objects as go
from plotly_resampler import FigureResampler
x = np.arange(1_000_000)
fig = FigureResampler(go.Figure(), default_n_shown_samples=800)
fig.add_trace(go.Scattergl(name="v1"), hf_x=x, hf_y=np.sin(x / 200))
fig.show_dash(mode="inline")
# Later: swap to a completely new figure keeping the same aggregation settings
new_go_fig = go.Figure()
new_go_fig.add_trace(go.Scatter(x=x, y=np.cos(x / 200), name="v2"))
fig.replace(new_go_fig, convert_existing_traces=True)
```