April 16th, 2014

python treemap module – PyTreeMap

As part of a couple different projects I’m working (and *hoping* to release sometime soon), I’ve created/implemented a simple treemap module for python (Code here). There are a few python treemap modules already, but I couldn’t find a simple one with minimal prerequisites and that implements anything other than the “squarified layout”. Since I couldn’t find one when I was looking, I thought it might be worth releasing independently of the other work. Note that the module does not handle the actual graphing, it’s intentionally just the layout calculations (I do have pygame and jython/processing.org test implementations for the graphing, so if you’re interested in those email me).

There are many different layout algorithms for treemap graphs optimized for various features, and they have evolved over the years. This is a good page on the history of treemaps. The algorithm for this module is the “split” layout and was taken from this great paper on treemaps. Chapter 5 covers several different algorithms with their various features and implementation details.

Here’s a sample of how to use the module:

    from PyTreeMap import SimpleTreeMap
    # Arbitrary list of numbers
    # Give the treemap its coordinates and title
    root = SimpleTreeMap(x=0, y=0, w=100, h=100, title="RootNode")
    # Add each of the items giving it a size or weight equivalent to its value
    for i in items:

    # Add a couple children to two different nodes

    print " [*] Setup [%s] top-level items to layout" % len(items)

    # Iterate over treemap nodes and their children nodes
    for i in root:
        print " [*] Coordinates are x [%s] y [%s] w [%s] h [%s]" % i.getCoordinates()
        for j in i:
            print " [*] -- Child Coordinates are x [%s] y [%s] w [%s] h [%s]" % j.getCoordinates()

And this will output the following:

 [*] Setup [19] top-level items to layout
 [*] Laying out now...
 [*] Coordinates are x [0.0] y [0.0] w [1.53579926455] h [14.5017095894]
 [*] Coordinates are x [1.53579926455] y [0.0] w [33.78758382] h [14.5017095894]
 [*] -- Child Coordinates are x [1.53579926455] y [0.0] w [12.9952245462] h [14.5017095894]
 [*] -- Child Coordinates are x [14.5310238107] y [0.0] w [20.7923592739] h [14.5017095894]
 [*] Coordinates are x [0.0] y [14.5017095894] w [31.6438640133] h [30.2644374039]
 [*] -- Child Coordinates are x [0.0] y [14.5017095894] w [12.1707169282] h [30.2644374039]
 [*] -- Child Coordinates are x [12.1707169282] y [14.5017095894] w [19.4731470851] h [30.2644374039]
 [*] Coordinates are x [31.6438640133] y [14.5017095894] w [3.67951907131] h [30.2644374039]
 [*] Coordinates are x [35.3233830846] y [0.0] w [38.8059701493] h [44.7661469933]
 [*] Coordinates are x [74.1293532338] y [0.0] w [9.53129091385] h [4.67338897183]
... (snip)

The previous output shows the x,y,w,h coordinates for each block of the graph including the child nodes. Children can be added arbitrarily deep. You can find the source for the module here.

Just so you can see what a treemap looks like, this is a random screenshot from a small project I’m hoping to release soon (ignore the colors):

Let me know if you have any feedback/improvements/etc.

28 Responses to 'python treemap module – PyTreeMap'

  1. 1Jonathan
    September 23rd, 2011 at 8:44 pm

    Hi there, is the code for this still available? The links are dead on this page. Thanks.

  2. 2Amy
    October 12th, 2011 at 9:17 pm

    I’m also looking for this code. Any chance it will be put back up?

  3. 3Carlos
    July 4th, 2012 at 2:39 am

    Hi Aaron,
    I can’t find the source code for this post.
    The requested URL /svn/hackery/trunk/lib/PyTreeMap.py was not found on this server.

    If possible can you correct the link, or send me your source code for PyTreeMap.py

    I’m trying to find a treemap algorithm in python to layout different sized images, and change to scale of individual images based on imageAttributeRating.

    Thank you.

  4. 4freight factoring for brokers
    September 20th, 2013 at 8:54 am

    Its like you read my mind! You seem to know so
    much about this, like you wrote the book in it or something.

    I think that you can do with some pics to drive the message home a little bit, but instead of that, this is
    wonderful blog. A great read. I will certainly be back.

  5. 5??? ???????? ??????? ? 40 ???
    September 22nd, 2013 at 1:11 pm

    It’s a pity you don’t have a donate button! I’d most certainly donate to
    this outstanding blog! I suppose for now i’ll
    settle for book-marking and adding your RSS feed
    to my Google account. I look forward to fresh updates and will talk about this site with
    my Facebook group. Talk soon!

  6. 6free iphone 5 today
    October 6th, 2013 at 3:47 am

    My partner and I stumbled over here from a different website
    and thought I should check things out. I like what I see so i am just following
    you. Look forward to checking out your web page for
    a second time.

  7. 7save22
    October 15th, 2013 at 4:19 pm

    I visited several sites except the ajdio quality for audio songs current at this webb site is
    really superb.

  8. 8Garcinia Cambogia
    October 21st, 2013 at 2:53 pm

    Thiis is really interesting, You are a very skilled blogger.
    I’ve joined your rss feed and look forward to
    seeking more of your fantastic post. Also, I have shared your website in my social networks!

  9. 9??? 24 ??????????? ????
    October 22nd, 2013 at 4:19 pm

    Awesome! Its truly awesome paragraph, I have got much clear idea concerning from this post.

  10. 10sttto.ru
    October 22nd, 2013 at 4:20 pm

    Aw, this was an extremely nice post. Spending some time and actual
    effort to generate a great article… but what can I say… I hesitate a
    lot and don’t manage to get anything done.

  11. 11??????? ??? ??? Daihatsu
    November 4th, 2013 at 1:07 am

    Oh my goodness! Amazing article dude! Thank you, However I am going
    through issues with your RSS. I don’t know why
    I cannot join it. Is there anyone else getting identical RSS issues?
    Anybody who knows the answer will you kindly respond?


  12. 12Katja
    November 7th, 2013 at 11:57 am

    I need to to thank you for this excellent read!!
    I certainly enjoyed every bit of it. I have
    got you bookmarked to check out new stuff you post…

  13. 13???????????? ???
    November 10th, 2013 at 12:53 am

    I’m really enjoying the theme/design of your web site. Do you ever
    run into any web browser compatibility problems? A few of my blog readers have complained about my website not working correctly in
    Explorer but looks great in Safari. Do you have any ideas to help fix this problem?

  14. 14???????? ????????
    November 10th, 2013 at 1:47 am

    I know this site offeers quality dependent posts and other stuff, is there any other site which offers such data in quality?

  15. 15benumadi.hyperphp.com
    November 10th, 2013 at 10:18 pm

    Its not my first time to pay a visit this website, i am browsing this web page
    dailly and get pleasant facts from here all the time.

  16. 16Symbian sms Tracker
    November 11th, 2013 at 6:31 am

    Fantastic beat ! I wish to apprentice even as you amend your web site,
    how could i subscribe for a blog web site? The account aided me a appropriate deal.

    I had been a little bit familiar of this your broadcast provided vibrant transparent concept

  17. 17samsung locator
    November 11th, 2013 at 11:31 am

    I for all time emailed this blog post page to all my contacts,
    for the reason that if like to read it then my links will too.

  18. 18phone surveillance
    November 11th, 2013 at 6:20 pm

    Hi there, I enjoy reading all of your article. I
    wanted to write a little comment to support you.

  19. 19Htc Sms Tracker
    November 12th, 2013 at 6:30 am

    Hello to all, it’s really a fastidious for
    me to pay a visit this web site, it includes valuable Information.

  20. 20Ernie
    November 12th, 2013 at 6:36 am

    At this time I am going to do my breakfast, afterward having my breakfast coming yet again to read other news.

  21. 21symbian sms tracker
    November 15th, 2013 at 3:31 am

    If you are going for best contents like me, only pay a quick visit this
    site all the time as it offers feature contents,

  22. 22spy app
    November 15th, 2013 at 1:02 pm

    I visited many blogs however the audio quality for audio songs current at this website is truly

  23. 23iphone tracking
    November 15th, 2013 at 5:55 pm

    Pretty section of content. I just stumbled upon your website and
    in accession capital to assert that I acquire actually enjoyed account your blog posts.
    Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.

  24. 24mobile spyware
    November 17th, 2013 at 3:13 pm

    Hey are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and create my
    own. Do you require any html coding knowledge to make your own blog?
    Any help would be really appreciated!

  25. 25seo consultant
    March 20th, 2014 at 12:47 am

    Within seven years we’ve got gained a respectable repute among the world’s leading brands.

    Some job descriptions are poorly written or purposely worded
    vaguely to draw a wider applicant pool. Weeprxis help guide
    everybody about On page SEO and Off Page SEO.

  26. 26write my essay canada
    April 1st, 2014 at 5:56 pm

    Hi! I’ve been following your site for a long time now
    and finally got the bravery to go ahead and give you a shout
    out from Porter Texas! Just wanted to mention
    keep up the fantastic work!

  27. 27write Me paper
    April 1st, 2014 at 6:37 pm

    Hi there, just became aware of your blog through Google, and found that it’s
    really informative. I am going to watch out for brussels.
    I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing.


  28. 28marc by marc jacobs
    April 9th, 2014 at 2:45 am

    Litt kjedelig med enkel finale- Ja,ray ban, det er vel egentlig litt kjedelig med en enkel finale. Vil ikke involveresHan sier det er uaktuelt å spille en aktiv rolle i Hjellegjerde utover å sikre flertall for emisjonen. I oktober var han p fotosession med herremagasinet i New York. Pluss- og minuspoeng ut fra vindforhold og avsats tas også med i beregningen, og kan av og til gjøre det hele til en forvirrende øvelse for TV-seerne. Kvinners rettigheter i denne lovgivninge

Leave a Response

Imhotep theme designed by Chris Lin. Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS