Data visualization on life expectancy using pyexcel and pyexcel-chart

Data source: Life Expectancy, The World Bank, World Development Indicators

First, let us read the data on life expectancy at birth around all countries.


In [38]:
import pyexcel as p
from IPython.display import HTML, display
sheet = p.get_sheet(url='https://raw.githubusercontent.com/pyexcel/pyexcel-chart/master/API_SP.DYN.LE00.IN_DS2_en_csv_v2.csv')
sheet.top_left()


Out[38]:
pyexcel sheet:
"Data Source" World Development Indicators
Last Updated Date2017-01-03
Country Name Country Code Indicator NameIndicator Code1960

The first four rows contain meta data and are not needed. Let us remove them


In [39]:
del sheet.row[:4]
sheet.top_left()


Out[39]:
pyexcel sheet:
Country NameCountry CodeIndicator Name Indicator Code1960
Aruba ABW Life expectancy at birth, total (years)SP.DYN.LE00.IN65.5693658536586
Andorra AND Life expectancy at birth, total (years)SP.DYN.LE00.IN
Afghanistan AFG Life expectancy at birth, total (years)SP.DYN.LE00.IN32.328512195122
Angola AGO Life expectancy at birth, total (years)SP.DYN.LE00.IN32.9848292682927

Now the column 'Country Code', 'Indicator Name', and 'Indicator Code' are not interesting. Let us remove them


In [40]:
del sheet.column[1:4]
sheet.top_left()


Out[40]:
pyexcel sheet:
Country Name1960 1961 1962 1963
Aruba 65.569365853658665.988024390243966.365536585365966.7139756097561
Andorra
Afghanistan 32.328512195122 32.777439024390233.219902439024433.6578780487805
Angola 32.984829268292733.386219512195133.787585365853734.1884634146342

In order to draw, x labels should be in one column and data series should be presented like columns. Hence, let us transpose the table


In [41]:
sheet.transpose()
sheet.top_left()


Out[41]:
pyexcel sheet:
Country NameAruba AndorraAfghanistan Angola
1960 65.5693658536586 32.328512195122 32.9848292682927
1961 65.9880243902439 32.777439024390233.3862195121951
1962 66.3655365853659 33.219902439024433.7875853658537
1963 66.7139756097561 33.657878048780534.1884634146342

Now let's select BRICS countries for viewing:


In [42]:
sheet.name_columns_by_row(0)
sheet.column.select(['Country Name', 'Brazil', 'Russian Federation', 'India', 'China', 'South Africa'])
sheet.top_left()


Out[42]:
pyexcel sheet:
Country Name Brazil China India Russian Federation
1960 54.205543.354 41.172 66.0553
1961 54.718743.673341.7905 66.597
1962 55.235744.398342.4174 67.0214
1963 55.751445.578943.0527 67.3399
1964 56.263 47.189643.6984 67.5665

We got a perfect data table. Let us draw it to a svg file:


In [43]:
from pygal.style import Style
sheet.plot(chart_type='line', 
           title='Life expectancy at birth in BRICS countries (years)',
           x_labels_major_count=10,
           x_label_rotation=30,
           style=Style(colors=["green", "red", "blue", "yellow", "grey"]),
           show_minor_x_labels=False)


Out[43]:
Life expectancy at birth in BRICS countries (years)444448485252565660606464686872721960196619721978198419911997200320092016Life expectancy at birth in BRICS countries (years)54.2054634111.331065198253336316.76119869906074196054.7187073221.44808769669381309.4606610973233196155.2356585431.56511019513429302.1073894882926196255.7513902441.682132693574765294.77146459218824196356.2629512251.79915519201525287.4948654542867196456.7673658561.916177690455726280.3199180541286196557.2656829372.0332001888962273.2317042185954196657.7579268382.15022268733668266.2298770134314196758.2406829392.26724518577716259.36300723482555196858.70802439102.38426768421763252.71539990759618196959.15058537112.50129018265811246.4202777870076197059.558122.61831268109859240.6250879329271197159.92480488132.73533517953905235.40754361925786197260.2515122142.85235767797957230.76035922657218197360.54109756152.96938017642003226.64120877533344197460.8005122163.08640267486052222.9512160018648197561.03912195173.20342517330099219.55715815090332197661.27082927183.32044767174145216.2612826950977197761.50646341193.43747017018197212.9095508236726197861.75497561203.55449266862243209.37463766375754197962.02029268213.67151516706292205.60068679973176198062.30436585223.78853766550338201.55994349091463198162.6022439233.90556016394385197.32283539178303198262.91146341244.02258266238434192.92440286244528198363.23207317254.1396051608248188.36395203438718198463.56256098264.2566276592653183.66299283163272198563.90046341274.3736501577058178.85656561429442198664.24582927284.4906726561462173.94397651385134198764.59821951294.60769515458674168.93147034695988198864.96119512304.72471765302726163.76839471187628198965.34031707314.8417401514677158.37564859766223199065.74268293324.95876264990824152.65227413509086199166.17180488335.07578514834864146.54831279093975199266.62768293345.19280764678916140.06376456520758199367.10682927355.30983014522957133.24824105686105199467.60319512365.4268526436701126.18778196200975199568.10973171375.5438751421106118.98265128136052199668.61639024385.66089764055107111.77578592942012199769.11309756395.77792013899153104.7104697549488199869.59129268405.89494263743297.90847668268412199970.04197561416.011965135872591.49782545401342200070.45960976426.128987634312985.55727014561774200170.8452439436.2460101327534480.07189258438336200271.20187805446.3630326311939674.99901985651081200371.532456.480055129634470.30326466763489200471.83963415466.597077628074965.92738286510058200572.13173171476.7141001265153561.77249818523501200672.41482927486.8311226249558757.745632246758134200772.69541463496.948145123396353.754500536908154200872.97802439507.0651676218367449.73457328360075200973.26414634517.182190120277345.664687497069224201073.55234146527.299212618717841.56531229856478201173.83958537537.416235117158337.47946753614093201274.12243902547.533257615598733.45607094024871201374.40187805557.650280114039129.481245140548253201443.35411.331065198253336471.11571965088234196043.6733414621.44808769669381466.5733094042662196144.3983414631.56511019513429456.26068857018095196245.5788780541.682132693574765439.468376589982196347.1895609851.79915519201525416.5575320267747196449.1393170761.916177690455726388.82360740182116196551.2821951272.0332001888962358.3426573180175196653.4298048882.15022268733668327.7944019880631196755.427512292.26724518577716299.37840461088706196857.19187805102.38426768421763274.2815272848342196958.68080488112.50129018265811253.10257854008842197059.90797561122.61831268109859235.64692825762717197160.95934146132.73533517953905220.6919801104761197261.90646341142.85235767797957207.21982898417718197362.76812195152.96938017642003194.96333549968483197463.55865854163.08640267486052183.71850231299385197564.28258537173.20342517330099173.421146586284197664.93653659183.32044767174145164.11914524722476197765.52321951193.43747017018197155.773988593127197866.04968293203.55449266862243148.28541262327815197966.5215122213.67151516706292141.5739693924831198066.93985366223.78853766550338135.6233529905918198167.3077561233.90556016394385130.39019663529012198267.6287561244.02258266238434125.82419485909514198367.91036585254.1396051608248121.81849191038776198468.15707317264.2566276592653118.30925188560104198568.37180488274.3736501577058115.25484267371547198668.55943902284.4906726561462112.58587742302984198768.72543902294.60769515458674110.22464285963935198868.88029268304.72471765302726108.02195725238562198969.0305122314.8417401514677105.88518915424868199069.18209756324.95876264990824103.72899273763585199169.33963415335.07578514834864101.4881443619463199269.50921951345.1928076467891699.07591046255612199369.69992683355.3098301452295796.36323149529869199469.92641463365.426852643670193.1415999708131199570.19982927375.543875142110689.2524669329635199670.52268293385.6608976405510784.6600981531218199770.89046341395.7779201389915379.4286764691139199871.29712195405.89494263743273.64424157704605199971.73046341416.011965135872567.48026060617224200072.17578049426.128987634312961.14593491437358200172.61487805436.2460101327534454.90007745849363200273.03312195446.3630326311939648.95084879363628200373.41995122456.480055129634443.44847145373387200473.76695122466.597077628074938.51263775797179200574.07170732476.7141001265153534.17769419794115200674.34043902486.8311226249558730.355172537723718200774.58326829496.948145123396326.901095060041087200874.80326829507.0651676218367423.77174804831867200975.00741463517.182190120277320.867908304625246201075.20217073527.299212618717818.097638250456043201175.39292683537.416235117158315.384265414681636201275.58514634547.533257615598712.650076523397729201375.78226829557.65028011403919.846153846153925201441.1719512211.331065198253336502.1538461538462196041.790487821.44808769669381493.3555933581382196142.4174146331.56511019513429484.43799517750614196243.0527317141.682132693574765475.4010516119457196343.6984146351.79915519201525466.2166609865223196444.353512261.916177690455726456.89835373731347196545.018512272.0332001888962447.43919117915254196645.6909268382.15022268733668437.87456060640636196746.367756192.26724518577716428.24713493286987196847.047102.38426768421763418.5853627677419196947.72707317112.50129018265811408.9117948378244197048.40741463122.61831268109859399.2344106310638197149.08697561132.73533517953905389.56812832057415197249.76029268142.85235767797957379.99066118026315197350.42341463152.96938017642003370.5582125600074197451.06792683163.08640267486052361.39047477899055197551.68946341173.20342517330099352.5495490694864197652.28463415183.32044767174145344.0836592934277197752.84902439193.43747017018197336.05560055160385197853.38070732203.55449266862243328.4927806504351197953.87470732213.67151516706292321.4659741786584198054.32853659223.78853766550338315.01056843064055198154.74460976233.90556016394385309.09221691479198255.13234146244.02258266238434303.5770030073242198355.49963415254.1396051608248298.35252000848413198455.86090244264.2566276592653293.21372977149645198556.2305122274.3736501577058287.9562880180988198656.61843902284.4906726561462282.4382986365652198757.03112195294.60769515458674276.56817098264645198857.47304878304.72471765302726270.2820691527795198957.94373171314.8417401514677263.5869318321339199058.43821951324.95876264990824256.5531866751878199158.94507317335.07578514834864249.3435458491782199259.45292683345.19280764678916242.11968071857199359.95482927355.30983014522957234.98046754703734199460.44436585365.4268526436701228.01715004456787199560.91560976375.5438751421106221.314033235976199661.36956098385.66089764055107214.85689281666578199761.80721951395.77792013899153208.6315044820385199862.22707317405.894942637432202.6593781561192199962.63063415416.0119651358725196.9190039148814200063.01985366426.1289876343129191.38262701764603200163.39919512436.24601013275344185.98675849510556200263.77453659446.36303263119396180.6477871909587200364.14780488456.4800551296344175.3383052987861200464.52387805466.5970776280749169.98892596688506200564.90809756476.71410012651535164.52367059264327200665.30043902486.83112262495587158.94288611031794200765.69943902496.9481451233963153.26738857542136200866.10263415507.06516762183674147.53221834806203200966.50614634517.1821901202773141.79253797534255201066.90417073527.2992126187178136.1309178107864201167.28987805537.4162351171583130.64449944677506201267.66041463547.5332576155987125.37387419155539201368.01380488557.6502801140391120.34714372006385201466.0552926811.331065198253336148.2056177438227196066.5970243921.44808769669381140.49986092814976196167.0214146331.56511019513429134.46320483014784196267.3399024441.682132693574765129.93293728257964196367.566512251.79915519201525126.70957108680153196467.7188536661.916177690455726124.5426197081747196567.812512272.0332001888962123.21039215551201196667.8601463482.15022268733668122.53282954865091196767.8743414692.26724518577716122.33091381020097196867.86712195102.38426768421763122.43360635071826196968.13365854112.50129018265811118.6423087737661197068.37658537122.61831268109859115.18684355904816197168.30853659132.73533517953905116.15479014027926197268.29463415142.85235767797957116.3525426676278197368.3202439152.96938017642003115.98826169619747197467.72390244163.08640267486052124.4708043166641197567.48756098173.20342517330099127.83259728158498197667.37634146183.32044767174145129.41461750037251197767.39097561193.43747017018197129.2064569452686197867.11439024203.55449266862243133.14069143672498197967.03390244213.67151516706292134.28557448979348198067.26390244223.78853766550338131.01398443208365198167.80609756233.90556016394385123.3016358654998198267.65268293244.02258266238434125.48385235150079198367.20268293254.1396051608248131.88478942093303198467.85682927264.2566276592653122.58001260780736198569.3897561274.3736501577058100.77519446071688198669.44284.4906726561462100.06050988819533198769.46439024294.6076951545867499.71357562969007198869.17170732304.72471765302726103.87678673175884198968.90243902314.8417401514677107.70694094566397199068.47439024324.95876264990824113.79563718244088199166.87317073335.07578514834864136.57187125334713199264.93585366345.19280764678916164.1288594064634199364.46707317355.30983014522957170.7969358549455199465.22121951365.4268526436701160.06972858194587199566.19414634375.5438751421106146.23052101014906199666.95073171385.66089764055107135.46862031129803199766.78390244395.77792013899153137.84165063947808199866.04365854405.894942637432148.37110538512917199965.34146341416.0119651358725158.35934268751254200065.48780488426.1289876343129156.27773713647673200165.08536585436.24601013275344162.00215240182348200265.00756098446.36303263119396163.1088726864558200365.42121951456.4800551296344157.22486766219833200465.47466.5970776280749156.53099914518634200566.64317073476.71410012651535139.843461311057200667.49756098486.83112262495587127.69035423559757200767.84878049496.9481451233963122.69450091311415200868.60487805507.06516762183674111.93953889943407200968.85609756517.1821901202773108.36611603682462201069.65853659527.299212618717896.9519789319827201170.36585366537.416235117158386.8908854353146201270.36585366547.533257615598786.8908854353146201370.36585366557.650280114039186.8908854353146201449.0362926811.331065198253336390.2890577097497196049.4298048821.44808769669381384.69162038301624196149.8132926831.56511019513429379.23677303653005196250.195756141.682132693574765373.7964969288996196350.581756151.79915519201525368.30591535378676196450.9718292761.916177690455726362.75739575750333196551.3630243972.0332001888962357.1929171853289196651.7494146382.15022268733668351.6967846620804196752.12692.26724518577716346.34011971075006196852.49326829102.38426768421763341.11598364617196952.85068293112.50129018265811336.0320090220249197053.19614634122.61831268109859331.1180321845493197153.53363415132.73533517953905326.31750284960367197253.87056098142.85235767797957321.52495300260426197354.21341463152.96938017642003316.6480981307893197454.57670732163.08640267486052311.48051235034416197554.97446341173.20342517330099305.822708462631197655.4122439183.32044767174145299.5955854567113197755.89260976193.43747017018197292.76271523543846197856.41404878203.55449266862243285.3456077228433197956.97307317213.67151516706292277.39387451789014198057.56263415223.78853766550338269.007779621287198158.16719512233.90556016394385260.40832015570425198258.76878049244.02258266238434251.85118666965792198359.35243902254.1396051608248243.54904986361328198459.91217073264.2566276592653235.58725556516447198560.4495122274.3736501577058227.943946916022198660.95802439284.4906726561462220.710714560435198761.42380488294.60769515458674214.08531102574926198861.82043902304.72471765302726208.44346611392865198962.12007317314.8417401514677204.18137874818416199062.29480488324.95876264990824201.69594172024813199162.32517073335.07578514834864201.26400856840894199262.19373171345.19280764678916203.13363728749619199361.88797561355.30983014522957207.48280515212542199461.37002439365.4268526436701214.85030106575488199560.60546341375.5438751421106225.72564926713716199659.61319512385.66089764055107239.83997570592874199758.44302439395.77792013899153256.48484062626187199857.15668293405.894942637432274.7821534198579199955.83687805416.0119651358725293.55546001612464200054.57080488426.1289876343129311.56447044090277200153.44119512436.24601013275344327.6323836893414200252.5237561446.36303263119396340.68231582303696200351.8737561456.4800551296344349.9281138122168200451.55734146466.5970776280749354.42889194781355200551.61370732476.71410012651535353.6271268764057200651.9965122486.83112262495587348.1819936891567200752.63602439496.9481451233963339.0853774311351200853.46709756507.06516762183674327.26393950680813200954.3907561517.1821901202773314.1255391371925201055.29565854527.2992126187178301.2539312123683201156.09831707537.4162351171583289.8366716992015201256.73658537547.5332576155987280.75774908836183201357.18212195557.6502801140391274.420300988236932014BrazilChinaIndiaRussian Federa…Russian FederationSouth Africa