Is there a way to check whether a keyword argument is valid for a method?

Is there a way to validate whether or not size is a valid keyword ahead of time, other than wrapping a try/except?

import matplotlib.pyplot as plt
plt.scatter([0, 3, 4], [6, 7, 8], size=25)

i.e. is there an internal dictionary I can verify against?
if 'size' in charts_valid_kwds.keys()

Is there something missing from the docs that doesn’t cover the keyword arguments?
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.scatter.html

I think the docs are fine, but is there a way to programmatically check? For example I have am trying to use the same set of keywords inherited from the line plot and I want to keep the ones that apply (color) and cull the ones that don’t apply (linewidth).

The trick is to use what the docs use to generate those lists. Which is

from matplotlib.artist import ArtistInspector
from matplotlib.collections import PathCollection
ArtistInspector(PathCollection).get_setters()

I’ve also written a function that will separate matplotlib kwargs from other kwargs for use in mpl-interactions:

1 Like

That isn’t perfect as it doesn’t grab other kwargs that aren’t from PathCollection such as plotnonfinite

So you can get the rest of them with

import inspect
sig = inspect.signature(plt.scatter)
list(sig.parameters.keys())

So in total for scatter you’d do

import inspect

from matplotlib.artist import ArtistInspector
from matplotlib.collections import PathCollection
scatter_kwargs = ArtistInspector(PathCollection).get_setters()

sig = inspect.signature(plt.scatter)
scatter_kwargs.extend(list(sig.parameters.keys()))

1 Like

Wow thanks for the insight! Is there an equivalent for plt.pie?

When I do:

import inspect

from matplotlib.artist import ArtistInspector
from matplotlib.collections import PathCollection
pie_kwargs = ArtistInspector(PathCollection).get_setters()

sig = inspect.signature(ax.pie)
pie_kwargs.extend(list(sig.parameters.keys()))

It lists cmap, but that’s invalid because it’s probably not a path collection.

import inspect

from matplotlib.artist import ArtistInspector
from matplotlib.collections import PathCollection
from matplotlib.patches import Wedge
pie_kwargs = ArtistInspector(Wedge).get_setters()

sig = inspect.signature(ax.pie)
pie_kwargs.extend(list(sig.parameters.keys()))

pie_kwargs

Okay this seems to work! Much appreciated!

I was just nerd sniping myself trying to figure out a general way to do this!

At the very least for pie you can look at the docs for what artist is returned and do that manually. But it’d be nice to get the information automatically. However, that’s a bit tricky because matplotlib doesn’t have any type annotations, so I think it’d need to be directly extracted from the docstrings.

1 Like

Yes I am trying to use plt.getp() to see if I can extract whether it’s a PathCollection or Wedge or something else

I think I got it:

import inspect
from collections import Iterable
from matplotlib.artist import ArtistInspector


# sometimes matplotlib returns a tuple/list of objects; just get the first
def get_root(obj):
    if isinstance(obj, Iterable):
        return traverse(obj[0])
    return obj


valid_kwds = {
    chart: ArtistInspector(
        get_root(getattr(plt, chart)([0], [0]))
    ).get_setters() + list(sig.parameters.keys())
    for chart in ["plot", "bar", "pie", "scatter"]
}

It’s not foolproof, unfortunately it keeps zorder and normalize and label for pie which is invalid.

Actually, maybe all the setters are invalid, so just don’t append if pie.