ggplot在python中的使用(plotnine)
时间:2020-09-06 14:40:01
收藏:0
阅读:94
# 条形图 median_age_dict={ ‘Country‘: [‘New Zealand‘,‘Spain‘,‘Ireland‘,‘Israel‘,‘Denmark‘,‘Norway‘,‘Netherlands‘,‘Australia‘,‘Italy‘,‘Sweden‘], ‘Age‘: [39.0, 37.0, 35.0, 34.0, 34.0, 34.0, 34.0, 34.0, 34.0, 34.0] } median_age=pd.DataFrame(median_age_dict) ( # fill以颜色区分 ggplot(median_age,aes(x=‘Country‘,y=‘Age‘,fill=‘Country‘)) # 条形图,需要stat指明统计量 +geom_bar(stat=‘identity‘,width=0.5) # 文本标签,nudge_y表示偏离量 +geom_text(aes(x=‘Country‘,y=‘Age‘,label=‘Age‘),nudge_y=2) +coord_flip()# 翻转x,y # 排序条形图 +xlim(median_age[‘Country‘]) # +xlim(median_age[‘Country‘][::-1])逆序 # 隐藏图例 +theme(legend_position = ‘none‘) # 加上标题 +ggtitle(‘Top 10 Median age of respondents from different countries‘) ) # 折线图 from plotnine.data import economics save_rate = economics[economics[‘date‘]>‘2013-01-01‘] save_rate=save_rate.reset_index(drop=True) ( ggplot(save_rate,aes(x=‘date‘,y=‘psavert‘)) + geom_line(color=‘blue‘) +geom_point(color=‘red‘) + ylim(0,6)#y轴的范围 # 改变x坐标刻度及间距 + scale_x_date(breaks=‘5 months‘,date_labels=‘%Y-%m‘) ) # 堆叠条形图 importance_dict={ ‘tool‘: [‘Python‘,‘Python‘,‘Python‘,‘R‘,‘R‘,‘R‘,‘BigData‘,‘BigData‘,‘BigData‘,‘SQL‘,‘SQL‘,‘SQL‘], ‘importance‘: [‘Necessary‘,‘Nice to have‘,‘Unnecessary‘,‘Nice to have‘,‘Necessary‘,‘Unnecessary‘,‘Nice to have‘,‘Necessary‘,‘Unnecessary‘,‘Nice to have‘,‘Necessary‘,‘Unnecessary‘], ‘mix‘: [0.6459935499875962,0.32721409079632846,0.026792359216075416,0.5139452332657201,0.4148073022312373,0.07124746450304259,0.5740647118301314,0.3799292214357937,0.04600606673407482,0.49177800616649536,0.434224049331963,0.07399794450154162] } Jobskillimpotance=pd.DataFrame(importance_dict) # Jobskillimpotance不能绘制多个折线图,因为geom_line要求数据点是数值坐标形式…… ( ggplot(Jobskillimpotance,aes(x=‘tool‘,y=‘mix‘,fill=‘importance‘))#传入数据来源和映射 + geom_bar(stat=‘identity‘)#统计方式为原数据 ) # 多折线图 ( ggplot(df,aes(x=‘year‘,y=‘mix‘,fill=‘gender‘,color=‘gender‘)) + geom_line(size=1) + geom_point(aes(shape=‘gender‘),size=3)#添加数据标记 + scale_x_date(breaks=‘5 years‘,date_labels=‘%Y‘)#设置x轴的间隔和表示格式 + ylim(0,6)#y轴范围 )
原文:https://www.cnblogs.com/lunge-blog/p/13621434.html
评论(0)