我尝试使用seaborn和matplotlib将热图放在我的图像上,但输出没有显示任何内容。我看了第一个答案:
Plotting seaborn heatmap on top of a background picture
我采用了几乎完全相同的代码,但没有显示任何内容,我不知道为什么。
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.
最后的输出是空的,我不知道为什么,我试着做hmax.imshow(map_img)
,但是除了<matplotlib.image.AxesImage at 0x129888670>
什么也没有显示。这是正常的吗?我不确定问题是来自我的图片、热图还是代码。
发布于 2020-12-16 13:33:15
正如评论中提到的,我将热图保存为png格式,然后使用Image (PIL包)将两张图片‘叠加’。这样做并没有给我带来任何麻烦。
我仍然不知道为什么它不知道如何使用问题中的代码。如果您有解决方案,请随时发布答案。
https://stackoverflow.com/questions/65311234
复制相似问题