[英]Plot a Seaborn heatmap over a background picture
我有以下问题。 我想 plot 我的热图覆盖图像。 请参阅下面的简化代码:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from PIL import Image
df = pd.DataFrame(np.array([[1, 2, 1], [4, 5, 1], [7, 8, 2], [8, 2, 3]]),
columns=['a', 'b', 'c'])
#make the df heatmap friendly
df_cnt = df.reset_index().pivot(index='a',
columns='b', values='c')
#missing values
df_cnt.fillna(0, inplace=True)
#load an image
my_image = Image.open("./image.png")
#plot a an image
plt.imshow(my_image, zorder = 0)
#Note: I can see the image correctly in my IDE
#plot a heatmap
h = sns.heatmap(df_cnt)
#Heatmap is displayed but it is not over the backroung image....
这里有什么问题? 是因为我的热图中 0 值的颜色是黑色吗?
我试着按照这里的答案,但它对我不起作用: Plotting seaborn heatmap on top of a background picture
您可以在您所指的答案中使用 mpl.image 来覆盖图像。 您需要确保指定热图的透明度。
import matplotlib.image as mpimg # add
h = sns.heatmap(df_cnt, alpha=0.1, zorder=2) # update
my_image = mpimg.imread("./image.png") # update
# update
h.imshow(my_image,
aspect=h.get_aspect(),
extent= h.get_xlim() + h.get_ylim(),
zorder=1)
plt.show() # add
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.