简体   繁体   English

带有单列的Seaborn Heatmap

[英]Seaborn Heatmap with single column

I have a dataframe that has an index (words) and a single column (counts) for some lyrics. 我有一个数据框,有一个索引(单词)和一个列(计数)的一些歌词。 I am trying to create a heatmap based on the word counts. 我正在尝试根据单词计数创建一个热图。

    Cuenta
Que 179
La  145
Y   142
Me  113
No  108

I am trying to produce the heatmap like this: 我正在尝试生成这样的热图:

df1 = pd.DataFrame.from_dict([top50]).T
df1.columns = ['Cuenta']
df1.sort_values(['Cuenta'], ascending = False, inplace=True)

result = df1.pivot(index=df1.index, columns='Cuenta', values=df1.Cuenta.count)
sns.heatmap(result, annot=True, fmt="g", cmap='viridis')
plt.show()

But, it keeps throwing 'Index' object has no attribute 'levels' 但是,它一直在抛出'Index'对象没有属性'level'

Any ideas why this isn't working? 任何想法为什么这不起作用? I tried using the index or words as a separate column and still doesn't work. 我尝试使用索引或单词作为单独的列仍然无法正常工作。

The data is one-dimensional. 数据是一维的。 The counts are already present in the one (and only) column of the dataframe. 计数已经存在于数据帧的一个(且唯一的)列中。 There is no meaningless way to pivot this data. 没有毫无意义的方法来转移这些数据。

You would hence directly plot the dataframe as a heatmap. 因此,您可以直接将数据框绘制为热图。

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

df = pd.DataFrame({"Cuenta": [179,145,142,113,108]},
                  index=["Que", "La", "Y", "Me", "No"])

sns.heatmap(df, annot=True, fmt="g", cmap='viridis')

plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM