我创建了一个分组条形图,表示每个时间间隔赢得案件的百分比。我想用每个时间间隔赢得的案件数量来注释条形图。
这是我的代码:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({
'years': ['1994-1998','1999-2003','2004-2008','2009-2013','2013-2017','2018-2022'],
'Starfish number of cases': [10,8,31,12,2,3],
'Starfish percent of wins': [0,0.25,0.225806451612903,0.416666666666666,1,0],
'Jellyfish number of cases':[597,429,183,238,510,595],
'Jellyfish percent of wins':[0.362646566164154,0.273892773892773,0.423497267759562,0.478991596638655,0.405882352941176,0.408403361344537],
})
df = pd.melt(df, id_vars=['years'], value_vars=['Starfish percent of wins', 'Jellyfish percent of wins'])
sns.set_theme(style="whitegrid")
# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(30, 15))
sns.barplot(x="years", y="value", hue='variable', data=df)
for p in ax.patches:
ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
我尝试在熔化函数中包含案例数 (即
df = pd.melt(df, id_vars=['years'], value_vars=['Starfish number of cases','Jellyfish number of cases','Starfish percent of wins', 'Jellyfish percent of wins'])
)但这会增加代表病例总数的额外条形。
我尝试通过添加下面的行来修改答案here,但结果显示百分比注释,而不是案例数:
for p,years in zip(ax.patches, df['Starfish number of cases','Jellyfish number of cases']):
ax.annotate(years, xy=(p.get_x()+p.get_width()/2, p.get_height()),
ha='center', va='bottom')
条形图存储在
ax.containers
中。有 2 个容器,每个容器对应一个色调值。
ax.bar_label()
可以获取一个容器和一个标签列表作为输入。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df_orig = pd.DataFrame({
'years': ['1994-1998', '1999-2003', '2004-2008', '2009-2013', '2013-2017', '2018-2022'],
'Starfish number of cases': [10, 8, 31, 12, 2, 3],
'Starfish percent of wins': [0, 0.25, 0.2258064516, 0.41666666666, 1, 0],
'Jellyfish number of cases': [597, 429, 183, 238, 510, 595],
'Jellyfish percent of wins': [0.3626465661, 0.2738927739, 0.4234972677, 0.4789915966, 0.4058823529, 0.4084033613],
})
df = pd.melt(df_orig, id_vars=['years'],
value_vars=['Starfish number of cases', 'Starfish percent of wins',
'Jellyfish number of cases', 'Jellyfish percent of wins'])
sns.set_theme(style="whitegrid")
# Initialize the matplotlib figure
fig, ax = plt.subplots(figsize=(12, 5))
sns.barplot(x="years", y="value", hue='variable',
hue_order=['Starfish percent of wins', 'Jellyfish percent of wins'], data=df, ax=ax)
for bargroup, variable in zip(ax.containers, ['Starfish number of cases', 'Jellyfish number of cases']):
labels = ['' if val == 0.0 else f'{val:.0f}' for val in df[df['variable'] == variable]['value']]
ax.bar_label(bargroup, labels)
sns.despine()