我有以下代码(DF和图表):
import pandas as pd
import matplotlib.pyplot as plt
data = [['2005','A',10],['2006','A',15],['2007','A',10],
['2005','B',15],['2006','B',70],['2007','B',10],
['2005','C',75],['2006','C',15],['2007','C',80]
]
df = pd.DataFrame(
data=data,
columns=['year','type','count'])
fig, ax = plt.subplots(figsize=(15, 7),facecolor = '#fdf1e6')
ax.set_facecolor('#fdf1e6')
ax = sns.histplot(df, x='year',
hue='type',
weights='count',
multiple='stack',
shrink=0.9,
linewidth=3.5,
)
ax.get_legend().remove()
for c in ax.containers:
ax.bar_label(c, fmt=lambda x: f'{x:.0%}' if x > 0 else '', label_type='center')
结果是
我想将每个堆栈标签放置在每个堆栈的顶部(现在位于每个堆栈的中心)并将字体颜色更改为白色。
有什么想法吗?
谢谢!!!