gpt4 book ai didi

python - 在 seaborn barplot 中绘制 value_counts()

转载 作者:太空狗 更新时间:2023-10-29 17:42:46 26 4
gpt4 key购买 nike

我无法在 seaborn 中获取条形图。这是我的可重现数据:

people = ['Hannah', 'Bethany', 'Kris', 'Alex', 'Earl', 'Lori']
reputation = ['awesome', 'cool', 'brilliant', 'meh', 'awesome', 'cool']
dictionary = dict(zip(people, reputation))
df = pd.DataFrame(dictionary.values(), dictionary.keys())
df = df.rename(columns={0:'reputation'})

然后我想得到一个条形图,显示不同声誉的值(value)计数。我试过:

sns.barplot(x = 'reputation', y = df['reputation'].value_counts(), data = df, ci = None)

sns.barplot(x = 'reputation', y = df['reputation'].value_counts().values, data = df, ci = None)

但两者都返回空白图。

知道我能做些什么吗?

最佳答案

在最新的seaborn中,可以使用countplot函数:

seaborn.countplot(x='reputation', data=df)

要用 barplot 来做,你需要这样的东西:

seaborn.barplot(x=df.reputation.value_counts().index, y=df.reputation.value_counts())

您不能将 'reputation' 作为列名传递给 x,同时还传递 y 中的计数。为 x 传递 'reputation' 将使用 df.reputationvalues(所有这些,而不仅仅是唯一的)作为 x 值,seaborn 无法将这些与计数对齐。因此,您需要将唯一值作为 x 传递,将计数作为 y 传递。但是您需要调用 value_counts 两次(或者对唯一值和计数进行一些其他排序)以确保它们正确匹配。

关于python - 在 seaborn barplot 中绘制 value_counts(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com