Mapped arguments not being passed properly to plots inside FacetGrid?
See original GitHub issueMy understanding is that, once a FacetGrid
is constructed, .map
is suppose to simply pass args
and kwargs
on to the plotting function. But this seems to behave strangely. The following works fine:
iris = sns.load_dataset('iris')
iris_melted = iris.melt('species')
g = sns.FacetGrid(iris_melted, col='variable', sharey=False
g.map(sns.boxplot, 'species', 'value')
But the following raises an exception (ValueError: Could not interpret input 'species'
), even though x
and y
are the first two arguments to boxplot
:
g = sns.FacetGrid(iris_melted, col='variable', sharey=False
g.map(sns.boxplot, x='species', y='value')
The docstring for map
seems to suggest that the positional args aren’t actually passed onto the plotting function, but instead identify columns in the DataFrame
to use in the plot. But clearly something is being passed to the x
and y
args, because doing the following also fails (with TypeError: boxplot() got multiple values for argument 'x'
):
g = sns.FacetGrid(iris_melted, col='variable', sharey=False
g.map(sns.boxplot, 'species', 'value', x='species', y='value')
I’m not sure if this is a bug, or if the docs are unclear about the expected behavior, but I would expect the second example above to work. Passing other kwargs
(e.g., notch
) seems to work fine; it appears to be just x
and y
that can’t be explicitly named.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Ah, nice! This is what I was missing–I assumed that the internal implementation was basically this (i.e., the
data
passed bymap
would just be a subset of the originalDataFrame
.FWIW, I do think this could probably be made clearer in the
.map
docstring. Maybe you could add a note to say something like “Note that callingmap
when the data passed to theFacetGrid
is a pandasDataFrame
can lead to unexpected behavior in the event that the plotting function’s positional arguments optionally expect something other than numpy arrays. For a method suitable for plotting with functions that accept a long-form DataFrame, seemap_dataframe
”.Thanks!
Just wanted to chime in and say I had this issue too and didn’t know about
.map_dataframe
until seeing this.