How to edit the label font sizes on building a treemap with squarify in Python?
Asked Answered
P

3

9

I am using a squarify package in python, codeLink, to draw a treemap, wikiTreemapArticle. Based on this example, I can produce a treemap, but I am unable to see how the fontsizes of the labels in the squares can be modified. The essential line in the script is:

ax = squarify.plot(countryPop, color=colors, label=labels, ax=ax, alpha=.7)

From here I cannot add the 'fontsize' attribute. How would I change the sizes of the labels?

Psychodiagnosis answered 23/10, 2016 at 21:4 Comment(0)
D
14

Update: There is now a possibility to change the fontsize (squarify==0.3.0 or higher) via the text_kwargs parameter:

ax = squarify.plot(countryPop, color=colors, label=labels, ax=ax, bar_kwargs={'alpha':.7}, text_kwargs={'fontsize':10})
Dandrea answered 3/12, 2018 at 9:8 Comment(0)
L
6

the squarify works on matplotlib pyplot so you just need change de font size of pyplot.

I use to do the following thing on my plotting codes.

SMALL_SIZE = 13
MEDIUM_SIZE = 18
BIGGER_SIZE = 23

plt.rc('font', size=MEDIUM_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE)     # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)   # fontsize of the figure title

This way you can change any font of your plot, including in the squarify treemap plot.

Litho answered 9/8, 2017 at 20:43 Comment(0)
G
3
#Fonts demo (kwargs)
#Set font properties using kwargs.

#See Fonts demo (object-oriented style) to achieve the same effect using setters.


squarify.plot(sizes=volume, label=labels,text_kwargs={'fontsize':10, 'fontname':"Times New Roman Bold",'weight':'bold'},color=color_list, alpha=0.9)


#Fonts demo(kwargs) 
# https://matplotlib.org/3.1.0/gallery/text_labels_and_annotations/fonts_demo_kw.html
Gulf answered 19/11, 2019 at 14:57 Comment(1)
Is there a way to adapt the label size with the square size? For example, if the label is too big to fit inside the single square, reduce the label size accordingly or go to a new lineArleen

© 2022 - 2024 — McMap. All rights reserved.