473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Mar 27 '07 #1
3 6835
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/Coo.../Recipe/139422

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

Bye,
bearophile

Mar 27 '07 #2
Jon
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"""
Mar 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dr. Colombes | last post by:
MatPlotLib question: How to get more different size plot symbols in the plot function ? Is there a way to get different size squares (or circles or triangles, etc.) ? For example, in a two...
1
by: Dr. Colombes | last post by:
Using MatPlotLib plot function, is there a way to get variable size plot symbols? For example, using symbol strings like 'o' (circle), 's' (square), 'x' (cross), etc., is there a way to specify...
8
by: questions? | last post by:
I want to make a 3d plot. x is a vector(discrete), y is also a vector(discrete), for each pairwise x,y I have a value z(x,y)(it is not a function, just discrete values for each pair of x,y) I...
1
by: Bo Peng | last post by:
Dear list, I am using rpy, a python module to control statistical package R from python. Running the following commands >>> from rpy import * >>> r.plot(0) will pass command 'plot' to R...
3
by: python101 | last post by:
I would like to plot something like Sin(x), Cos(x), etc using python, I wonder if python has built-in graphic class for plot.
0
by: meggahertz | last post by:
I am using matplotlib to generate plot in python in linux. The porblem I am facing is that when I generate a plot using plot() then show() the script stops at that point until I close the plot...
1
by: dazzler | last post by:
Hi! I just moved using wxpython so I'm a quite newbie. I was wondering how to update plotcanvas? In my code I made button with event to update plotcanvas with new results, but how to properly do...
6
by: SpeedMath | last post by:
Actually, I already "compute out my wind vectors" in my case study by using Python. I couldn't find any information for plotting my wind vector field by using Python command.I...
1
by: oyinbo55 | last post by:
I am trying to use the pylab plot command on my laptop running Ubuntu 6.06 (Dapper). Although the plot command works fine on my XP desktop at work, I cannot open the plot window on the laptop. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.