说我下面有一个示例数据框:
Division Home Corners Away Corners
Bundesliga 5 3
Bundesliga 5 5
EPL 7 4
EPL 3 2
League 1 10 6
Serie A 3 3
Serie A 8 2
League 1 3 1
我想创建一个将每个游戏按总分划分的总角球的箱线图,但是我希望主角球和远角球分开但在同一图上。类似于“ hue”关键字完成的操作,但是我该如何完成?
答案 0 :(得分:1)
seaborn.boxplot
pandas.DataFrame.stack
将数据整形为长格式import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = {'Division': ['Bundesliga', 'Bundesliga', 'EPL', 'EPL', 'League 1', 'Serie A', 'Serie A', 'League 1'],
'Home Corners': [5, 5, 7, 3, 10, 3, 8, 3],
'Away Corners ': [3, 5, 4, 2, 6, 3, 2, 1]}
df = pd.DataFrame(data)
# convert the data to a long format
df.set_index('Division', inplace=True)
dfl = df.stack().reset_index().rename(columns={'level_1': 'corners', 0: 'val'})
# plot
sns.boxplot('corners', 'val', data=dfl, hue='Division')
plt.legend(title='Division', bbox_to_anchor=(1.05, 1), loc='upper left')
答案 1 :(得分:1)