本期,我们从国家统计局官网上下载了2005-2021年各省份的人均可支配收入的数据,用Plotly来做个小动画看看接近20年的时间里,哪个省份的人均可支配收入最高 。具体数据如下:
plotly的代码为:
import pandas as pd import plotly.graph_objects as go#导入相关的csv文件df=pd.read_csv('分省年度数据-人均可支配收入.csv',encoding='gbk')df['colors']=['aliceblue','antiquewhite','aqua','aquamarine','azure','beige','bisque','black','blanchedalmond','blue','blueviolet','brown','burlywood','cadetblue','chartreuse','chocolate','coral','cornflowerblue','red','crimson','cyan','darkblue','darkcyan','darkgoldenrod','darkgray','darkgreen','darkkhaki','darkmagenta','darkolivegreen','darkorange','darkorchid'] ani_frames=[]for i in df.columns[1:-1][::-1]: df.sort_values(by=i,inplace=True) bar=go.Bar(x=df[i],y=df['地区'],orientation='h',textposition='outside',marker_color=df['colors'],text=df[i]) frame=go.Frame(data=bar,layout=go.Layout(title=i+'各省份人均可支配收入(元)',title_x=0.5)) ani_frames.append(frame) d_inf=df.loc[:,['2005年','地区','colors']]d_inf.sort_values(by='2005年',inplace=True) ini_bar= go.Bar(x=d_inf['2005年'],y=d_inf['地区'],orientation='h',textposition='outside',marker_color=d_inf['colors'],text=d_inf['2005年']) buttons={ "type":"buttons", "direction":"right", "pad":{"r":80,"t":0}, "buttons":[{"label":"Play","method":"animate","args":[ None, dict(frame=dict(duration=600,redraw=True), transition=dict(duration=1800,easing="linear-in-out"), fromcurrent=True, mode='immediate') ]}]} fig=go.Figure( data=[ini_bar], layout=go.Layout( title="2005年各省份人均可支配收入(元)", title_x=0.5, width=1000,height=750, updatemenus=[buttons], xaxis=dict(showgrid=True,zeroline=False,automargin=True,range=[0,81000]), yaxis=dict(showgrid=True,zeroline=False,automargin=True) ), frames=ani_frames )fig.update_yaxes(nticks=40)fig.show()
运行一下: