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.9.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
100%|██████████| 20/20 [00:00<00:00, 207.69it/s]
[2]:
<pymovements.dataset.dataset.Dataset at 0x7f7fb58c7af0>

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

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

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: (23054, 7)
text_idpage_idtimex_right_pixy_right_pixy_right_posx_right_pos
i64i64f64f64f64f64f64
112.415266e6176.8140.2-12.297242-8.259494
112.415267e6176.7139.8-12.306793-8.261927
112.415268e6176.7139.3-12.318732-8.261927
112.415269e6176.6139.3-12.318732-8.264361
112.41527e6176.7139.3-12.318732-8.261927
112.415271e6176.8139.5-12.313957-8.259494
112.415272e6177.3139.8-12.306793-8.247325
112.415273e6177.8140.0-12.302018-8.235155
112.415274e6178.3140.0-12.302018-8.222985
112.415275e6178.3139.9-12.304406-8.222985
112.415276e6178.0140.2-12.297242-8.230287
112.415277e6177.7140.4-12.292466-8.237589
112.438308e6649.1633.7-0.1450823.415265
112.438309e6648.8633.9-0.1400793.407836
112.43831e6649.1634.1-0.1350773.415265
112.438311e6649.6634.2-0.1325753.427645
112.438312e6650.1634.1-0.1350773.440025
112.438313e6650.0634.0-0.1375783.437549
112.438314e6649.9633.9-0.1400793.435073
112.438315e6649.9633.9-0.1400793.435073
112.438316e6650.1633.7-0.1450823.440025
112.438317e6650.2633.5-0.1500853.442501
112.438318e6650.0633.2-0.1575893.437549
112.438319e6649.7633.1-0.1600913.430121

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