'Generating Heat map from scatter (x,y) data and then plot the same over an image using Python

WHAT I am trying to do: I am trying to create a heatmap from these data points (x,y) and then superimpose that over the image that I used originally to collect these data points.

Context: I had floated a small survey where I showed an image to the respondents and asked them to click on the best aspect of the image. In return i got the x,y data (where the x is % from left and y is % from top of the image). I renamed y as yInverse (since I am used to treating bottom left as the origin) and created another column y (100 - original y). My image size was 484 * 433 pixels. Since these 3 columns were in percentages, I multiplied each of x with 484 and yInverse and Y with 433 to arrive at click coordinates for each of the respondent.

Here is the image with the scatter data plotted on it. Here the data points are plotted correctly. Image with scatter data

Code of the above image:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np 
from scipy.ndimage.filters import gaussian_filter

mydf = pd.read_csv("C:/Users/anuyadav/Downloads/Data_Q12_220126/Q12_Text.csv")
mydf.head(2)

x = mydf['xactual']
yInverse = mydf['yInverseactual']
y = mydf['yactual']

im = plt.imread("C:/Users/anuyadav/Downloads/Data_Q12_220126/Edited_Doc.png", format = "png")
implot1 = plt.imshow(im,zorder=1,alpha=0.5,origin="upper")
plt.rcParams['figure.figsize'] = [10,10]
plt.scatter(x,yInverse,c="r",zorder=2,s=10,alpha=0.5)

Here is the image with the heatmap superimposed on it. Image with heatmap As you can see from the 2 images, the heatmap is not properly aligned with the scatter data. Any help on this would be appreciated!

Code:

im = plt.imread("C:/Users/anuyadav/Downloads/Data_Q12_220126/Original_Doc.png", format = "png")
im = im[::-1,:,:]
implot1 = plt.imshow(im,zorder=1,alpha=0.5)
x = mydf['xactual']
yInverse = mydf['yInverseactual']
y = mydf['yactual']
heatmap, xedges, yedges = np.histogram2d(x, yInverse, bins=(484,433))
heatmap = gaussian_filter(heatmap, sigma=16)

implot2 = plt.imshow(heatmap.T, extent=[0,484,0,433], cmap=cm.jet,zorder=2,alpha=.5)
plt.axis("off")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source