如何在Python中使用squarify编辑树映射时编辑标签字体大小?

2024-09-24 06:23:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python中的squarify包,codeLink,来绘制treemapwikiTreemapArticle。基于这个example,我可以生成一个treemap,但是我无法看到如何修改正方形中标签的字体大小。剧本中最重要的一句话是:

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

从这里我不能添加'fontsize'属性。如何更改标签的大小?在


Tags: plotexample绘制标签axcolor字体大小colors
3条回答

squarifymatplotlibpyplot上工作,所以您只需要将de font size改为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

这样您就可以更改绘图的任何字体,包括squarifytreemap绘图。在

更新: 现在可以通过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

相关问题 更多 >