带有马赛克图的 Seaborn FacetGrid

Seaborn FacetGrid with mosaic plot

尝试使用 Seaborn 的 FacetGrid 和 statsmodels 的 mosaic 绘制马赛克图网格,但未能成功。

示例数据集:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.graphics.mosaicplot import mosaic

index = [i for j in [[k] * 6 for k in range(4)] for i in j]
gender = ['male', 'male', 'male', 'female', 'female', 'female'] * 4
pet = np.random.choice(['cat', 'dog'], 24).tolist()
data = pd.DataFrame({'index': index, 'gender': gender, 'pet': pet})
data.head(10)
    index   gender  pet
0   0   male    dog
1   0   male    dog
2   0   male    cat
3   0   female  dog
4   0   female  dog
5   0   female  cat
6   1   male    cat
7   1   male    dog
8   1   male    dog
9   1   female  dog

我想制作一个由 4 个马赛克图组成的 2x2 网格,每个用于列 index 的子集。

现在,假设第一组 (index == 0) 的单个马赛克图:

data0 = data[data['index'] == 0]

props = {}
for x in ['female', 'male']:
    for y, col in {'dog': 'red', 'cat': 'blue'}.items():
        props[(x, y)] ={'color': col}

mosaic(data0, ['gender', 'pet'],
       labelizer=lambda k: '',
       properties=props)
plt.show()

但是试图把这个马赛克放在自定义函数中 sns.FacetGrid.map() 可以使用,我失败了(这是一个版本,我试了几个):

def my_mosaic(sliced_data, **kwargs):
    mosaic(sliced_data, ['gender', 'pet'],
       labelizer=lambda k: '',
       properties=props)

g = sns.FacetGrid(data, col='index', col_wrap=2)
g = g.map(my_mosaic)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-323-a81a61aaeaff> in <module>()
      5 
      6 g = sns.FacetGrid(data, col='index', col_wrap=2)
----> 7 g = g.map(my_mosaic)

~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\axisgrid.py in map(self, func, *args, **kwargs)
    741 
    742             # Draw the plot
--> 743             self._facet_plot(func, ax, plot_args, kwargs)
    744 
    745         # Finalize the annotations and layout

~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    825 
    826         # Draw the plot
--> 827         func(*plot_args, **plot_kwargs)
    828 
    829         # Sort out the supporting information

TypeError: my_mosaic() missing 1 required positional argument: 'sliced_data'

我阅读了 documentation and examples,但我无法弄清楚如何从 Seaborn 或 matplotlib.pyplot 中未内置的任何绘图函数创建可调用函数(例如plt.scattersns.regplot).

我发现当您最终要处理...数据帧时,使用 map_dataframe() 更容易。

def my_mosaic(*args,**kwargs):
    mosaic(kwargs['data'], list(args),
           labelizer=lambda k: '',
           properties=props,
           ax=plt.gca())

g = sns.FacetGrid(data, col='index', col_wrap=2)
g = g.map_dataframe(my_mosaic, 'gender', 'pet')