在Seaborn热图图上添加阴影 [英] adding hatches to seaborn heatmap plot

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

问题描述

是否有一种方法可以在海洋热图中填充特定的单元",例如满足条件?我已经使用带掩码的数组和matplotlib pcolor进行了尝试,但事实证明,它孵化了错误的单元格.

Is there a way to hatch particular 'cells' in a seaborn heatmap, which e.g. fullfill a condition? I already tried it with masked arrays and matplotlib pcolor, but it turned out that it hatched the wrong cells.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
zm = np.ma.masked_less(flights.values, 200)
x= np.arange(0,12)
y= np.arange(0,12)
sns.heatmap(flights,linewidth=.1)
plt.pcolor(x, y, zm, hatch='//', alpha=0.)

plt.show()

推荐答案

我认为这个想法/策略是正确的.您只是没有使用正确的坐标.

I think the idea/strategy is correct. You just didn't use the correct coordinates.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
print(flights)
zm = np.ma.masked_less(flights.values, 200)
x= np.arange(len(flights.columns)+1)
y= np.arange(len(flights.index)+1)
sns.heatmap(flights,linewidth=.1)
plt.pcolor(x, y, zm, hatch='//', alpha=0.)

plt.show()

可能,一个人可能想要孵化单个单元格,而不是整个区域.这很难.一种解决方法是创建一个较小刻度线的网格并将其着色为白色以覆盖阴影线.

Potentially, one might want to hatch individual cells, instead of the complete area. This is hard. A wordaround is to create a grid of minor ticks and colorize it in white as to overlay the hatching.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams["axes.axisbelow"] = False

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")

zm = np.ma.masked_less(flights.values, 200)
x= np.arange(len(flights.columns)+1)
y= np.arange(len(flights.index)+1)

fig, ax = plt.subplots()
sns.heatmap(flights,linewidth=0, ax=ax)

ax.pcolor(x, y, zm, hatch='//', alpha=0.)

ax.set_xticks(np.arange(flights.shape[1]+1), minor=True)
ax.set_yticks(np.arange(flights.shape[1]+1), minor=True)
ax.grid(True, which="minor", color="w", linewidth=2)
ax.tick_params(which="minor", left=False, bottom=False)

plt.show()

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

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