我在熊猫中有DataFrame,想要绘制热图,但我有重复记录,而seaborn无法处理重复数据。我的数据框:
From To Distance
0 ABINGTON AMBLER 10.0
1 ABINGTON BERKS COUNTY 42.0
2 ABINGTON BRIDGEPORT 19.0
3 ABINGTON BRYN ATHYN 6.0
4 ABINGTON BUCKS COUNTY 19.0
5 ABINGTON CHELTENHAM 4.0
6 ABINGTON CHESTER COUNTY 38.0
7 ABINGTON COLLEGEVILLE 30.0
8 ABINGTON CONSHOHOCKEN 16.0
9 AMBLER BERKS COUNTY 32.0
10 AMBLER BRIDGEPORT 12.0
11 AMBLER BRYN ATHYN 14.0
12 AMBLER BUCKS COUNTY 11.0
13 AMBLER CHELTENHAM 11.0
14 AMBLER CHESTER COUNTY 29.0
我如何绘制热图?
答案 0 :(得分:2)
相反,重复数据"就像你提供的那样实际上很适合旋转,这基本上就是制作热图所需要的。除非我误解了你想要完成的事情,否则
df_piv = df.pivot('From', 'To', 'Distance')
ax = sns.heatmap(df_piv)
plt.xticks(rotation=20)
将为您提供数据热图,其中灰色框表示缺少值。
透视数据框看起来像这样,
In [14]: df_piv.T
Out[15]:
From ABINGTON AMBLER
To
AMBLER 10.0 NaN
BERKS COUNTY 42.0 32.0
BRIDGEPORT 19.0 12.0
BRYN ATHYN 6.0 14.0
BUCKS COUNTY 19.0 11.0
CHELTENHAM 4.0 11.0
CHESTER COUNTY 38.0 29.0
COLLEGEVILL 30.0 NaN
CONSHOHOCKEN 16.0 NaN