E. Tufte Slope Graphs contest

So here is my entry for the slope Graph contest. (You can find the initial bounty description here )

Installation

Dependancies

This script is written in Python and relies on Numpy, Pandas and Matplotlib.
The easiest way to have a clean and robust install is to download one the great Scientific Python distribution, nameley :

Everything you'll need is included. I personally use Anaconda from the guys at Continuum Analytics. All of them should work on Linux, Windows and Mac.

Download the sources

Go grab the sources at https://github.com/pascal-schetelat/Slope

Launch Spyder, the Scientific python IDE bundled with Anaconda.
Set the working directory where plotSlope.py is and import it in the console :

from plotSlope import slopeGrid as slope

And you are good to go.

Usage

Load data from file into a data frame :

data = pd.read_csv(os.path.join('Data','EU_GDP_2007_2013.csv'),index_col=0,na_values='-')

Plot it

f = slope(data/1000,kind='interval',height= 12,width=20,font_size=12,dpi=150,savename='EU_interval.png',title = u'title')

Interactive example


In [4]:
from plotSlope import slope

# Load some additional lib to load the files 
import os
import pandas as pd

In [6]:
# Load data from a csv file and display it 
data = pd.read_csv(os.path.join('Data','EU_GDP_2007_2013.csv'),index_col=0,na_values='-')
data/1000


Out[6]:
2007 2008 2009 2010 2011 2012 2013
Austria 274.0198 282.7460 274.8182 286.1973 300.8913 310.1333 322.1904
Belgium 335.6100 346.1300 340.3980 354.3780 370.4364 381.7799 396.2738
Bulgaria 30.7724 35.4305 34.9328 36.0335 38.9899 NaN NaN
Croatia 43.3804 47.7602 45.6661 45.8992 46.0216 46.7810 48.1752
Cyprus 15.9015 17.1571 16.8535 17.3336 17.9286 18.4096 19.1675
Czech Republic 131.9086 154.2697 141.4498 149.3133 157.5383 155.6888 160.0309
Denmark 227.5339 235.1330 223.9853 235.6086 241.1481 249.1229 258.1553
Estonia 16.0694 16.3042 13.8396 14.3053 16.0116 17.0060 18.1639
Finland 179.8300 185.6510 173.2670 180.2530 190.2574 198.2515 206.1155
France 1886.7921 1933.1950 1889.2310 1932.8015 1987.6994 2027.9698 2092.9284
Germany 2428.5000 2473.8000 2374.5000 2476.8000 2570.0000 2626.4279 2705.1811
Greece 222.7711 232.9203 231.6420 227.3179 217.8288 212.1399 214.3092
Hungary 99.4305 105.5451 91.4025 97.0948 99.2855 95.3987 100.0595
Ireland 189.9329 179.9898 160.5959 155.9923 156.1092 158.8649 164.6216
Italy 1554.1989 1575.1439 1526.7904 1556.0286 1586.2090 1617.1547 1660.1336
Latvia 21.0265 22.8898 18.5213 17.9748 19.6059 20.7016 21.8880
Lithuania 28.7388 32.4617 26.6544 27.6075 30.8069 32.7818 34.5452
Luxembourg 37.4905 39.4365 37.3926 40.2669 41.7783 42.8930 45.0359
Malta 5.4343 5.8146 5.8127 6.1542 6.4400 6.6900 7.0083
Netherlands 571.7730 594.4810 571.1450 588.4140 607.4352 622.7140 639.5636
Poland 311.0017 363.1537 310.4182 354.3100 369.3183 355.3463 372.4172
Portugal 169.3192 171.9831 168.5036 172.5712 171.6324 168.2864 172.6477
Romania 124.7285 139.7654 118.1960 124.0589 131.5278 136.2781 147.8008
Slovakia 54.8108 64.5001 62.8955 65.8874 69.9445 71.6140 75.1231
Slovenia 34.5623 37.2795 35.3106 35.4158 36.4460 38.0184 NaN
Spain 1053.1610 1087.7490 1047.8310 1051.3420 1074.9405 1094.2900 1123.4959
Sweden 337.9442 333.2557 291.3470 346.5364 386.2016 396.1884 409.5825
United Kingdom 2054.2377 1800.7108 1564.4679 1706.3019 1747.3156 1862.1907 1931.4076

In [11]:
color  = {"France":'b','Germany':'r','Ireland':'chocolate','United Kingdom': 'purple'}
f = slope(data/1000,kind='interval',height= 18,width=20,font_size=15,savename='test.png',color=color,title = u'European GPD until 2010 and forecasts at market prices (billions of Euro) source : EUROSTAT')



In [20]:
f = slope(data/1000,width =30,height= 12,kind='interval',font_size=20,color=color,savename=None,dpi=200)


Other example : Random data


In [15]:
df = pd.DataFrame( np.random.normal(loc=np.ones(shape=[20,30])*np.arange(30)))
df.rename(columns = lambda el : str(el),index =lambda el : str(el),inplace=True)

In [16]:
f = slope(df.T,width =10,height= 8,kind='ordinal',savename=None,dpi=200,color={'10':'red','27':'blue'},marker=None)