0

I tried to generate a heatmap using x y coordinates and overlay it on top of a png image. I have some challenges aligning the two images. The results I got was a normal sized heatmap but the base png image was very small. Could anyone please help align the images? My code is below:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import matplotlib.image as mpimg 

file = "CLP_Coordinates2.csv"
df = pd.read_csv(file)

map_img = mpimg.imread('Floor Plan.png')

fig, ax = plt.subplots(figsize=(10, 8))
ax.imshow(map_img, extent=[0.5, 8.0, 1.0, 7.0])

sns.kdeplot(data=df, x='x', y='y', cmap="Reds", shade=True, bw_method=.15, ax=ax)

plt.show()

The code I pasted was the closest results I got but the base image and the heatmap was misaligned.

Tranbi
  • 11,407
  • 6
  • 16
  • 33

1 Answers1

0

How did you choose the values for extent?

You probably need to adjust them to your dataframe:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import matplotlib.image as mpimg 

file = "CLP_Coordinates2.csv"
df = pd.read_csv(file)

map_img = mpimg.imread('Floor Plan.png')

fig, ax = plt.subplots(figsize=(10, 8))
ax.imshow(map_img, extent=[df.x.min(), df.x.max(), df.y.min(), df.y.max()])

sns.kdeplot(data=df, x='x', y='y', cmap="Reds", shade=True, bw_method=.15, ax=ax)

plt.show()

Note: you can add/subtract some offsets to make sure the image and heatmap are aligned

Tranbi
  • 11,407
  • 6
  • 16
  • 33