seaborn文档尚不清楚这些区别,我无法弄清楚它们之间的区别。似乎它们具有非常相似的功能,甚至不完全相同。
seaborn.FacetGrid.map_dataframe
区别到底是什么?何时使用一个与另一个?关于map_dataframe
的原始文档说:“与map方法不同,此处使用的函数必须“理解” Pandas对象。”那是map_dataframe与map文档中的唯一区别。什么样的对象被发送到map中的函数,如果不是一个数据框,它为什么重要呢?我也不太了解目标功能必须接受的color
论点。该color
参数中包含什么信息?
答案 0 :(得分:1)
使用FacetGrid.map(func, "col1", "col2", ...)
时,会将函数func
和"col1"
(如果需要的话,更多)列的值作为参数1和2(参数[0],args [1],...)。此外,该函数始终会收到一个名为"col2"
的关键字参数。
使用color=
时,会将函数FacetGrid.map_dataframe(func, "col1", "col2", ...)
和func
(如果需要,可以更多)传递给函数"col1"
作为参数1和2(args [0] ,args [1],...)和过滤后的数据帧作为关键字参数"col2"
。此外,该函数始终会收到一个名为data=
的关键字参数。
也许这个演示会有所帮助:
color=
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 |
:代码:
FacetGrid.map()
输出:
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')
>>> 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)}
代码:
map_dataframe
输出:
g.map_dataframe(test, 'col1', 'col2')