2017-02-22 53 views
0

热图时,我有一个熊猫数据帧以下的数据集,我已经整理并保存到"filename1.csv"类型错误策划与seaborn

import pandas as pd 
df = pd.read_csv("filename1.csv") 
print(df) 

    samples a  b  c  percent_a percent_c ratio_a:b ratio_c:b 
0 sample1 185852 6509042 253303 0.028553 0.038916 35.022717 25.696664 
1 sample2 218178 6456571 273448 0.033792 0.042352 29.593135 23.611696 
2 sample3 251492 6353453 343252 0.039584 0.054026 25.263042 18.509588 
3 sample4 232299 6431376 284522 0.036120 0.044240 27.685767 22.604143 
.............................. 

我想绘制该数据帧作为使用seaborn热图。首先,它会看到(每行一个样品)的样品对两列,percent_apercent_c很有趣:

import numpy as np 
import seaborn as sns 
import matplotlib.pyplot as plt 
# drop unnecessary columns 
df = df.drop(["a", "b", "c", "ratio_a:b", "ratio_c:b"], axis = 1) 
sns.heatmap(df) 
plt.show() 

然而,这将引发一个错误:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs 
could not be safely coerced to any supported types according to the casting rule ''safe'' 

我本来以为这意味着此DataFrame中存在NaN值。然而,它看起来是错误的,因为

df.isnull().values.any() 

输出False。所以,我怀疑这是因为samples是一列非数值。

如何绘制seaborn热图以显示这些分类值?

回答

2

如果您只是删除"samples"列,是不是你在找什么?!然后,您可以使用matplotlib的ax.set_yticklabels函数将样品名称放入。请注意,您需要反转样本名称列表,因为matplotlib从底部开始标记。

import seaborn as sns 
import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.read_csv("SO_pandassnsheatmap.txt", delim_whitespace=True) 
df2 = df.drop(["samples", "a", "b", "c", "ratio_a:b", "ratio_c:b"], axis = 1) 
ax = sns.heatmap(df2) 
ax.set_yticklabels(df.samples.values[::-1]) 

plt.show() 

enter image description here

+0

是。但是,我希望y轴显示“样本”的名称,而不仅仅是索引0,1,2,3, 如何实现这一目标? – ShanZhengYang

+1

编辑答案。你是这个意思吗? – ImportanceOfBeingErnest

+0

是的,这就是我所困惑的。谢谢! – ShanZhengYang