Countries by Lowest Point of Altitude on Land

IPython Notebook to retrieve, process, display and plot data on lowest point of altitude on land for 80 countries and Antarctica from photius.com, which compiled the data from the CIA World Fact Book 2014.

First import necessary libraries, setup geonames mapper, download data and turn it into a pandas DataFrame.

In [1]:
import pandas as pd
from lxml import html
from geonamescache.mappers import country

mapper = country(from_key='name', to_key='iso3')

url ='http://www.photius.com/rankings/geography/elevation_extremes_lowest_point_2014_0.html'
xpath = '//table'

tree = html.parse(url)
tables = tree.xpath(xpath)
raw_html = html.tostring(tables[4])
df = pd.read_html(raw_html, header=0)[0][['Country', 'Value']]

Process data

Add an iso3 column using the geonamescache mapper function created above, needed for rendering the d3 based map and set more meaningful column names.

In [2]:
df['iso3'] = df['Country'].apply(lambda x: mapper(x))
df.dropna(subset=['iso3', 'Value'], inplace=True)
df.set_index('Country', inplace=True)
df.columns = ['Altitude', 'iso3']
df.head()
Out[2]:
Altitude iso3
Country
Lesotho 1400 LSO
Rwanda 950 RWA
Andorra 840 AND
Burundi 772 BDI
Uganda 621 UGA

Display and plot highest low points

First set some common plotting properties.

In [3]:
footer = 'CC BY-SA 2014 Ramiro Gómez - ramiro.org • Data: www.photius.com/rankings/geography/elevation_extremes_lowest_point_2014_0.html'
mpl.rcParams['font.size'] = 11
mpl.rcParams['font.family'] = 'Ubuntu'
mpl.rcParams['axes.color_cycle'] = 'a6cee3, 1f78b4, b2df8a, 33a02c, fb9a99, e31a1c, fdbf6f, ff7f00, cab2d6'

Show highest low points

In [4]:
df.sort('Altitude', inplace=True)
df.tail(10)
Out[4]:
Altitude iso3
Country
Central African Republic 335 CAF
Armenia 400 ARM
Liechtenstein 430 LIE
Botswana 513 BWA
Mongolia 560 MNG
Uganda 621 UGA
Burundi 772 BDI
Andorra 840 AND
Rwanda 950 RWA
Lesotho 1400 LSO

Plot highest low points

In [5]:
s = df['Altitude'][-10:]
s.plot(kind='barh', figsize=(10, 6), title='Countries by Highest Low Point of Altitude on Land\n', fontsize='large')

ax = plt.axes()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_color((1, 1, 1))
ax.xaxis.set_label_text(footer)
ax.xaxis.set_ticklabels('')
ax.yaxis.set_label_text('')

for i, x in enumerate(s):
    ax.text(x + 10, i - .1, int(x), ha='left', fontsize='large')

plt.savefig('../static/img/graphs/highest-low-point-altitude-countries.png', bbox_inches='tight')

Show lowest low points

In [6]:
df.head(10)
Out[6]:
Altitude iso3
Country
Antarctica -2540 ATA
Israel -408 ISR
Jordan -408 JOR
Syria -200 SYR
Djibouti -155 DJI
China -154 CHN
Egypt -133 EGY
Kazakhstan -132 KAZ
Kyrgyzstan -132 KGZ
Ethiopia -125 ETH

Plot lowest low points

In [7]:
s = df['Altitude'][:10].apply(lambda x: x * -1)
s.plot(kind='barh', figsize=(10, 6), title='Countries by Lowest Low Point of Altitude on Land\n', fontsize='large')

ax = plt.axes()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_color((1, 1, 1))
ax.xaxis.set_label_text(footer)
ax.xaxis.set_ticklabels('')
ax.yaxis.set_label_text('')

for i, x in enumerate(s):
    ax.text(x + 20, i - .1, int(-x), ha='left', fontsize='large')

plt.savefig('../static/img/graphs/lowest-low-point-altitude-countries.png', bbox_inches='tight')
In [8]:
df.to_csv('../static/data/csv/lowest-altitude-countries.csv', encoding='utf-8', index=False)

Map Preview


Ramiro Gómez

About this post

This post was written by Ramiro Gómez (@yaph) and published on October 21, 2014.


blog comments powered by Disqus