基于出现频率的 Seaborn 散点图大小

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

我正在尝试使用 Seaborn 库绘制数据,其中:

x 轴 - 电影发行年份
y 轴 - 电影评分(0-10,离散)

我现在正在使用散点图。我的数据在 Pandas 数据框中。

很明显,因为我的评分数据是离散整数,所以很多数据是相互叠加的。我怎样才能使每个点的大小与数据集中出现的频率成比例?

例如,如果 2008 年 6/10 评级的数量高于任何其他评级/年份组合,我希望该点大小(或图中的其他内容)表明这一点。

对于这样的事情我应该使用不同的情节吗?

python pandas matplotlib seaborn scatter-plot
1个回答
3
投票

对于这样的事情我应该使用不同的情节吗?

我建议将其可视化为评级年的

heatmap
crosstab

years = range(df['Release Year'].min(), df['Release Year'].max() + 1)
cross = pd.crosstab(df['IMDB Rating'], df['Release Year']).reindex(columns=years, fill_value=0)

fig, ax = plt.subplots(figsize=(30, 5))
sns.heatmap(cross, cbar_kws=dict(label='Count'), ax=ax)
ax.invert_yaxis()

但是,如果您仍然喜欢

scatterplot
气泡图,请通过
size
 设置 
groupby.size
参数:

counts = df.groupby(['Release Year', 'IMDB Rating']).size().reset_index(name='Count')

fig, ax = plt.subplots(figsize=(30, 5))
sns.scatterplot(data=counts, x='Release Year', y='IMDB Rating', size='Count', ax=ax)
ax.grid(axis='y')
sns.despine(left=True, bottom=True)


参考资料:

url = 'https://raw.githubusercontent.com/vega/vega/main/docs/data/movies.json'
df = pd.read_json(url)[['Title', 'Release Date', 'IMDB Rating']]

df['IMDB Rating'] = df['IMDB Rating'].round().astype('Int8')
df['Release Year'] = pd.to_datetime(df['Release Date']).dt.year
df = df.loc[df['Release Year'] <= 2010]
© www.soinside.com 2019 - 2024. All rights reserved.