0

I have a treemap that has small enough sections that the labels overlap. Is there any way to move the labels for sections under size=4 (or something around that) to either outside of the plot with an arrow pointing to it, or into a small legend only containing the labels for the small portions?

The treemap generated and code is below.

import squarify #pip install squarify
import matplotlib.pyplot as plt

labels=["longlabel1","longlabel2","longlabel3","longlabel4","longlabel5","longlabel6","longlabel7","longlabel8","longlabel9","longlabel10","longlabel11","longlabel12",]
sizes=[1.8,1.3,10.5,13.8,7.8,6.7,9.9,12.2,12.7,10.9,7.6,4.8]

x=dict(zip(labels,sizes))
sortedDict=dict(sorted(x.items(),key=lambda item:item[1],reverse=True))

squarify.plot(sizes=list(sortedDict.values()),color=['red','blue','cyan','black','gray','green'],label=list(iter(sortedDict)),alpha=.8)

plt.axis('off')
plt.show

enter image description here

Cole MG
  • 317
  • 2
  • 13

1 Answers1

1

Maybe you can use the matplotlib-extra package, which includes a treemap function to plot a hierarchical treemap.

For your case, it's simple:

import matplotlib.pyplot as plt

import mpl_extra.treemap as tr

labels=["longlabel1","longlabel2","longlabel3","longlabel4","longlabel5",
        "longlabel6","longlabel7","longlabel8","longlabel9","longlabel10",
        "longlabel11","longlabel12",]
sizes=[1.8,1.3,10.5,13.8,7.8,6.7,9.9,12.2,12.7,10.9,7.6,4.8]

fig, ax = plt.subplots(figsize=(7,7), dpi=100, subplot_kw=dict(aspect=1.156))

tr.treemap(ax, sizes, labels=labels, 
           fill=labels, cmap=['red','blue','cyan','black','gray','green'],
           rectprops=dict(ec='w'), 
           textprops=dict(c='w'))

ax.axis('off')

The following is the obtained figure:

enter image description here

Z-Y.L
  • 1,740
  • 1
  • 11
  • 15