与Seaborn一起绘制热图时发生TypeError [英] TypeError when plotting heatmap with seaborn

查看:74
本文介绍了与Seaborn一起绘制热图时发生TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在经过整理并保存到"filename1.csv" 的pandas DataFrame中具有以下数据集:

I have the following dataset in a pandas DataFrame, which I've tidied and saved into "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 相对有趣:

I would like to plot this DataFrame as a heatmap using seaborn. At first, it would be interesting to see the samples (one sample per row) against two columns, percent_a and 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 个值.但是,它似乎是错误的,因为

I originally thought this meant that there were NaN values in this DataFrame. However, it appears wrong, as

df.isnull().values.any()

输出 False .因此,我怀疑这是因为 samples 是一列非数字值.

outputs False. So, I suspect this is because samples is a column of non-numerical values.

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

How do I plot a seaborn heatmap such that these categorical values are shown?

推荐答案

如果您也只删除"samples" 列,这不是您要查找的内容吗?!然后,您可以稍后使用matplotlib的 ax.set_yticklabels 函数输入样本名称.请注意,由于matplotlib从底部开始标记,因此您需要反转样品名称列表.

If you just drop the "samples" column as well, isn't that what you are looking for?! You can then put the sample names in later using matplotlib's ax.set_yticklabels function. Note that you need to reverse sample names list, since matplotlib starts the labeling from the bottom.

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()

这篇关于与Seaborn一起绘制热图时发生TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆