繁体   English   中英

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

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

我有工作树图,我需要获取此树图的每个形状的坐标,例如,之后将它们放入 GeoJSON 中。 是否有任何功能可以帮助我解决这个问题,或者我将从该树状图的 svg 版本中获取所有坐标?

使用ax = squarify.plot(...)ax.patches包含一个Rectangle patch列表。 这些矩形具有诸如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()

示例图 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