向海上热图图添加剖面线

时间:2019-03-21 16:24:20

标签: python matplotlib seaborn heatmap patch

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

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()

1 个答案:

答案 0 :(得分:1)

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

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()

enter image description here

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

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()

enter image description here