注解:文本、线段、矩形阴影

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张图片


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张图片


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张图片


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")
)

注解:文本、线段、矩形阴影_第4张图片


你可能感兴趣的:(plotnine,数据分析,数据挖掘,pandas,python)