简单的seaborn分布图不起作用

问题描述 投票:0回答:1

我试图绘制 Reddit 帖子上的分数分布,但无法弄清楚。

我的数据框是这样的

df = pd.DataFrame({"score": [12, 19, 25987, 887, 887, 1]},
                  {"author": ["xxx", "x", "xxx", "xx", "xxxx", "x"]})

有更多的数据点(约 300,000 个)。

我尝试了以下两件事:

plt.figure(figsize=(10, 6))
sns.displot(data=df, x="score", bins=30, kde=True)
plt.title('Distribution of Post Scores')
plt.xlabel('Score')
plt.ylabel('Frequency')

plt.show()

但它给了我以下内容:

我试过这个:

score_freq = df['score'].value_counts()

plt.figure(figsize=(10, 6))
sns.displot(score_freq, kind="kde", bw_adjust=30)
plt.title('Distribution of Post Scores')
plt.xlabel('Score')
plt.ylabel('Frequency')

plt.show()

这给了我这个:

所以第二种选择似乎更好一些,尽管仍然是错误的。我的分数没有一直到 -100,000,但我有一直到 299,489 的分数。

我真的不明白我做错了什么。

python plot statistics seaborn distribution
1个回答
0
投票

你可以尝试:

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

# Create a dataframe
df = pd.DataFrame({"score": [12, 19, 25987, 887, 887, 1],
                   "author": ["xxx", "x", "xxx", "xx", "xxxx", "x"]})

# Plotting using seaborn's displot
plt.figure(figsize=(10, 6))
sns.histplot(data=df, x="score", bins=30, kde=True)
plt.title('Distribution of Post Scores')
plt.xlabel('Score')
plt.ylabel('Frequency')

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.