Search code examples
pythonmatplotlibgraphtreemap

TreeMap In Python - Vertical and Horizontal


I have code below which simply creates a Treemap in Python. Can anyone help me in creating only all Vertical Rectangles in Treemap or Horizontal using below code sample.

import matplotlib.pyplot as plt
import squarify
volume = [350, 220, 170, 150, 50]
labels = ['A', 'B', 'C', 'D', 'E']
color_list = ['#0f7216', '#b2790c', '#ffe9a3', '#f9d4d4', '#d35158', '#ea3033']
plt.rc('font', size=14)
squarify.plot(sizes=volume, label=labels, color=color_list, alpha=0.7)
plt.axis('on')
plt.show()

Current sample graph in Python

it should look like horizontal or vertical stack Bar chart. but any option to create using Treemap in Python. because i want to modify it further. Below is the proposed look.

Desired vertical output Desired vertical output

Desired horizontal output

Desired horizontal output

Appreciate your help.


Solution

  • You can use norm_x and norm_y to specify the shape of the rectangle to fit the data into (default is a 100 x 100 square, hence the name squarify):

    Horizontal:

    squarify.plot(sizes=volume, norm_x=100, norm_y=10, label=labels, color=color_list, alpha=0.7)
    

    Vertical:

    squarify.plot(sizes=volume, norm_x=10, norm_y=100, label=labels, color=color_list, alpha=0.7)
    

    See the documentation for details.