import pandas as pd
from plotnine import *
df = pd.DataFrame({'id': [1, 2, 3, 4, 5, 6],
'sex':['male', 'female', 'male', 'male', 'female', 'female'],
'UA': [420, 300, 320, 500, 390, 320]})
# df为:
id sex UA
0 1 male 420
1 2 female 300
2 3 male 320
3 4 male 500
4 5 female 390
5 6 female 320
"""绘制柱状图"""
print( ggplot(df.groupby(['sex'])['UA']\
.agg(['mean']).reset_index())
+ geom_col( aes(y='mean', x='sex'))
)

1、添加文本
print( ggplot(df.groupby(['sex'])['UA']\
.agg(['mean']).reset_index())
+ geom_col( aes(y='mean', x='sex'))
+ annotate("text",
x = 1.5, y = 430,
label = "*****")
)

2、添加线段
print( ggplot(df.groupby(['sex'])['UA']\
.agg(['mean']).reset_index())
+ geom_col( aes(y='mean', x='sex'))
+ annotate("segment",
x = 1, y= 420,
xend = 2, yend = 420)
)

3、添加矩形阴影
print( ggplot(df.groupby(['sex'])['UA']\
.agg(['mean']).reset_index())
+ geom_col( aes(y='mean', x='sex'))
+ annotate("rect",
xmin = 1, xmax = 2,
ymin = 300, ymax = 430,
alpha = 0.7,
fill = "skyblue")
)
