21

I'm interested in drawing a treemap:

treemap example

What is the easiest way to make one in Python? Is there a library that could produce such a graphic, given the proper input data?

Jason Sundram
  • 10,998
  • 19
  • 67
  • 84
Primoz
  • 249
  • 1
  • 2
  • 4
  • 4
    7 years later it is still ridiculous that there is no decent python package able to plot a simple treemap. The answer listed below are still the state of the art but highly unhelpful. With R or js it is a matter of two lines of code, but why so complicated in python? I don't get it. – MERose Feb 22 '17 at 12:49
  • 2
    Actually there's [squarify](https://github.com/laserson/squarify), which makes it quite easy: https://python-graph-gallery.com/200-basic-treemap-with-python/ – Nickolay May 26 '18 at 18:06

4 Answers4

5

You can use:

1) Pygal package

It's simple: http://www.pygal.org/en/stable/documentation/types/treemap.html

2) squarify package

Uses matplotlib as plotting API. Example code:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import squarify

# qualtities plotted
# squarre area is the town surface area (superf)
# color scale is the town population in 2011 (p11_pop)

# read data from csv file
# data from CAPP opendata http://opendata.agglo-pau.fr/index.php/fiche?idQ=27
df = pd.read_excel("Customer Success New.xlsx")
df = df.set_index("location_id")
df = df[["user_id", "company_id"]]
df2 = df.sort_values(by="user_id", ascending=False)

# treemap parameters
x = 0.
y = 0.
width = 100.
height = 100.
cmap = matplotlib.cm.viridis

# color scale on the population
# min and max values without Pau
mini, maxi = df2.company_id.min(), df2.company_id.max()
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in df2.company_id]
colors[1] = "#FBFCFE"

# labels for squares
#labels = ["hab" % (label) for label in zip(df2.index, df2.user_id), df2.company_id)]
#labels[11] = "MAZERES" % (df2["user_id"]["MAZERES-LEZONS"], df2["company_id"]["MAZERES-LEZONS"])

# make plot
fig = plt.figure(figsize=(12, 10))
fig.suptitle("Population et superficie des communes de la CAPP", fontsize=20)
ax = fig.add_subplot(111, aspect="equal")
ax = squarify.plot(df2.superf, color=colors, label=labels, ax=ax, alpha=.7)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("L'aire de chaque carré est proportionnelle à la superficie de la commune\n", fontsize=14)

# color bar
# create dummy invisible image with a color map
img = plt.imshow([df2.p11_pop], cmap=cmap)
img.set_visible(False)
fig.colorbar(img, orientation="vertical", shrink=.96)

fig.text(.76, .9, "Population", fontsize=14)
fig.text(.5, 0.1,
         "Superficie totale %d km2, Population de la CAPP : %d hab" % (df2.superf.sum(), df2.p11_pop.sum()),
         fontsize=14,
         ha="center")
fig.text(.5, 0.07,
         "Source : http://opendata.agglo-pau.fr/",
         fontsize=14,
         ha="center")

plt.show()
smci
  • 26,085
  • 16
  • 96
  • 138
3

These could be used:

I noticed that there is a treemap library in pypi also. I haven't tried that one.

SiggyF
  • 18,472
  • 8
  • 36
  • 55
  • The matlab approach is in fact a SciPy cookbook available at http://scipy-cookbook.readthedocs.io/items/Matplotlib_TreeMap.html – MERose Feb 22 '17 at 12:17
2

Here are a couple of options:

ars
  • 106,073
  • 21
  • 135
  • 131