Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

plot dendrogram with python


Hi,

does anyone know if there is a way to plot a dendrogram with python.
Pylab or matplotlib do not provide such a function.

Thanks!

Frank

Frank:

> does anyone know if there is a way to plot a dendrogram with python.
> Pylab or matplotlib do not provide such a function.

An ASCII solution:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/139422

Some graphics:
http://plone.org/products/phylogenetictree
http://www.bioinformatics.org/mavric/

Bye,
bearophile

I use pycluster

http://bonsai.ims.u-tokyo.ac.jp/~mdehoon/software/cluster/software.ht...

> does anyone know if there is a way to plot a dendrogram with python.
> Pylab or matplotlib do not provide such a function.

This makes a datafile for gnuplot using output from pycluster. I'd be
interested to see something like this added to pylab/matplotlib,
although I don't have time myself. Not very elegant, but someone can
probably transform it to the three line recursion which escapes me.

Best,

Jon

import Numeric
from Pycluster import treecluster
dist = Numeric.zeros((10,10),Numeric.Float)
for i in range(dist.shape[0]):
   dist[i:,i:]=i

tree , dist = treecluster(distancematrix=dist,method='a')

tree=tree.tolist()
base = []
line = []
names = range(dist.shape[0]+1)

def f(i,tree,names,spos):
   height=dist[spos]
   if i>=0:
      try:
         base.append(names[i])
      except:
         print i
      x=len(base)
      line.append((x,0))
      line.append((x,height))
      line.append(("#","#"))
   else:
      cluster = tree[-i-1]
      h1,x1=f(cluster[0],tree,names,-i-1)
      h2,x2=f(cluster[1],tree,names,-i-1)
      x=(x1+x2)/2.0
      if h1==h2:
         # tie
         line.append((x1,h1))
         line.append((x2,h2))
         line.append(("#","#"))
         line.append((x,height))
         line.append((x,h1))
         line.append(("#","#"))
      else:
         raise Exception("Whoops")
      tree[-i-1].append((x,h1))
   return height,x

h1,x1 = f(tree[-1][0],tree,names,len(tree)-1)
h2,x2 = f(tree[-1][1],tree,names,len(tree)-1)
x=(x1+x2)/2.0
height = dist[-1]
tree[-1].append((x,h1))
if h1==h2:
         # tie
         line.append((x1,h1))
         line.append((x2,h2))
         line.append(("#","#"))
         line.append((x,height))
         line.append((x,h1))
         line.append(("#","#"))
else:
         raise Exception("Whoops")

# print base

d=open("dend.dat","w")
# make a tree diagram
for point in line:
   if point[0]!="#":
      print >> d, point[0],point[1]
   else:
      print >> d
      print >> d

d.close()

#
# os.system("gnuplot")
# """plot "dend.dat" u 1:2 w l"""

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc