从Pandas数据框的两列创建矩形热图

时间:2018-09-21 15:07:21

标签: python pandas matplotlib seaborn

我有一个这样的Pandas数据框-

enter image description here

“ s”列中的值是模型对“ k”和“ w”的相应值的准确性。因此,它严格在0到1之间。

我想绘制一个热图,以便创建一个7x2的网格,其中沿X轴的k(7个值),沿y轴的w(2个值),并且根据s的值对相应的单元进行着色。 / p>

我尝试了Seaborn的headmap函数,但是它不允许我定义用于着色网格的列。

1 个答案:

答案 0 :(得分:2)

尝试:

import matplotlib.pyplot as plt
import seaborn as sns

flights = df.pivot("w", "k", "s")
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True,  linewidths=.5, ax=ax)
plt.show()

enter image description here