Seaborn FacetGrid与马赛克图

时间:2019-07-29 03:42:03

标签: python matplotlib seaborn facet-grid

尝试使用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

我想制作一个2x2的网格,其中包含4个马赛克图,每个网格用于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()

single mosaic

但是尝试将此镶嵌放在自定义函数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中未内置的任何绘图函数中创建可调用函数(例如{{ 1}}或plt.scatter)。

1 个答案:

答案 0 :(得分:1)

我发现在最终处理...数据帧时,使用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')

enter image description here