0

I'm trying to put a heatmap over an image I have using seaborn and matplotlib, but the output doesn't show anything. I looked at the first answer of:

Plotting seaborn heatmap on top of a background picture

I took nearly the exact same code and nothing is showing I don't know why.

import matplotlib.image as mpimg 
import matplotlib.cm
import seaborn as sns
import matplotlib.pyplot as plt

wd = matplotlib.cm.winter._segmentdata # only has r,g,b  
wd['alpha'] =  ((0.0, 0.0, 0.3), 
               (0.3, 0.3, 1.0),
               (1.0, 1.0, 1.0))

al_winter = matplotlib.colors.LinearSegmentedColormap('AlphaWinter', wd) 

map_img = mpimg.imread('./my_picture.png') 

hmax = sns.heatmap(results,
            cmap = al_winter, # this worked but I didn't like it
            #cmap = matplotlib.cm.winter,
            alpha = 0.5, # whole heatmap is translucent
            annot = True,
            zorder = 2
            )

# The heatmap is working as I can see the output in my notebook.

hmax.imshow(map_img,
          aspect = hmax.get_aspect(),
          extent = hmax.get_xlim() + hmax.get_ylim(),
          zorder = 1) #put the map under the heatmap
plt.show()

# Here the output is empty.

The last output is empty and I don't know why, I tried to do hmax.imshow(map_img) and nothing is showing too except <matplotlib.image.AxesImage at 0x129888670>. Is this normal? I'm not sure if the problem comes from my picture, heatmap, or code.

victorfink
  • 343
  • 4
  • 17
  • I've experienced that sometimes `sns` refused to render on small figure sizes but ran fine on bigger figures. – Quang Hoang Dec 15 '20 at 18:09
  • So I should increase the size of my image? Or the size of my heatmap? – victorfink Dec 15 '20 at 18:33
  • Size of the heatmap. put, e.g. `plt.figure(figsize=(12,8))` before `hmax = sns.heatmap(...)`` – Quang Hoang Dec 15 '20 at 18:36
  • Thanks for your answer, I added the line you mentioned and saw on my notebook that the heatmap is indeed bigger, but still no output for the last command `hmax.imshow(...)`... – victorfink Dec 15 '20 at 20:44
  • Maybe `'./my_picture.png'` contains a bad image? Did you test displaying only the image (as in `plt.imshow(map_img)`)? What is `results.shape`? – JohanC Dec 15 '20 at 22:24
  • Maybe it comes from the image. `plt.imshow(map_img)` outputs my image correctly though. And `results.shape` outputs (8,5) – victorfink Dec 16 '20 at 07:28
  • UPDATE: I'm gonna try to save the heatmap as a png and then 'superimpose' the two files. Maybe that will work. – victorfink Dec 16 '20 at 09:12

1 Answers1

0

As mentioned in the comments, I saved the heatmap as a png and then 'superimposed' the two pictures using Image (PIL package). Doing it this way didn't cause me any trouble.

I still don't know why it didn't know work with the code in the question. Feel free to post an answer if you have a solution.

victorfink
  • 343
  • 4
  • 17