一些示例
数据
1. 点图
from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap
from plotnine.data import mtcars
p=(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
+ geom_point()
+ stat_smooth(method='lm')
+ facet_wrap('~gear'))
p.save(filename=png_path,width=400,height=150,units='mm')
2. 点图+折线图
代码
wt列是数字,但是这里要把它当成str,而不是数字
坐标轴也需要对小数位进行处理
数字太多,为了横轴展示开,旋转60度
from plotnine import *
from plotnine.data import mtcars
x_tick = [str(round(s, 2)).format("%.2f") for s in list(mtcars['wt'].unique())]
mtcars['wt'] = mtcars['wt'].astype(str)
p1 = (ggplot(mtcars,aes('wt','mpg',group='factor(gear)',color='factor(gear)'))
+ geom_line()
+ geom_point(aes(shape='factor(gear)'))
#+ ylim(0.75,1)
+ scale_x_discrete(labels=x_tick)
+ labs(x='wt')
+ theme(axis_text_x = element_text(rotation=60,size=9))
)
print(p1)