Resampling Data Using Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to downsample data using Matplotlib in Python. Downsampling is the process of reducing the sample rate or sample size of a signal. We will use a class that will downsample the data and recompute when zoomed.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} matplotlib/figures_axes -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} matplotlib/line_plots -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} matplotlib/event_handling -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/variables_data_types -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/booleans -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/conditional_statements -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/lists -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/tuples -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/sets -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/function_definition -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/importing_modules -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/classes_objects -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/constructor -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/polymorphism -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/encapsulation -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/numerical_computing -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/data_visualization -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} python/build_in_functions -.-> lab-48905{{"`Resampling Data Using Matplotlib`"}} end

Importing libraries

We will start by importing the necessary libraries. We will be using Matplotlib and NumPy libraries for this task.

import matplotlib.pyplot as plt
import numpy as np

Defining the class

We will define a class DataDisplayDownsampler that will downsample the data and recompute when zoomed. The constructor of the class will take the xdata and ydata as input parameters. We will set the maximum number of points to 50 and calculate the delta of xdata.

class DataDisplayDownsampler:
    def __init__(self, xdata, ydata):
        self.origYData = ydata
        self.origXData = xdata
        self.max_points = 50
        self.delta = xdata[-1] - xdata[0]

Downsampling data

We will define a method downsample that will downsample the data. The method will take the xstart and xend as input parameters. We will get the points in the view range and dilate the mask by one to catch the points just outside of the view range to not truncate the line. We will sort out how many points to drop and mask the data. Finally, we will downsample the data and return the xdata and ydata.

def downsample(self, xstart, xend):
    ## get the points in the view range
    mask = (self.origXData > xstart) & (self.origXData < xend)
    ## dilate the mask by one to catch the points just outside
    ## of the view range to not truncate the line
    mask = np.convolve([1, 1, 1], mask, mode='same').astype(bool)
    ## sort out how many points to drop
    ratio = max(np.sum(mask) // self.max_points, 1)

    ## mask data
    xdata = self.origXData[mask]
    ydata = self.origYData[mask]

    ## downsample data
    xdata = xdata[::ratio]
    ydata = ydata[::ratio]

    print(f"using {len(ydata)} of {np.sum(mask)} visible points")

    return xdata, ydata

Updating the data

We will define a method update that will update the data. The method will take the ax (axis) as an input parameter. We will update the line by getting the view limit and checking if the width of the view limit is different from delta. If the width of the view limit is different from delta, we will update the delta and get the xstart and xend. We will then set the data to the downsampled data and draw the idle.

def update(self, ax):
    ## Update the line
    lims = ax.viewLim
    if abs(lims.width - self.delta) > 1e-8:
        self.delta = lims.width
        xstart, xend = lims.intervalx
        self.line.set_data(*self.downsample(xstart, xend))
        ax.figure.canvas.draw_idle()

Creating the signal

We will create a signal using NumPy. We will create an array xdata using linspace function with start=16, stop=365 and num= (365-16)*4. We will create an array ydata using sin and cos functions.

xdata = np.linspace(16, 365, (365-16)*4)
ydata = np.sin(2*np.pi*xdata/153) + np.cos(2*np.pi*xdata/127)

Creating the plot

We will create a plot using Matplotlib. We will create an instance d of the DataDisplayDownsampler class using xdata and ydata. We will create a figure and an axis using subplots function. We will hook up the line and set the autoscale on False. We will connect for changing the view limits, set the x limit and show the plot.

d = DataDisplayDownsampler(xdata, ydata)
fig, ax = plt.subplots()
d.line, = ax.plot(xdata, ydata, 'o-')
ax.set_autoscale_on(False)
ax.callbacks.connect('xlim_changed', d.update)
ax.set_xlim(16, 365)
plt.show()

Summary

In this lab, we learned how to downsample data using Matplotlib in Python. We used a class that downsamples the data and recomputes when zoomed. We created a signal using NumPy and created a plot using Matplotlib. We connected for changing the view limits and set the x limit.

Other Python Tutorials you may like