1说明: 1。1资料来源:标点符https:www。biaodianfu。combarchartrace。html感谢 1。2知识点:python、matplotlib、csv和微软编辑器小技巧。 1。3环境: 华为笔记本电脑、深度deepinlinux操作系统、python3。8和微软vscode编辑器。 2citypopulations。csv 2。1在线地址:https:gist。githubusercontent。comjohnburnmurdoch4199dbe55095c3e13de8d5b2e5e5307arawfa018b25c24b7b5f47fd0568937ff6c04e384786citypopulations 2。2我喜欢离线的数据,那就下载下来。用360浏览器,打开上面的网页,复制地址,下载,新建下载,保存csv,即可。 如操作图: 2。3打开数据看看,自动保存csv格式,不错吧。 3用微软编辑器自带启动jupyter: 3。1新建一个11。py,小技巧:就可以类似jupyter的一段段cell格式,并启动jupyter。 3。2结果: 3。3代码:注意代码:是带有的格式,就是微软vscode自带jupyterimportpandasaspdimportmatplotlib。pyplotaspltimportmatplotlib。tickerastickerimportmatplotlib。animationasanimationfromIPython。displayimportHTMLdfpd。readcsv(datacitypopulations。csv,usecols〔name,group,year,value〕)df。head()currentyear2018dffdf〔df〔year〕。eq(currentyear)〕。sortvalues(byvalue,ascendingTrue)。head(10)fig,axplt。subplots(figsize(15,8))ax。barh(dff〔name〕,dff〔value〕)colorsdict(zip(〔India,Europe,Asia,LatinAmerica,MiddleEast,NorthAmerica,Africa〕,〔adb0ff,ffb3ff,90d595,e48381,aafbff,f7bb5f,eafb50〕))grouplkdf。setindex(name)〔group〕。todict()fig,axplt。subplots(figsize(15,8))ax。barh(dff〔name〕,dff〔value〕,color〔colors〔grouplk〔x〕〕forxindff〔name〕〕)fori,(value,name)inenumerate(zip(dff〔value〕,dff〔name〕)):ax。text(value,i,name,haright)Tokyo:nameax。text(value,i。25,grouplk〔name〕,haright)Asia:groupnameax。text(value,i,value,haleft)38194。2:valueax。text(1,0。4,currentyear,transformax。transAxes,size46,haright)fig,axplt。subplots(figsize(15,8))defdrawbarchart(year):dffdf〔df〔year〕。eq(year)〕。sortvalues(byvalue,ascendingTrue)。tail(10)ax。clear()ax。barh(dff〔name〕,dff〔value〕,color〔colors〔grouplk〔x〕〕forxindff〔name〕〕)dxdff〔value〕。max()200fori,(value,name)inenumerate(zip(dff〔value〕,dff〔name〕)):ax。text(valuedx,i,name,size14,weight600,haright,vabottom)ax。text(valuedx,i。25,grouplk〔name〕,size10,color444444,haright,vabaseline)ax。text(valuedx,i,f{value:,。0f},size14,haleft,vacenter)ax。text(1,0。4,year,transformax。transAxes,color777777,size46,haright,weight800)ax。text(0,1。06,Population(thousands),transformax。transAxes,size12,color777777)ax。xaxis。setmajorformatter(ticker。StrMethodFormatter({x:,。0f}))ax。xaxis。setticksposition(top)ax。tickparams(axisx,colors777777,labelsize12)ax。setyticks(〔〕)ax。margins(0,0。01)ax。grid(whichmajor,axisx,linestyle)ax。setaxisbelow(True)ax。text(0,1。12,Themostpopulouscitiesintheworldfrom1500to2018,transformax。transAxes,size24,weight600,haleft)ax。text(1,0,bypratapvardhan;creditjburnmurdoch,transformax。transAxes,haright,color777777,bboxdict(facecolorwhite,alpha0。8,edgecolorwhite))plt。box(False)drawbarchart(2018)importmatplotlib。animationasanimationfromIPython。displayimportHTMLfig,axplt。subplots(figsize(15,8))animatoranimation。FuncAnimation(fig,drawbarchart,framesrange(1968,2019))HTML(animator。tojshtml())oruseanimator。tohtml5video()oranimator。save() 3。4保存动画mp4格式,由于不能保存gif格式,本文章需要gif格式,否则估计上传不了。HTML(animator。tojshtml())备注一个,我是这么保存的,在上面的代码尾部进行修改,即可animator。save(homexgjDesktopdatacsvdd。mp4) 3。5mp4格式转换gif格式:python代码frommoviepy。editorimport指定路径clip(VideoFileClip(homexgjDesktopdatacsvdd。mp4))指定路径clip。writegif(homexgjDesktopdatacsvoutput。gif)带有进度条的print(转换完成了) 3。6output。gif 自己整理并分享出来,喜欢微软编辑器的人可以这样使用jupyter 喜欢的朋友,就请点赞、评论、转发、关注和收藏。