Corrplot demonstration


In [1]:
# some useful pylab imports for this notebook
%pylab inline
import pandas as pd
matplotlib.rcParams['figure.dpi'] = 120
matplotlib.rcParams['figure.figsize'] = (8,6)


Populating the interactive namespace from numpy and matplotlib

In [2]:
from biokit.viz import corrplot

In [3]:
# for debugging
# _ = reload(corrplot)

Let us create some dummy data set


In [24]:
import string
letters = string.ascii_uppercase[0:15]
df = pd.DataFrame(dict(( (k, np.random.random(10)+ord(k)-65) for k in letters)))
df = df.corr()

In [25]:
# if the input is not a square matrix or indices do not match 
# column names, correlation is computed on the fly
c = corrplot.Corrplot(df)

In [26]:
c.plot(colorbar=False, method='square', shrink=.9 ,rotation=45)


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [27]:
c.plot(method='text', fontsize=8, colorbar=False)
# only red to blue colormap is implemented so far


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [28]:
c.plot(method='color') # shrink not available


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [29]:
c.plot(method='pie', shrink=.9, grid=False)


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [30]:
c.plot(colorbar=False, method='circle', shrink=.9, lower='circle',
       label_color='red'  )


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [31]:
c.plot(colorbar=False, shrink=.8, upper='circle'  )


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [32]:
c.plot(colorbar=False, method='circle', shrink=.8, upper='circle' , 
       lower='square' )
# ignore the method if upper and lower are provided
# todo: option to set labels on diagonal


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [33]:
c.order(inplace=True)


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [34]:
c.plot(method='circle')


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [35]:
# with ellipses, rotation of the patches helps to see the different patterns

In [36]:
c.plot(cmap=('orange', 'white', 'green'))


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [37]:
# More tuning with shrink and fontsizes

In [38]:
df = [[1,.5,-.5],[.5,1,-.1],[-.5,-.1,1]]
c = corrplot.Corrplot(df)
c.plot(method='square', cmap=('red', 'white', 'blue'), 
       fontsize=30, shrink=1, colorbar=False)


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

subplot and axes


In [39]:
fig = plt.figure()
fig.subplots_adjust(left=0.2, wspace=0.6)
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

import numpy as np
c = corrplot.Corrplot(np.random.rand(10,10))
c.plot(fig=fig, ax=ax3, colorbar=True, cmap='copper', method='rectangle')
c.plot(fig=fig, ax=ax4, colorbar=True, cmap='jet', method='color')
c.plot(fig=fig, ax=ax2, colorbar=True, cmap='hot', method='ellipse')
c.plot(fig=fig, ax=ax1, colorbar=True, method='pie')


/home/cokelaer/Work/github/biokit/biokit/viz/linkage.py:41: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  Y = hierarchy.linkage(D, method=method, metric=metric)

In [ ]: