我认为这个想法/策略是正确的。你只是没有使用正确的坐标。
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()
您可能想要孵化单个单元格,而不是整个区域。这很难。一个解决方法是创建一个小刻度网格并将其着色为白色以覆盖阴影。
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()