question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapped arguments not being passed properly to plots inside FacetGrid?

See original GitHub issue

My 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
tyarkonicommented, Jul 29, 2018

You may wish also to look at FacetGrid.map_dataframe

Ah, nice! This is what I was missing–I assumed that the internal implementation was basically this (i.e., the data passed by map would just be a subset of the original DataFrame.

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 calling map when the data passed to the FacetGrid is a pandas DataFrame 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, see map_dataframe”.

Thanks!

8reactions
jdkentcommented, Oct 3, 2018

Just wanted to chime in and say I had this issue too and didn’t know about .map_dataframe until seeing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plot data using facet-grid in seaborn [duplicate] - Stack Overflow
It is not working without passing the data again in map. It says "Could not interpret input 'region' ". And when i am...
Read more >
Use Seaborn FacetGrid to Quickly Create Figures With Subplots
To split the plot up into multiple subplots (facets) we add a col argument to the FacetGrid() and pass in the name of...
Read more >
Building structured multi-plot grids - Seaborn - PyData |
It must accept the data that it plots in positional arguments. Internally, FacetGrid will pass a Series of data for each of the...
Read more >
Introduction to structured multi-plot grids | H2kinfosys Blog
The FacetGrid class is useful when you want to visualize the distribution of a variable or the relationship between multiple variables ...
Read more >
Data visualization with ggplot2 - Data Carpentry
define an aesthetic mapping (using the aesthetic ( aes ) function), by selecting the variables to be plotted and specifying how to present...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found