使用 seaborn 绘制热图时出现 TypeError
TypeError when plotting heatmap with seaborn
我在 pandas DataFrame 中有以下数据集,我整理并保存到 "filename1.csv"
中:
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 将此 DataFrame 绘制为热图。一开始,看到两列的样本(每行一个样本)会很有趣,percent_a
和 percent_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 热图以显示这些分类值?
如果您也删除 "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()
我在 pandas DataFrame 中有以下数据集,我整理并保存到 "filename1.csv"
中:
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 将此 DataFrame 绘制为热图。一开始,看到两列的样本(每行一个样本)会很有趣,percent_a
和 percent_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 热图以显示这些分类值?
如果您也删除 "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()