简体   繁体   English

如何在matplotlib树图中获取形状坐标?

[英]How to get coordinates of shape in matplotlib treemap?

I have working treemap, and I need to get coordinates of each shape of this treemap to, for example put them in GeoJSON after that.我有工作树图,我需要获取此树图的每个形状的坐标,例如,之后将它们放入 GeoJSON 中。 Is there any function to help me with that, or I'll be getting all the coordinates from svg version of this treemap?是否有任何功能可以帮助我解决这个问题,或者我将从该树状图的 svg 版本中获取所有坐标?

With ax = squarify.plot(...) , ax.patches contains a list of Rectangle patches .使用ax = squarify.plot(...)ax.patches包含一个Rectangle patch列表。 Those Rectangles have functions such as get_x() .这些矩形具有诸如get_x()函数。 The coordinates are in the axes coordinate system, which seem to go from 0 to 100 in both x and y direction.坐标在轴坐标系中,在 x 和 y 方向上似乎从 0 到 100。

When you're drawing more elements in the same plot, ax might also contain other elements, so you might need to filter them.当您在同一图中绘制更多元素时, 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()

示例图 PS: To obtain the corresponding texts, again supposing the plot only contains the treemap: PS:要获取相应的文本,再次假设情节仅包含树状图:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM