.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ts_utils/plot_rolling_stddev.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ts_utils_plot_rolling_stddev.py: ========================== Rolling standard deviation ========================== This example computes the rolling standard deviation of a single time series using a time-based window. .. GENERATED FROM PYTHON SOURCE LINES 12-48 .. image-sg:: /auto_examples/ts_utils/images/sphx_glr_plot_rolling_stddev_001.png :alt: Time series with rolling standard deviation :srcset: /auto_examples/ts_utils/images/sphx_glr_plot_rolling_stddev_001.png :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd from matplotlib.dates import DateFormatter from indsl.ts_utils.rolling_stats import rolling_stddev # Generate a synthetic time series rng = np.random.default_rng(12345) num_datapoints = 200 index = pd.date_range(start="1970-01-01", periods=num_datapoints, freq="1min") # Random walk with a small seasonal component y = np.cumsum(rng.standard_normal(num_datapoints)) + 0.1 * np.sin(np.linspace(0, 10 * np.pi, num_datapoints)) data = pd.Series(y, index=index) # Compute rolling standard deviation over a 5-minute window window = pd.Timedelta(minutes=5) std_series = rolling_stddev(data, time_window=window, min_periods=1) # Plot both on the same axes (overlaid) fig, ax = plt.subplots(figsize=[15, 6]) ax.plot(data, label="Time series") ax.plot(std_series, color="tab:orange", label="Rolling stddev (5 min window)") ax.set_title("Time series with rolling standard deviation") ax.legend(loc="best") # Formatting date_fmt = DateFormatter("%b %d, %H:%M") ax.xaxis.set_major_formatter(date_fmt) _ = plt.setp(ax.get_xticklabels(), rotation=45) plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.212 seconds) .. _sphx_glr_download_auto_examples_ts_utils_plot_rolling_stddev.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rolling_stddev.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rolling_stddev.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rolling_stddev.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_