0
$\begingroup$

I have a simple dataset created using NLTK's FreqDist function, and have successfully converted the distribution to a pandas dataframe. When visualizing that dataframe using seaborn, I get a weird output on the plot itself.

fdist = FreqDist(words)
fdist = fdist.most_common(20)

# Create a dataframe representing occurences of the most common keywords
df_words = pd.DataFrame(data=fdist,columns=['word','occurences'])
print(df_words)

enter image description here

sns.displot(data=df_words,x='word',y='occurences')
plt.xticks(rotation=45,horizontalalignment='right');

enter image description here

My expectation is that the chart uses the numbers in the occurences column as the values on the y-axis, which it sort of does, but it doesn't format the chart like any histogram I've ever seen, and I cannot figure out how to correct the output. I've tried using sns.countplot instead, but it gives a different but equally unusable output.

$\endgroup$

1 Answer 1

1
$\begingroup$

This is because you have provided the aggregated data to the function instead of the non-aggregated data. See the following example:

import seaborn as sns
import pandas as pd

df = pd.DataFrame({"word": ["a", "a", "a", "b", "b", "c"]})

Using sns.displot(df, x="word") gives the following plot:

enter image description here

While using sns.displot(df["word"].value_counts().reset_index(), x="index", y="word") gives the following plot:

enter image description here

So you could either using the non-aggregated dataset in combination with sns.displot, or use sns.barplot with the aggregated dataset, which would give the following plot:

enter image description here

$\endgroup$
1
  • $\begingroup$ Using 'sns.barplot' gave the correct output. I didn't realize there was a distinction between 'barplot' and 'histplot'. Thanks for the solution! (I gave an upvote but karma limit won't let me actually give credit) $\endgroup$
    – NotHead
    Commented Feb 16 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.