Heatmap Plotting#

In this tutorial, we will demonstrate how to use the heatmap function from the pymovements.plotting module to create a heatmap of gaze data. The heatmap will show the distribution of gaze positions across the experiment screen, with color values indicating the time spent at each position in seconds.

Preparations#

We import pymovements as the alias pm for convenience.

[1]:
import pymovements as pm
/home/docs/checkouts/readthedocs.org/user_builds/pymovements/envs/v0.13.0/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Loading the Dataset#

Let’s start by downloading our ToyDataset and loading in its data:

[2]:
dataset = pm.Dataset('ToyDataset', path='data/ToyDataset')
dataset.download()
dataset.load()
Using already downloaded and verified file: data/ToyDataset/downloads/pymovements-toy-dataset.zip
Extracting pymovements-toy-dataset.zip to data/ToyDataset/raw
100%|██████████| 20/20 [00:00<00:00, 192.49it/s]
[2]:
<pymovements.dataset.dataset.Dataset at 0x7fd39c154d30>

After loading the dataset, we will transform the pixel coordinates to degrees.

[3]:
dataset.pix2deg()
100%|██████████| 20/20 [00:00<00:00, 705.46it/s]
[3]:
<pymovements.dataset.dataset.Dataset at 0x7fd39c154d30>

Now our available columns for each GazeDataFrame are the following. We will use the x_right_pos and y_right_pos columns for plotting. However, it is also possible to use the pixel data columns x_right_pix and y_right_pix.

[4]:
dataset.gaze[5].frame
[4]:
shape: (23_054, 7)
text_idpage_idtimex_right_pixy_right_pixx_right_posy_right_pos
i64i64f64f64f64f64f64
112.415266e6176.8140.2-11.420403-9.148145
112.415267e6176.7139.8-11.422806-9.157834
112.415268e6176.7139.3-11.422806-9.169943
112.415269e6176.6139.3-11.42521-9.169943
112.41527e6176.7139.3-11.422806-9.169943
112.415271e6176.8139.5-11.420403-9.1651
112.415272e6177.3139.8-11.408386-9.157834
112.415273e6177.8140.0-11.396367-9.15299
112.415274e6178.3140.0-11.384348-9.15299
112.415275e6178.3139.9-11.384348-9.155412
112.415276e6178.0140.2-11.39156-9.148145
112.415277e6177.7140.4-11.398771-9.143301
112.438308e6649.1633.70.2401353.033792
112.438309e6648.8633.90.2326313.038748
112.43831e6649.1634.10.2401353.043704
112.438311e6649.6634.20.2526423.046182
112.438312e6650.1634.10.2651493.043704
112.438313e6650.0634.00.2626483.041226
112.438314e6649.9633.90.2601463.038748
112.438315e6649.9633.90.2601463.038748
112.438316e6650.1633.70.2651493.033792
112.438317e6650.2633.50.267653.028836
112.438318e6650.0633.20.2626483.021402
112.438319e6649.7633.10.2551443.018924

Creating a Heatmap#

Let’s create a heatmap using the heatmap function from the pymovements library. We will use the default gridsize of 10x10 with interpolation and display the colorbar.

[5]:
figure = pm.plotting.heatmap(
    gaze=dataset.gaze[5],
    position_columns=('x_right_pix', 'y_right_pix'),
    origin='upper',
    show_cbar=True,
    cbar_label='Time [s]',
    title='Gaze Heatmap with Interpolation On',
    xlabel='X [pix]',
    ylabel='Y [pix]',
    gridsize=[10, 10]
)
../_images/tutorials_heatmap_11_0.png

To better understand the effect of the gridsize parameter on the heatmap, we can turn off the interpolation. By doing this, we can clearly visualize the individual bins used to calculate the heatmap. With interpolation turned off, the heatmap will display the raw bin values rather than a smoothed representation.

[6]:
figure = pm.plotting.heatmap(
    dataset.gaze[5],
    position_columns=('x_right_pix', 'y_right_pix'),
    origin='upper',
    show_cbar=True,
    cbar_label='Time [s]',
    title='Gaze Heatmap with Interpolation Off',
    xlabel='X [pix]',
    ylabel='Y [pix]',
    gridsize=[10, 10],
    interpolation='none'
)
../_images/tutorials_heatmap_13_0.png

Increasing the gridsize parameter results in a finer grid and more detailed heatmap representation. With a higher grid size, we divide the plot into smaller bins, which can capture more nuances in the data distribution

[7]:
figure = pm.plotting.heatmap(
    dataset.gaze[5],
    position_columns=('x_right_pix', 'y_right_pix'),
    origin='upper',
    show_cbar=True,
    cbar_label='Time [s]',
    title='Gaze Heatmap with Higher Grid Size',
    xlabel='X [pix]',
    ylabel='Y [pix]',
    gridsize=[25, 25]
)
../_images/tutorials_heatmap_15_0.png