When you use FacetGrid.map(func, "col1", "col2", ...)
, the function func
is passed the values of the columns "col1"
and "col2"
(an more if needed) as parameters 1 and 2 (args[0], args[1], ...). In addition, the function always receives a keyword argument named color=
.
When you use FacetGrid.map_dataframe(func, "col1", "col2", ...)
, the function func
is passed the names "col1"
and "col2"
(an more if needed) as parameters 1 and 2 (args[0], args[1], ...), and the filtered dataframe as keyword argument data=
. In addition, the function always receives a keyword argument named color=
.
Maybe this demonstration would help:
N=4
df = pd.DataFrame({'col1': np.random.random(N), 'col2':np.random.random(N), 'cat':np.random.choice([True,False], size=N)})
| | col1 | col2 | cat |
|---:|---------:|----------:|:------|
| 0 | 0.651592 | 0.631109 | True |
| 1 | 0.981403 | 0.550882 | False |
| 2 | 0.467846 | 0.997084 | False |
| 3 | 0.119726 | 0.0452547 | False |
code:
def test(*args, **kwargs):
print(">>> content of ARGS:")
print(args)
print(">>> content of KWARGS:")
print(kwargs)
g = sns.FacetGrid(df, col='cat')
g.map(test, 'col1', 'col2')
output:
>>> content of ARGS:
(1 0.981403
2 0.467846
3 0.119726
Name: col1, dtype: float64, 1 0.550882
2 0.997084
3 0.045255
Name: col2, dtype: float64)
>>> content of KWARGS:
{'color': (0.12156862745098039, 0.4666666666666667, 0.7058823529411765)}
>>> content of ARGS:
(0 0.651592
Name: col1, dtype: float64, 0 0.631109
Name: col2, dtype: float64)
>>> content of KWARGS:
{'color': (0.12156862745098039, 0.4666666666666667, 0.7058823529411765)}
code:
g.map_dataframe(test, 'col1', 'col2')
output:
>>> content of ARGS:
('col1', 'col2')
>>> content of KWARGS:
{'color': (0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
'data': col1 col2 cat
1 0.981403 0.550882 False
2 0.467846 0.997084 False
3 0.119726 0.045255 False}
>>> content of ARGS:
('col1', 'col2')
>>> content of KWARGS:
{'color': (0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
'data': col1 col2 cat
0 0.651592 0.631109 True}