【用pandas_alive几行代码绘制竞赛动图】8.城市人口(测试代码+数据集+绘图参数解析)

目录

    • 8.城市人口
      • 城市人口API说明:
      • 城市人口数据集
      • 城市人口图例程
    • 总结


欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中

资源文件下载:
专栏学习说明(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)

所有效果图预览:
效果图展示(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)

环境配置:
环境配置与检测(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)


8.城市人口

讲述更多关于两张图距离的控制

城市人口API说明:

有部分在代码注释中

n_visible=10,

  • 属性参数取10个

adjust_subplot_left=0.15,

  • float类型,(0,1)之间
  • 设置子图左浮动=0.15,

adjust_subplot_right=0.9,

  • float类型,(0,1)之间
  • 设置子图右浮动=0.9,

adjust_subplot_bottom=0.1,

  • float类型,(0,1)之间
  • 设置子图底部浮动=0.1,

adjust_subplot_top=0.9,

  • float类型,(0,1)之间
  • 设置子图顶部浮动=0.9,

adjust_subplot_wspace=0.2,

  • float类型,(0,1)之间
  • 设置子图间距浮动=0.2,

adjust_subplot_hspace=0.25

  • float类型,(0,1)之间
  • 设置子图空间浮动=0.25,

城市人口数据集

保存文件名为 urban_pop.csv,建议码云下载,否则可能出现编码问题

year,United States,India,China,Ethiopia,Poland,Malaysia,Peru,Venezuela,Iraq,Saudi Arabia,Canada,Algeria,Ukraine,Vietnam,Thailand,"Congo, Dem. Rep.",Spain,South Africa,Colombia,Argentina,Egypt,South Korea,Italy,Philippines,France,United Kingdom,Bangladesh,Iran,Turkey,Germany,Pakistan,Nigeria,Mexico,Russia,Japan,Indonesia,Brazil
1976,160611122,138219074,162497601,3194879,19215135,4802814,9834645,10382196,7530612,4673719,17705309,6919237,28966085,9399461,10568872,6141742,25379947,12445371,15011687,21370520,17210387,17823527,36687131,15245564,39578989,43757644,7667521,15742290,16914583,56885943,18365687,13177979,38883279,90810675,85642808,26596457,67790415
1977,162256678,143699557,165293316,3300643,19625330,5038232,10197056,10778965,7901451,5041250,17923214,7196331,29452016,9652431,11092595,6381644,25880544,12771480,15524763,21797026,17668393,18743336,36955192,15826569,39798460,43833733,8543815,16521005,17474315,56801863,19175026,13868037,40371908,92479950,86538157,28001978,70478354
1978,164005080,149379782,171153535,3406129,20007316,5284698,10567468,11185526,8253727,5441027,18112108,7565661,29928920,9911702,11628473,6630163,26368356,13105135,16052408,22227673,18089504,19679806,37198851,16426578,40001620,43925435,9528585,17336704,18048189,56796181,20032626,14598364,41879098,94157479,87391419,29468690,73245834
1979,165847531,155285824,180399661,3522584,20341874,5539880,10945723,11600322,8598800,5885282,18301934,7956090,30386569,10181044,12176748,6882719,26838008,13454231,16593237,22668000,18530889,20636698,37418639,17048162,40205371,44054299,10622254,18227951,18640066,56865826,20941767,15355488,43407693,95659612,88197927,30996679,76091693
1980,167551171,161444128,189947471,3658252,20663601,5801267,11331194,12022351,8945814,6382806,18549289,8369497,30825447,10447627,12692095,7135884,27289444,13828615,17145845,23122595,18994489,21623805,37607540,17738316,40423470,44195960,11827261,19206467,19252680,57028530,21906732,16131172,44952217,96960865,88958689,32591870,79015954
1981,169552427,167521704,199949784,3817958,20985955,6050366,11723239,12450233,9294818,6937865,18791654,8805955,31287683,10720898,13071774,7389309,27669147,14253248,17710021,23609667,19480106,22617087,37764025,18687449,40659231,44271630,12920085,20274421,20329814,57229147,22897114,17103116,46412878,98228786,89733659,34436558,82013626
1982,171528659,173152676,210823843,4000296,21314754,6305685,12089571,12875246,9646517,7546375,19057229,9265778,31693551,11006067,13450801,7645260,27925417,14700152,18286697,24118025,19987197,23619800,37819718,19680770,40911627,44228171,13607076,21426582,21630562,57267174,23877291,18109529,47890690,99464693,90474900,36463770,85085296
1983,173459636,178956141,220472140,4203323,21651242,6571674,12460705,13303646,9998790,8194651,19287381,9746745,32110163,11301138,13832449,7907632,28151118,15162766,18874699,24636888,20516521,24621925,37822134,20717792,41177471,44215089,14326358,22654500,22977503,57114118,24901258,19156448,49383429,100677781,91224787,38577957,88209014
1984,175321738,184906540,230206255,4422527,21984900,6854613,12837778,13741349,10349519,8862080,19511294,10246073,32533314,11603770,14220724,8182628,28357570,15630401,19472129,25163825,21068787,25577374,37819273,21799606,41450438,44257473,15083858,23944608,24362121,56792461,25962081,20257934,50884710,102058393,91937389,40774954,91357900
1985,177239105,190975975,240414890,4655890,22299554,7158816,13220933,14189017,10697257,9530555,19731489,10759943,32933625,11912008,14616958,8520035,28549379,16134214,20077488,25695319,21643302,26473134,37818886,22923393,41726184,44329755,15880101,25281030,25769767,56485448,27052677,21421320,52390613,103466989,92632808,43042272,94505178
1986,179244877,197165614,251325056,4901173,22589785,7487770,13610395,14647328,11040738,10193888,19967235,11287827,33344880,12225169,15025684,8876843,28723937,16716155,20656600,26230730,22241759,27480866,37809627,24093004,42005647,44404206,16718788,26666054,27034322,56438249,28172326,22654337,53899557,104825013,93348378,45381922,97647144
1987,181215212,203469688,262976051,5160078,22855463,7839689,14005309,15114933,11382713,10774882,20237468,11821049,33766182,12541644,15443627,9253676,28887112,17299431,21228791,26769637,22812398,28537079,37802166,25307752,42289365,44470325,17598710,28011702,28238882,56695484,29321589,23956989,55410753,106169956,93963675,47793464,100778354
1988,183232441,209894838,275121076,5434867,23089202,8210275,14404448,15589603,11640586,11342799,20506939,12346640,34209429,12862589,15862003,9660136,29036437,17891635,21808613,27309085,23358966,29585374,37809110,26565345,42572024,44540723,18516964,29328931,29455664,57044790,30497993,25327851,56925349,107471421,94519909,50281090,103903008
1989,185333919,216442440,287504237,5728511,23241946,8591294,14805506,16067828,11877599,11895419,20883649,12878973,34541910,13237507,16267069,10107028,29169746,18505772,22398271,27844463,23893893,30626132,37826147,27860539,42846787,44628280,19464440,30587388,30682120,57469536,31695621,26761923,58445699,108424260,95061557,52840795,107023058
1990,187966119,223096279,300165618,6043927,23350476,8977771,15207438,16547195,12142167,12432320,21206427,13416507,34641542,13772504,16641681,10602701,29286916,19149881,23000000,28373007,24406147,31656393,37846480,29082060,43127028,44733264,20439396,31751090,31923300,58079842,32914428,28259055,59951345,108837430,95542280,55483475,110146163
1991,191509147,229752408,314301034,6383315,23450653,9366643,15608008,17014346,12437110,12952377,21482271,13958197,34734234,14324149,16936803,11155614,29430524,19822817,23613619,28887229,24890671,32459655,37861339,29760929,43467991,44855736,21391215,32799183,32891192,58625381,34148626,29472185,61411745,109073667,96005316,58355038,113265899
1992,195199459,236274335,328521540,6744717,23539562,9846036,16007183,17476497,12759904,13454748,21813814,14502589,34854630,14889784,17206294,11762423,29607126,20511177,24238652,29352243,25351104,33169705,37907512,30439167,43785894,45011752,22255441,33888647,33738148,59146205,35400161,30726608,62876086,109115423,96414127,61396542,116379447
1993,198806845,242896345,342961393,7125617,23616843,10348234,16406525,17938345,13107820,13888412,22130869,15044498,34894325,15462867,17462829,12400106,29793326,21212379,24870590,29809452,25795989,33872973,37964793,31119331,44077099,45154426,23136115,34949749,34589980,59647492,36682088,32024149,64340297,108985461,96821759,64522965,119489196
1994,202278113,249629427,357836540,7519515,23684074,10868160,16820251,18400682,13475501,14285427,22450573,15579364,34741407,16036816,17724400,13037939,29967941,21906214,25506165,30260590,26238204,34575766,38006634,31802962,44342923,45304828,24044693,35978707,35453793,60028206,38012967,33368626,65800649,108843023,97320876,67741187,122608542
1995,205718394,256470883,373035157,7888014,23733222,11409139,17240497,18863803,13858715,14663134,22760570,16103497,34487999,16604653,18004312,13652804,30133075,22576687,26142216,30705908,26687693,35280305,38041344,32491471,44604027,45459651,24983813,37010603,36333541,60378049,39405594,34764761,67251519,108866238,97862490,71046004,125743641
1996,209146726,263440887,388593258,8247227,23768665,11973524,17668537,19326875,14255733,15019607,23081461,16615709,34203211,17163390,18309040,14233459,30291556,23218617,26777679,31145811,27145811,35810625,38086153,33183963,44863823,45610451,25958281,38054098,37230469,60725922,40868934,36216363,68621427,108700578,98272808,74433222,128896593
1997,212694273,270523260,404485562,8610539,23801620,12557524,18100961,19788904,14665741,15360171,23428320,17115300,33912723,17713643,18632440,14788167,30451227,23836117,27410868,31579645,27681745,36259672,38140443,33879027,45123550,45762480,26961627,39051703,38142785,60986289,42394458,37721269,69944968,108515107,98667335,77897525,132175402
1998,216206090,277707327,420606126,8981735,23827143,13157335,18529226,20251502,15123544,15704580,23739358,17606301,33630036,18256423,18965132,15338240,30610090,24431488,28043543,32009566,28270163,36633453,38185563,34576876,45390693,45931122,27993765,40038693,39068844,61167004,43938667,39284672,71260342,108329743,99095072,81452538,135456370
1999,219721677,284978106,436766058,9363839,23842562,13764926,18940925,20715860,15606189,16078075,24048329,18103945,33333798,18823973,19293244,15915279,30767125,25011815,28676311,32438320,28862915,37006183,38226137,35277843,45743988,46119137,29046503,41030621,40002835,61376500,45445310,40910972,72568684,107987645,99434460,85098300,138709641
2000,223069137,292322757,452999147,9761536,23611695,14375105,19326872,21182664,16094906,16499665,24388404,18600197,33019123,19477364,19758316,16544508,30937864,25582579,29309135,32867357,29457843,37428328,38277624,35981496,46214923,46319551,30114488,42026541,40942334,61629857,46947759,42603693,73899942,107528575,99760751,88840036,141915773
2001,225792302,300118524,471767321,10174157,23622394,14918252,19683539,21651926,16594707,16978440,24757782,19094843,32707281,20134757,20680738,17234266,31186430,26143328,29941986,33297967,30055505,37867709,38333314,36689358,46709571,46557334,31346174,43031116,41943212,61902439,48435634,44726802,75268749,107067618,101706485,91738482,144508698
2002,228400290,308796507,491993700,10604081,23621395,15458510,20014065,22057235,17104294,17510539,25052940,19591211,32432088,20800243,21613066,17981229,31708814,26720456,30573543,33728934,30659220,38258247,38447500,37399541,47206863,46930583,32796275,44050010,42979004,62174878,49919690,46947855,76634718,106568907,104055019,94698443,147005321
2003,230876596,317584393,512473984,11049316,23563051,15999895,20323604,22460389,17607000,18084339,25304780,20095408,32238838,21474148,22554369,18783707,32390830,27305501,31200433,34159485,31267885,38626122,38686985,38104244,47699501,47323791,34276157,45078311,44016864,62376854,51408755,49272800,78013778,106132766,106256267,97720468,149452654
2004,233532722,326495067,533257098,11510093,23506503,16548747,20619818,22858285,18080764,18680566,25566897,20616005,32075858,22163653,23508150,19635486,33059302,27898739,31817837,34588886,31879899,38947802,39006818,38793193,48209404,47767769,35772637,46107032,45048098,62529997,52922901,51708640,79428213,105771150,108136910,100811250,151843988
2005,236200507,335503762,554367818,11986371,23453429,17108405,20909083,23248394,18513378,19286107,25834340,21159469,31932581,22870388,24472850,20534304,33727737,28506195,32422505,35015695,32495526,39195731,39267369,39459729,48730233,48269624,37274029,47130046,46065713,62660267,54474508,54260116,80890450,105433013,109856670,103961908,154176524
2006,238999326,344622641,575118254,12478999,23396235,17681869,21194852,23622801,18889524,19893608,26126316,21729732,31801166,23598140,25451682,21483246,34408810,29131012,33015422,35439470,33111793,39490771,39454178,40099208,49228316,48798541,38777278,48145863,47064036,62753557,56069495,56934076,82408287,105152847,111383848,107178769,156447985
2007,241795278,353850625,595670841,13001478,23340838,18266045,21477303,23979532,19223314,20507062,26441461,22327433,31694727,24348000,26439736,22487132,35159317,29774433,33597410,35861110,33700835,39740941,39722857,40715257,49690034,49351705,40283010,49125150,48048353,62833410,57704173,59734515,83973950,105037426,112827761,110459097,158660052
2008,244607104,363154577,616481190,13689470,23300939,18854125,21722213,24328099,19565443,21137735,26789863,22955890,31605915,25120981,27438708,23545867,35833174,30442138,34166611,36284569,34289703,40093884,40056298,41319488,50127513,49913475,41802031,50093529,49047262,62875807,59373384,62665438,85573769,105055515,114107975,113814309,160823508
2009,247276259,372465917,637407288,14413055,23274004,19435329,21951808,24683266,19985687,21801430,27158023,23612219,31547432,25920396,28447159,24661566,36260460,31137754,34720224,36714245,34919606,40351067,40308358,41929228,50547198,50463084,43352798,51080202,50096864,62877220,61070625,65723902,87182005,105149982,115228215,117243827,162949977
2010,249845296,381763166,658498663,15178365,23165018,20002882,22185851,25050752,20552598,22508632,27522537,24299173,31465465,26757120,29469051,25833935,36535850,31866171,35257426,37055902,35603056,40602657,40502481,42597021,50961407,51030310,44954427,52095517,51225589,62940432,62793242,68917190,88781439,105261473,116302928,120709130,165055094
2011,252186596,391040303,678933504,15986316,23134846,20516662,22421443,25452964,21282801,23264870,27847821,25016303,31395063,27621281,30179366,27065209,36773882,32630538,35770189,37543830,36347578,40909592,40641670,43505397,51373800,51600211,46610737,53140089,52440387,61940177,64539555,72230098,90370891,105407907,116416235,124016544,167158525
2012,254576561,400417728,699187267,16839218,23086831,21009012,22662393,25875071,22141872,24058860,28166078,25763106,31359984,28514086,30828051,28355103,36904876,33429132,36262940,38027774,37114895,41089082,40894259,44437841,51791144,52130345,48311928,54203628,53730762,62064608,66312625,75664328,91948801,105669949,116331281,127396459,169240750
2013,256899730,409909187,719587859,17717910,23025350,21491648,22922695,26249155,23075649,24865453,28479640,26536378,31330962,29430443,31477989,29704531,36891840,34249974,36761204,38509756,37919402,41240244,41548775,45385009,52228061,52650595,50048763,55292621,55071747,62242278,68114463,79214937,93515787,105998510,116262976,130826016,171300683
2014,259361210,419568459,740239259,18635946,22960228,21974918,23221633,26482983,24005089,25648738,28781576,27332602,31223156,30371288,32127193,31115109,36890017,35079618,37300597,38990109,38737018,41463573,42109853,46329521,52647802,53209683,51818338,56418308,56438761,62510392,69956952,82878551,95072126,106354643,116208079,134287151,173346772
2015,261865944,429428653,761027100,19590313,22897449,22464989,23571246,26518336,24872481,26382755,29011826,28146511,31183824,31333193,32772699,32586918,36971015,35905874,37904385,39467043,39551544,41645542,42247229,47262079,53009026,53802927,53608403,57580319,57806283,63062064,71845558,86652532,96615320,106703732,116182717,137751865,175375436
2016,264366216,439498772,782199374,20581872,22849639,22964507,23979736,26313884,25661856,27057429,29357013,28977628,31122512,32314724,33415230,34121207,37112875,36724030,38589139,39940546,40359123,41774264,42351339,48177810,53321626,54382825,55420910,58781895,59175037,63592936,73782310,90531047,98145001,107050095,116145370,141210511,177386818
2017,266676634,449789369,803554542,21609845,22824769,23467810,24438508,25917344,26391343,27678169,29727992,29821745,31043768,33310316,34051250,35717778,37311863,37534797,39338952,40410674,41185809,41861498,42462869,49096983,53612472,54923317,57254681,60016579,60537696,63861626,75761710,94518555,99655905,107349518,116053379,144652795,179379301
2018,268720071,460295677,823827650,22678295,22806875,23973075,24921870,25465822,27085311,28255384,30169097,30670086,30946607,34317154,34678853,37376673,37587449,38339668,40105215,40877099,42030812,42038247,42559879,50027217,53879064,55426598,59107944,61266765,61857510,64096118,77810763,98611179,101149488,107539347,115920900,148084795,181335507

城市人口图例程

# @Time    : 2022/1/12 22:14
# @Author  : 南黎
# @FileName: 8.城市人口.py

import pandas as pd

######显示中文宋体字体导入,如果使用中文加上这段代码######
import matplotlib as plt

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#####################################################

import pandas_alive

urban_df = pd.read_csv("数据源data/urban_pop.csv", index_col=0, parse_dates=[0], thousands=',')

animated_line_chart = (
    urban_df
        .sum(axis=1)  # 按行计算的和
        .pct_change()  # 计算与前一个元素的百分比 比如说[1,2,3].pct_change()=[NaN,(2-1)/1,(3-2)/2] 为什么第一个是NaN空值呢,因为第一数前面没有数给他减,给他除了···
        .fillna(method='bfill')  # backfill/bfill用下一个非缺失值填充该缺失值,因为前一步计算会有缺失值
        .mul(100)  # 缺失值单元格在相乘之前已填充100 函数返回数据帧和其他元素的乘法。此功能本质上与dataframe * other,但它提供了额外的支持来处理其中一个输入中的缺失值。
        .plot_animated(
        kind="line",  # 图表类型为折线图
        title="子图1",  # 子图名称
        period_label=False,  # 不显示时间
        add_legend=False  # 不显示渲染图进度条
    )

)

animated_bar_chart = urban_df.plot_animated(
    n_visible=10,  # 属性参数取10个
    title="子图2",  # 子图名称
    period_fmt="%Y"  # 时间格式,XXXX年
)

pandas_alive.animate_multiple_plots(
    filename='8.城市人口.gif',
    plots=[animated_bar_chart, animated_line_chart],  # 两个表放进一个列表中
    title='发现你走远了——8.城市人口',
    enable_progress_bar=True,
    adjust_subplot_top=0.85,

    # 设置子图的位置
    # adjust_subplot_left=0.5,
    # adjust_subplot_right=0.9,
    # adjust_subplot_bottom=0.1,
    # adjust_subplot_top=0.9,
    # adjust_subplot_wspace=0.2,
    # adjust_subplot_hspace=0.25
)


总结

版权声明:

发现你走远了@mzh原创作品,转载必须标注原文链接

Copyright 2022 mzh

Crated:2022-1-13

我折腾了一星期,梳理了很多入门小白避雷的方法,还会继续更新,如果看了对你有帮助,希望得到大家的点赞收藏支持!(毕竟时短间学完太难了,建议放进收藏夹吃灰

欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
欢迎关注 『pandas_alive绘制竞赛动图』 专栏,持续更新中
【一、效果图展示(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)】
【二、专栏学习说明(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)】
【三、环境配置与检测(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)】
【四、数据集说明(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)】
【五、常见问题(配置好的venv虚拟环境+拿来即用测试代码+测试数据集+参数api解析)】
【1.条形图(测试代码+数据集+绘图参数解析)】
【2.折线图(测试代码+数据集+绘图参数解析)】
【3.散点图(测试代码+数据集+绘图参数解析)】
【4.饼状图(测试代码+数据集+绘图参数解析)】
【5.气泡图(测试代码+数据集+绘图参数解析)】
【6.地理空间图(测试代码+数据集+绘图参数解析)】
【7.多个图表(测试代码+数据集+绘图参数解析)】
【8.城市人口(测试代码+数据集+绘图参数解析)】
【9.G7国家的预期寿命(测试代码+数据集+绘图参数解析)】
【10.新南威尔士州 COVID 可视化(测试代码+数据集+绘图参数解析)】
【更多内容敬请期待】


你可能感兴趣的:(#,python,pandas_alive,动态绘图,城市人口图,数据可视化)