如何在 Python 中使用 squarify 构建 treemap 时编辑标签字体大小?

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

我在 python、codeLink, to draw a treemap, wikiTreemapArticle. Based on this example 中使用 squarify 包,我可以生成树状图,但我看不到方块中标签的字体大小如何被修改。脚本中的基本行是:

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

从这里我无法添加 'fontsize' 属性。我将如何更改标签的大小?

squarify 适用于 matplotlib pyplot,因此您只需要更改 pyplot.

的字体大小

我过去常常在我的绘图代码上做以下事情。

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

通过这种方式,您可以更改绘图的任何字体,包括 squarify treemap 绘图。

更新: 现在可以通过 text_kwargs 参数更改字体大小(squarify==0.3.0 或更高):

ax = squarify.plot(countryPop, color=colors, label=labels, ax=ax, bar_kwargs={'alpha':.7}, text_kwargs={'fontsize':10})
#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