在 seaborn barplot 中绘制 value_counts()
plotting value_counts() in seaborn barplot
我无法在 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'})
然后我想得到一个条形图,显示不同声誉的价值计数。我试过:
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)
但两个 return 空白图。
知道我能做些什么吗?
在最新的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.reputation
的 值 (所有这些,而不仅仅是唯一的)作为 x
值,而 seaborn 无法将这些与计数对齐。因此,您需要将唯一值作为 x
传递,将计数作为 y
传递。但是您需要调用 value_counts
两次(或对唯一值和计数进行一些其他排序)以确保它们正确匹配。
仅使用 countplot
您可以获得与 .value_counts()
输出相同顺序的条形图:
seaborn.countplot(data=df, x='reputation', order=df.reputation.value_counts().index)
我无法在 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'})
然后我想得到一个条形图,显示不同声誉的价值计数。我试过:
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)
但两个 return 空白图。
知道我能做些什么吗?
在最新的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.reputation
的 值 (所有这些,而不仅仅是唯一的)作为 x
值,而 seaborn 无法将这些与计数对齐。因此,您需要将唯一值作为 x
传递,将计数作为 y
传递。但是您需要调用 value_counts
两次(或对唯一值和计数进行一些其他排序)以确保它们正确匹配。
仅使用 countplot
您可以获得与 .value_counts()
输出相同顺序的条形图:
seaborn.countplot(data=df, x='reputation', order=df.reputation.value_counts().index)