利用镜像条形图探索Erasmus项目
importpandasaspdimportnumpyasnpimportmatplotlib.pyplotasplt数据探索
以下数据如果有需要的同学可关注公众号HsuHeinrich,回复【数据可视化】自动获取~
resume_url='https://raw.githubusercontent.com/holtzy/the-python-graph-gallery/master/static/data/resume.csv'erasmus_url='https://raw.githubusercontent.com/holtzy/the-python-graph-gallery/master/static/data/erasmus.csv'resume=pd.read_csv(resume_url)data=pd.read_csv(erasmus_url)print(resume.head())print(data.head())1、resume数据:
country_name:国家地区名称
mean_rec:平均入境学生数
mean_send:平均出国学生数
2、data数据:
academic_year:年份
participants_x:出国学生的x位置
participants_y:入境学生的x位置
y_position:学生的y位置
绘制有序条形图
# 初始化布局fig,ax=plt.subplots(figsize=(6,6))# 创建两个条形图:数据已经按照mean_send升序了ax.barh(resume['country_name'],resume['mean_rec'],color='blue',alpha=0.3)ax.barh(resume['country_name'],-resume['mean_send'],color='darkorange',alpha=0.3)# 标题ax.set_title('Number of Student',weight='bold')plt.show()移除边框并修改国家标签位置
fig,ax=plt.subplots(figsize=(6,6))# 创建两个条形图:数据已经按照mean_send升序了ax.barh(resume['country_name'],resume['mean_rec'],color='blue',alpha=0.3)ax.barh(resume['country_name'],-resume['mean_send'],color='darkorange',alpha=0.3)# 移除轴刻度ax.set_xticks([])ax.set_yticks([])# 移除边框ax.spines[['right','top','left','bottom']].set_visible(False)# 国家标签居中fori,country_nameinenumerate(resume['country_name']):ax.text(0,i,country_name,ha='center',va='center',fontsize=8,alpha=0.6)# 标题ax.set_title('Number of Student',weight='bold',fontsize=9)plt.show()添加观测数据
fig,ax=plt.subplots(figsize=(6,6))# 创建两个条形图:数据已经按照mean_send升序了ax.barh(resume['country_name'],resume['mean_rec'],color='blue',alpha=0.3)ax.barh(resume['country_name'],-resume['mean_send'],color='darkorange',alpha=0.3)# 移除轴刻度ax.set_xticks([])ax.set_yticks([])# 移除边框ax.spines[['right','top','left','bottom']].set_visible(False)# 国家标签居中fori,country_nameinenumerate(resume['country_name']):ax.text(0,i,country_name,ha='center',va='center',fontsize=8,alpha=0.6)# 为每个国家地区添加不同年份的观测数据y_position=0fori,rowindata.iterrows():# 获取观测值数据sending=-row['participants_x']receiving=row['participants_y']y_position=row['y_position']years=row['academic_year']# 根据相关年份更改 alpha 参数决定透明度year_alpha_mapping={'2014-2015':0.3,'2015-2016':0.4,'2016-2017':0.5,'2017-2018':0.6,'2018-2019':0.7,'2019-2020':0.9}alpha=year_alpha_mapping[years]*0.6# y轴位置添加小噪声y_position+=np.random.normal(0,0.1,1)# 添加散点ax.scatter(sending,y_position,c='darkorange',alpha=alpha,s=3)ax.scatter(receiving,y_position,c='darkblue',alpha=alpha,s=3)# 标题ax.set_title('Number of Student',weight='bold',fontsize=9)plt.show()额外的注释丰富图表信息
# 初始化fig,ax=plt.subplots(figsize=(8,6))# 创建两个条形图:数据已经按照mean_send升序了ax.barh(resume['country_name'],resume['mean_rec'],color='blue',alpha=0.2)ax.barh(resume['country_name'],-resume['mean_send'],color='darkorange',alpha=0.2)# 移除轴刻度ax.set_xticks([])ax.set_yticks([])# 移除边框ax.spines[['right','top','left','bottom']].set_visible(False)# 国家标签居中fori,country_nameinenumerate(resume['country_name']):ax.text(0,i,country_name,ha='center',va='center',fontsize=8,alpha=0.6)# 为每个国家地区添加不同年份的观测数据y_position=0fori,rowindata.iterrows():# 获取观测值数据sending=-row['participants_x']receiving=row['participants_y']y_position=row['y_position']years=row['academic_year']# 根据相关年份更改 alpha 参数决定透明度year_alpha_mapping={'2014-2015':0.3,'2015-2016':0.4,'2016-2017':0.5,'2017-2018':0.6,'2018-2019':0.7,'2019-2020':0.9}alpha=year_alpha_mapping[years]*0.6# y轴位置添加小噪声y_position+=np.random.normal(0,0.2,1)# 添加散点ax.scatter(sending,y_position,c='darkorange',alpha=alpha,s=3)ax.scatter(receiving,y_position,c='darkblue',alpha=alpha,s=3)# 离校和入学学生标签ax.text(-6000,24,'Outgoing\nstudents',color='darkorange',ha='center',va='center',weight='bold')ax.text(6000,24,'Incoming\nstudents',color='darkblue',ha='center',va='center',weight='bold')# 描述信息的标题ax.text(-7000,9,'Students\nexchanges\nin Europe',ha='left',va='center',weight='bold',fontsize=14)# 描述信息text='''Country ranking based on a sample Erasmus programs. Bars show the annual average for the period, points show the values for each year.'''ax.text(-7000,4.5,text,ha='left',va='center',fontsize=7)# 著作信息text='''Data: Data.Europa | Plot: @BjnNowak'''ax.text(-7000,1,text,ha='left',va='center',fontsize=6)# 年份图例ax.text(x=4200,y=11,s='Academic Year',fontsize=7,weight='bold')y_position=10# 开始的位置foryear,alphainyear_alpha_mapping.items():# 添加点ax.scatter(4000,y_position,alpha=alpha,s=5,c='black')ax.text(x=4200,y=y_position-0.2,s=year,fontsize=7)y_position-=1# 迭代减少# 标题ax.set_title('Number of Student',weight='bold',fontsize=9)plt.show()参考:Mirror barplot with data points
共勉~