【问题标题】:seaborn heatmap from pandas dataframe with NaNs来自带有 NaN 的 pandas 数据框的 seaborn 热图
【发布时间】:2019-05-16 21:27:27
【问题描述】:

您好,我真的很想创建一个热图,但我很挣扎:

# correlations between undergrad studies and occupation
data_uni = n.groupby(['Q5','Q6'])['Q6'].count().to_frame(name = 'count').reset_index()
# some participants did not answer the question in the survey
data_uni.fillna('Unknown', inplace=True)

data_uni.pivot(index='Q5', columns='Q6', values='count')
plt.figure(1, figsize=(14,10))
sns.heatmap(data_uni, cmap="YlGnBu")

我得到的错误信息是 "TypeError: ufunc 'isnan' not supported for the input types, and the input could not be safe to becovered to any supported types based on the cast rule ''safe''"。

这是创建热图的正确方法吗?如果是,我做错了什么,如果不是,正确的方法是什么?谢谢您的帮助!

【问题讨论】:

    标签: python pandas seaborn nan heatmap


    【解决方案1】:

    根据问题GH375,您可以指定一个掩码,其中不会显示掩码值为True 的单元格的数据。

    sns.heatmap(data_uni, cmap="YlGnBu", mask=data_uni.isnull())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2020-05-14
      • 2018-10-05
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多