我有工作树图,我需要获取此树图的每个形状的坐标,例如之后将它们放入 GeoJSON。有什么功能可以帮助我,或者我将从这个树形图的 svg 版本中获取所有坐标?
问问题
195 次
1 回答
2
与ax = squarify.plot(...)
,ax.patches
包含一个Rectangle 补丁列表。这些矩形具有诸如get_x()
. 坐标位于轴坐标系中,在 x 和 y 方向上似乎从 0 到 100。
当您在同一个图中绘制更多元素时,ax
可能还包含其他元素,因此您可能需要过滤它们。
import matplotlib.pyplot as plt
import squarify # pip install squarify (algorithm for treemap)
ax = squarify.plot(sizes=[13, 22, 35, 5], label=["group A", "group B", "group C", "group D"], color=['b','r','y','g'])
for rect in ax.patches:
x, y, w, h = rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height()
c = rect.get_facecolor()
print(f'Rectangle x={rect.get_x()} y={rect.get_y()} w={rect.get_width()} h={rect.get_height()} ')
plt.axis('off')
plt.show()
for rect, text in zip(ax.patches, ax.texts):
x, y, w, h = rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height()
c = rect.get_facecolor()
t = text.get_text()
于 2020-03-15T19:23:00.030 回答