Draw dendrogram in python manually
Asked Answered
A

4

9

I have implemented an algorithm to solve the problem of clustering in a graph. I used the python library "python-graph" to represent the graph. Now, at each step of my computation (the algorithm is iterative) I have to draw a part of the dendrogram. In fact, the algorithm is divisive, in the sense that starting from the original graph calculates the clusters. Now, I don't know what to use to draw the dendrogram (someone suggested PIL, but I'm looking for something easy and I don't know how to use PIL)... can you suggest something and show me how to do plot with it?

Note: I read other questions but everything seems to use methods that use automatic computation of the clusters... this is not what I'm looking for: I need to manually draw the dendrogram or at least find a way to represent the clusters computed to be drawn.

Thanks!

Affected answered 26/6, 2011 at 13:57 Comment(0)
V
3

Code to implement scipy dendrogram can be found here and this simple implementation will help you to move on.

Venable answered 26/6, 2011 at 14:4 Comment(3)
It's a it hard to find a way, it's a lot of code to look at and I still can't figure out how to draw the dendrogram while generating it... It's not easy to define image size without knowing the final result. I think a better solution is to create the tree, represent it in some way and finding a function to plot it. See here: #5958125Affected
@Raffo: Did you check detailed manner gist.github.com/846567? IMHO it's very simple and you should quite easily be able to extract from it what you really need. ThanksVenable
I have to dig in the code a little bit, I didn't look at it with attention.. I just saw that there are almost no comments at all..Affected
M
3

perhaps an other solution could be this one: http://ete.cgenomics.org/ I recommend you the main help pdf to start: http://ete.cgenomics.org/releases/ete2/doc/ete_tutorial.pdf

Marianelamariani answered 26/6, 2011 at 17:40 Comment(0)
I
3

The ETE python toolkit gives you a lot of possibilities for tree drawing. The drawing engine allows for programmatic tree rendering. Trees can be plotted as PNG or SVG images. Dendrograms can be represented as rectangular or circular tress.

Although ETE is commonly used to deal with phylogenetic trees, it provides also a clustering module, with several special predefined visualization modes.

Check some examples at http://packages.python.org/ete2/tutorial/tutorial_drawing.html

Interlude answered 13/12, 2011 at 14:53 Comment(0)
A
1

You can use the plotting function inside scipy dendrogram. For this you would need to generate the same data as output by scipy dendrogram. Example:

from scipy.cluster.hierarchy import dendrogram,  _plot_dendrogram
# Generate data for plot without plotting:
ddata = dendrogram(linked_data, no_plot=True)  

MaxVerticalAxis = 120  # You can choose max value or take it from ddata["ddcoord"]

#Plotting command:
_plot_dendrogram(ddata["icoord"], ddata["dcoord"], ddata["ivl"], 30,20, MaxVerticalAxis,  "top", False, ddata["color_list"], leaf_font_size=None, leaf_rotation=None, contraction_marks=None, ax=None, above_threshold_color='C0')
Anthozoan answered 5/5, 2020 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.