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]:
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]:
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]:
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]:
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]:
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]: