Quantcast
Channel: Uncategorized - Matplotlib
Viewing all articles
Browse latest Browse all 56

Is there a way to "recursively" get all artists from a given Figure (in a convenient format)?

$
0
0

Hi!

I’m looking to find a way to easily get, for instance, all `Line2D` instance from a Figure, without having to check the kind of artist I get from fig.get_children().

I’m thinking of something like this:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.scatter([1, 2, 3], [1, 2, 3], s=300, zorder=10)
ax.spines[["top", "right"]].set_visible(False)
ax.grid()

def get_childrens(artist):
    children = []
    try:
        kids = artist.get_children()
    except AttributeError:
        return children

    for child in kids:
        children.append(child)
        children.extend(get_childrens(child))
    return children

all_artists = get_childrens(fig)

Is there an already built-in function to do this? Something like fig.get_children().flatten().

On a related note, the “ideal” function for me here would be something that returns all artists but in a dict-like format:

{
    "Figure": {
        "Axes": {
            "XAxis": {
                "Ticks": ["Line2D", "Text"],
                "Label": "Text"
            },
            "YAxis": {
                "Ticks": ["Line2D", "Text"],
                "Label": "Text"
            },
            "Spines": ["Spine(top)", "Spine(bottom)", "Spine(left)", "Spine(right)"],
            "Title": "Text",
            "Collections": ["PathCollection (e.g. scatter)"],
            "Lines": ["Line2D (e.g. plot, gridlines)"],
            "Patches": ["Patch (background, rectangles, etc.)"],
            "Images": []
        }
    }
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 56

Trending Articles