如何获取 matplotlib 轴上存在的所有对象的列表

How to get a list of all objects present on the matplotlib axes

如何获取 matplotlib 轴上存在的所有对象(绘制数据、垂直线、水平线、点等)的列表?如果没有简单的方法将它们全部放在一个列表中,那么我怎样才能得到这种只包含垂直线的列表?

您可以使用 ax.get_children(). 获取所有艺术家(绘图对象) 如果只关心行数,可以使用ax.get_lines()。这个 有更多的细节。

对于 select 垂直线,您只需检查它们的 x 坐标即可。

vertical_lines = [l for l in ax.get_lines() if l.get_xdata()[0] == l.get_xdata()[1]]