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

  • If you want slope to be available anywhere on your system :
python setup.py install
  • Else, just set the working directory where plotSlope.py is.

Then you are good to go. Import it in a Python interpreter (for instance Spyder, the Scientific python IDE bundled with Anaconda, or a jupyter notebook):

from plotSlope import slope

Usage

Import :


In [4]:
from plotSlope import slope

Load data from file into a data frame :


In [7]:
data = pd.read_csv(os.path.join('data','EU_GDP_2007_2013.csv'),index_col=0,na_values='-')
(data/1000).head()


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

Plot it :


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



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



In [10]:
f = slope(data/1000, title = u'European GPD until 2010 and forecasts at market prices (billions of Euro) source : EUROSTAT',
          kind='interval',height= 12,width=30,font_size=20,
          savename=None,color=color)


 Other example : Random data


In [11]:
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 [12]:
f = slope(df.T,width =10,height= 8,kind='ordinal',savename=None,dpi=200,color={'10':'red','27':'blue'},marker=None)