2017-02-22 40 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