graphical visualisation of multiindex

45 views
Skip to first unread message

Francis Wolinski

unread,
Sep 26, 2016, 7:07:43 AM9/26/16
to PyData
Hi pandas team,

What about a graphical visualization of multiindex?

I implemented something very simple but working using ete3 lib.

A full example (in French) is available here: http://www.yotta-conseil.fr/python/Mise-a-plat-de-donnees-opendata-en-Python-v2.html

Here is the code:

Cheers,

Francis

from ete3 import Tree, TreeStyle, TextFace, add_face_to_node

def make_tree(multi_index):
    # specific textstyle
    def my_layout(node):
            tf = TextFace(node.name, tight_text=True, ftype='Calibri', fsize=12)
            add_face_to_node(tf, node, column=0, position="branch-right")

    ts = TreeStyle()
    ts.show_leaf_name = False
    ts.show_scale = False
    ts.branch_vertical_margin = 3
    ts.layout_fn = my_layout

    # tree construction
    t = Tree()
    for branch in zip(*multi_index.labels): # loop on all branches
        current_node = t
        for i, j in enumerate(branch): # loop on each branch from root to leaf
            # node name
            node_name = multi_index.levels[i][int(j)]
            nodes = current_node.search_nodes(name=node_name)
            if nodes: # node exists and becomes current node
                current_node = nodes[0]
            else: # node doesn't exist and becomes current node
                child = Tree(node_name+';', format=8)
                current_node.add_child(child=child,dist=1)
                current_node = child
    return t, ts
   

t, ts = make_tree(df.columns)
t.render("tree.png", tree_style=ts) # or t.render("%%inline", tree_style=ts) in Jupyter notebook
tree1.png
tree2.png
Reply all
Reply to author
Forward
0 new messages