在背景图片上绘制Seaborn热图 [英] Plotting seaborn heatmap on top of a background picture

查看:287
本文介绍了在背景图片上绘制Seaborn热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Jupyter中的seaborn创建一个热图,以显示选择某个坐标点的人数.我目前使用以下代码创建了热图

I am creating a heatmap through seaborn in Jupyter to display the amount of people that would choose a certain coordinate point. I currently have the heatmap created with the following code

cm = metrics.confusion_matrix(yVals, xVals)
fig, ax = plt.subplots(figsize=(10,10))
sns.heatmap(cm, annot=True, fmt="0.3f", linewidth=0.5, cbar=False,
              cmap="Reds", square=True, ax=ax)
plt.show()

我的问题是我如何在背景图像上绘制此热图,并使热图中的正方形更加接近0,以显示更多背景图像?还有一种方法可以将热图上的索引从1而不是0开始?

My questions are how could I plot this heatmap on top of a background image and to make the squares in the heatmap more transparent the closer to 0 they are to show the background image more? Also is there a way to start the indexes on the heatmap at 1 instead of 0?

如果需要查看图片的外观,这里也提供了指向图片的链接.

Here's a link to the picture as well if needed to see how it looks.

推荐答案

您还需要缩放/翻转图像,以便它们一起绘制,因为地图的分辨率可能比热图好得多.我们让Seaborn进行调整工作,然后在显示地图的imshow中进行匹配.

You also need to scale/flip the images so they plot together, because the map is probably much finer resolution than the heatmap. We let Seaborn do its adjustment work and then match it in imshow which displays the map.

您可以修改或创建一个颜色图以使透明度接近0,并且我在代码中向您展示了该方法,但是由于我无法在高温环境下阅读该图,因此得出的结果不是最理想的.如图所示,整个热图是半透明的.

You can modify or create a colormap to have transparency near 0, and I left the code in to show you how, but the resulting figure was suboptimal because I couldn't read the map under high-heat locations. As shown, the whole heatmap is translucent.

供读者阅读的左侧:更改刻度标记以引用地图坐标,而不是热图索引.

Left for the reader: change the tickmarks to refer to map coordinates, not heatmap indices.

# add alpha (transparency) to a colormap
import matplotlib.cm from matplotlib.colors 
import LinearSegmentedColormap 
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))

# modified colormap with changing alpha
al_winter = LinearSegmentedColormap('AlphaWinter', wd) 

# get the map image as an array so we can plot it 
import matplotlib.image as mpimg 
map_img = mpimg.imread('tunis.png') 

# making and plotting heatmap 
import numpy.random as random 
heatmap_data = random.rand(8,9) 

import seaborn as sns; sns.set()

hmax = sns.heatmap(heatmap_data,
            #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,
            )

# heatmap uses pcolormesh instead of imshow, so we can't pass through 
# extent as a kwarg, so we can't mmatch the heatmap to the map. Instead, 
# match the map to the heatmap:

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

from matplotlib.pyplot import show 
show()

这篇关于在背景图片上绘制Seaborn热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆