我有一个树状地图,有足够小的部分,标签重叠。是否有任何方法将size=4下的部分(或周围的部分)的标签移动到地块之外,箭头指向它,或者到一个只包含小部分标签的小图例中?
树状图生成,代码如下。
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

发布于 2022-04-09 07:45:34
也许您可以使用matplotlib-extra包,它包括一个treemap函数来绘制分层树状图。
就你的情况而言,这很简单:
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')获得的数字如下:

https://stackoverflow.com/questions/70276375
复制相似问题