微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Squarify-在树形图中自动调整标签大小

如何解决Squarify-在树形图中自动调整标签大小

我正在使用Squarify在Python中实现一个简单的树形图。

我正在用演唱者图表中的流媒体百分比来绘制歌手姓名,正方形越大/越暗,则值越高。

我的代码如下:

dataGoals = sort_by_streams[sort_by_streams["Streams"]>1]

#Utilise matplotlib to scale our stream number between the min and max,then assign this scale to our values.
norm = matplotlib.colors.normalize(vmin=min(dataGoals.Streams),vmax=max(dataGoals.Streams))
colors = [matplotlib.cm.Blues(norm(value)) for value in dataGoals.Streams]

#Create our plot and resize it.
fig1 = plt.figure()
ax = fig1.add_subplot()
fig1.set_size_inches(16,4.5)

#Use squarify to plot our data,label it and add colours. We add an alpha layer to ensure black labels show through
labels = ["%s\n%.2f" % (label) for label in zip(dataGoals.Artist,dataGoals.Streams)]

squarify.plot(label=labels,sizes=dataGoals.Streams,color = colors,alpha=.7,bar_kwargs=dict(linewidth=0.5,edgecolor="#222222"),text_kwargs={'fontsize':15})
plt.title("Streams Percentage",fontsize=23,fontweight="bold")

#Remove our axes and display the plot
plt.axis('off')
plt.show()

这是结果:

Treemap

您可能会注意到,较小正方形的标签重叠并且超出了边界。 有没有一种方法可以自动调整标签大小以适合正方形?

编辑:我尝试使用以下代码来实现matplotlib的自动包装功能squarify.plot(label=labels,text_kwargs={'fontsize':20,'wrap':True})但这不能解决我的问题,我的文本标签仍然超出范围。 / p>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。