Python 中的分组、百分比和条形图
grouping, percentage, and barchart in Python
我是 Python 的新手,我正在尝试绘制一个显示 winner_rank_status
百分比的条形图,并且在每个条形中,我想显示获胜者的百分比(颜色)。
我的数据集是这样的:
我写的代码:
Q3_df=games_df[['winner','winner_rank_status']]
Q3_df=Q3_df.groupby(['winner_rank_status','winner']).size().groupby(level=0).apply(lambda x: round(100*x/x.sum(),2))
Q3_df=Q3_df.unstack()
ax= Q3_df.plot(
kind='bar',
stacked=True,
figsize=(14,7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black','grey','white'],
edgecolor='black',
)
for c in ax.containers:
ax.bar_label(c, label_type='center',color='b')
这是我得到的结果:
这个结果是错误的,因为它显示所有类别的 100%!!!我需要显示每个类别(相等、较高、较低)的真实百分比,然后在每个类别中显示每种颜色的比例...
能否请您指导我如何实现它?
感谢您的帮助。
您可以为每组条形的标签指定不同的颜色。要获得所有 9 个值总和为 100 的百分比,您可以除以游戏总数:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
winner_options = ['black', 'draw', 'white']
rank_options = ['lower', 'equal', 'higher']
Q3_df = pd.DataFrame({'winner_rank_status': pd.Categorical(np.random.choice(rank_options, 1000, p=[.46, .07, .47]), rank_options),
'winner': pd.Categorical(np.random.choice(winner_options, 1000, p=[.51, .03, .46]), winner_options)})
Q3_rank_winner_df = Q3_df.groupby(['winner_rank_status', 'winner']).size().groupby(level=0).apply(
lambda x: np.round(100 * x / len(Q3_df), 2))
Q3_rank_winner_df = Q3_rank_winner_df.unstack()
ax = Q3_rank_winner_df.plot(
kind='bar',
stacked=True,
figsize=(14, 7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black', 'grey', 'white'],
edgecolor='black')
for bars, color in zip(ax.containers, ['skyblue', 'navy', 'darkblue']):
ax.bar_label(bars, label_type='center', color=color)
ax.legend(bbox_to_anchor=[1.01, 1.02], loc='upper left')
plt.tight_layout()
plt.show()
新要求有点混乱。可以在条形图的顶部添加每个等级的百分比:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
winner_options = ['black', 'draw', 'white']
rank_options = ['lower', 'equal', 'higher']
Q3_df = pd.DataFrame(
{'winner_rank_status': pd.Categorical(np.random.choice(rank_options, 1000, p=[.65, .05, .30]), rank_options),
'winner': pd.Categorical(np.random.choice(winner_options, 1000, p=[.46, .07, .47]), winner_options)})
Q3_rank_winner_df = Q3_df.groupby(['winner_rank_status', 'winner']).size().groupby(level=0).apply(
lambda x: np.round(100 * x / x.sum(), 2))
Q3_rank_winner_df = Q3_rank_winner_df.unstack()
ax = Q3_rank_winner_df.plot(
kind='bar',
stacked=True,
figsize=(14, 7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black', 'grey', 'white'],
edgecolor='black')
for bars, color in zip(ax.containers, ['skyblue', 'navy', 'darkblue']):
ax.bar_label(bars, label_type='center', color=color)
Q3_rank_df = Q3_df.groupby(['winner_rank_status']).size() * 100 / len(Q3_df)
for row, percent in enumerate(Q3_rank_df):
ax.text(row, 103, f'{percent:.02f} %', color='navy', ha='center', va='center')
ax.margins(y=0.08) # more space on top
ax.legend(bbox_to_anchor=[1.01, 1.02], loc='upper left')
plt.tight_layout()
plt.show()
我是 Python 的新手,我正在尝试绘制一个显示 winner_rank_status
百分比的条形图,并且在每个条形中,我想显示获胜者的百分比(颜色)。
我的数据集是这样的:
我写的代码:
Q3_df=games_df[['winner','winner_rank_status']]
Q3_df=Q3_df.groupby(['winner_rank_status','winner']).size().groupby(level=0).apply(lambda x: round(100*x/x.sum(),2))
Q3_df=Q3_df.unstack()
ax= Q3_df.plot(
kind='bar',
stacked=True,
figsize=(14,7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black','grey','white'],
edgecolor='black',
)
for c in ax.containers:
ax.bar_label(c, label_type='center',color='b')
这是我得到的结果:
这个结果是错误的,因为它显示所有类别的 100%!!!我需要显示每个类别(相等、较高、较低)的真实百分比,然后在每个类别中显示每种颜色的比例...
能否请您指导我如何实现它?
感谢您的帮助。
您可以为每组条形的标签指定不同的颜色。要获得所有 9 个值总和为 100 的百分比,您可以除以游戏总数:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
winner_options = ['black', 'draw', 'white']
rank_options = ['lower', 'equal', 'higher']
Q3_df = pd.DataFrame({'winner_rank_status': pd.Categorical(np.random.choice(rank_options, 1000, p=[.46, .07, .47]), rank_options),
'winner': pd.Categorical(np.random.choice(winner_options, 1000, p=[.51, .03, .46]), winner_options)})
Q3_rank_winner_df = Q3_df.groupby(['winner_rank_status', 'winner']).size().groupby(level=0).apply(
lambda x: np.round(100 * x / len(Q3_df), 2))
Q3_rank_winner_df = Q3_rank_winner_df.unstack()
ax = Q3_rank_winner_df.plot(
kind='bar',
stacked=True,
figsize=(14, 7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black', 'grey', 'white'],
edgecolor='black')
for bars, color in zip(ax.containers, ['skyblue', 'navy', 'darkblue']):
ax.bar_label(bars, label_type='center', color=color)
ax.legend(bbox_to_anchor=[1.01, 1.02], loc='upper left')
plt.tight_layout()
plt.show()
新要求有点混乱。可以在条形图的顶部添加每个等级的百分比:
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
winner_options = ['black', 'draw', 'white']
rank_options = ['lower', 'equal', 'higher']
Q3_df = pd.DataFrame(
{'winner_rank_status': pd.Categorical(np.random.choice(rank_options, 1000, p=[.65, .05, .30]), rank_options),
'winner': pd.Categorical(np.random.choice(winner_options, 1000, p=[.46, .07, .47]), winner_options)})
Q3_rank_winner_df = Q3_df.groupby(['winner_rank_status', 'winner']).size().groupby(level=0).apply(
lambda x: np.round(100 * x / x.sum(), 2))
Q3_rank_winner_df = Q3_rank_winner_df.unstack()
ax = Q3_rank_winner_df.plot(
kind='bar',
stacked=True,
figsize=(14, 7),
rot=0,
title='Effect of piece colour and winner rating status on the result',
color=['black', 'grey', 'white'],
edgecolor='black')
for bars, color in zip(ax.containers, ['skyblue', 'navy', 'darkblue']):
ax.bar_label(bars, label_type='center', color=color)
Q3_rank_df = Q3_df.groupby(['winner_rank_status']).size() * 100 / len(Q3_df)
for row, percent in enumerate(Q3_rank_df):
ax.text(row, 103, f'{percent:.02f} %', color='navy', ha='center', va='center')
ax.margins(y=0.08) # more space on top
ax.legend(bbox_to_anchor=[1.01, 1.02], loc='upper left')
plt.tight_layout()
plt.show()