Search code examples
pythonmatplotlibtouchdata-visualizationheatmap

Heat map visualizing touch input on smartphone (weighted 2d binning, histogram)


I have a dataset where each sample consists of x- and y-position, timestamp and a pressure value of touch input on a smartphone. I have uploaded the dataset here (OneDrive): data.csv

It can be read by:

import pandas as pd
df = pd.read_csv('data.csv')

Now, I would like to create a heat map visualizing the pressure distribution in the x-y space.

I envision a heat map which looks like the left or right image:

enter image description here

For a heat map of spatial positions a similar approach as given here could be used. For the heat map of pressure values the problem is that there are 3 dimensions, namely the x- and y-position and the pressure.

I'm happy about every input regarding the creation of the heat map.


Solution

  • There are several ways data can be binned. One is just by the number of events. Functions like numpy.histogram2d or hist2d allow to specify weights to each data point to manipulate the weight of each event.

    But there is a more general histogram function that might be useful in your case: scipy.stats.binned_statistic_2d

    By using the keyword argument statistic you can pick how the value of each bin is calculated from the values that lie within:

    • mean
    • std
    • median
    • count
    • sum
    • min
    • max
    • or a user defined function

    I guess in your case mean or median might be a good solution.