如何使用seaborn python绘制数据点的比例

2024-05-21 02:53:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用代码创建了一个绘图,结果如下所示:

代码:

%matplotlib inline
sns.histplot(x = 'time_class', hue = 'Det_poiResult',data = df, multiple="dodge", shrink=.8)

结果: enter image description here

我想转换此图以显示结果的比例

  1. 对于x上的每个类别,按比例计算色调(赢、输、抽)的结果是什么。(例如:在课堂闪电战中,赢取所有数据点的分数)
  2. 色调比例的结果是什么(通过将色调计数作为x上所有类的所有数据点的分数计算得出)

我应该修改我的数据框来计算比例结果,然后绘制它,还是有任何简单的方法可以使用内置类来实现这一点

预期结果-1

on Y-axis I have proportions computed for each category of X instead of counts. 

proportions computation:
for the blitz on X.
  win= count(wins)/count(blitz)
  loss= count(loss)/count(blitz)
  draw= count(draw)/count(blitz)....

预期结果-2

on Y-axis I have proportions computed as a fraction of datapoints in the entire dataset instead of counts. 

proportions computation:
for the blitz on X.
  win= count(wins)/count(all datapoints of df)
  loss= count(loss)/count(all datapoints of df)
  draw= count(draw)/count(all datapoints of df)....

Tags: ofthe数据dfforoncountall
1条回答
网友
1楼 · 发布于 2024-05-21 02:53:56

您可以使用pandas的groupby计算总和,并从中计算百分比。然后,sns.barplot可以创建绘图

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

df = pd.DataFrame({'time_class': np.random.choice(['blitz', 'rapid', 'bullit'], 5000, p=[.6, .1, .3]),
                   'Det_poiResult': np.random.choice(['win', 'loss', 'draw'], 5000, p=[.49, .48, .03])})
df_counts = df.groupby(['time_class', 'Det_poiResult']).size()

df_pcts1 = (df_counts.groupby(level=0).apply(lambda x: 100 * x / float(x.sum()))).to_frame(name="Percent").reset_index()
df_pcts2 = (df_counts.groupby(level=1).sum() * 100 / len(df)).to_frame(name="Percent").reset_index()
df_pcts2['time_class'] = "overall"

ax = sns.barplot(data=df_pcts1.append(df_pcts2), y='Percent',
                 x='time_class', order=['blitz', 'rapid', 'bullit', 'overall'],
                 hue='Det_poiResult', hue_order=['win', 'loss', 'draw'], palette=['dodgerblue', 'tomato', 'chartreuse'])
ax.legend(loc='upper left', bbox_to_anchor=(1.02, 1.02))
ax.axvline(2.5, ls=' ', color='0.4')
plt.tight_layout()
plt.show()

sns.barplot from aggregated dataframes

相关问题 更多 >