Seaborn Heatmap with single column

痞子三分冷 提交于 2020-05-29 08:24:05

问题


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'

Any ideas why this isn't working? I tried using the index or words as a separate column and still doesn't work.


回答1:


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



来源:https://stackoverflow.com/questions/47585775/seaborn-heatmap-with-single-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!