Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 跳过seaborn FaceGrid中用于注释的空面_Python_Plot_Facet_Seaborn - Fatal编程技术网

Python 跳过seaborn FaceGrid中用于注释的空面

Python 跳过seaborn FaceGrid中用于注释的空面,python,plot,facet,seaborn,Python,Plot,Facet,Seaborn,我正在整理一个表格,其中有一些表格是空的。此外,我正在用一些统计数据对每个方面进行注释,但是我不确定如何“跳过”空的方面,以便注释落在正确的方面 g.axes.flat长度为9(9个面有数据);但是,当我在g.axes.flat中的每个元素上放置注释时,它并没有被放置在我期望的位置 g = sns.FacetGrid(mapping, col=options.facetCol, row=options.facetRow, col_order=sorted(cols), hue=options.g

我正在整理一个表格,其中有一些表格是空的。此外,我正在用一些统计数据对每个方面进行注释,但是我不确定如何“跳过”空的方面,以便注释落在正确的方面

g.axes.flat
长度为9(9个面有数据);但是,当我在
g.axes.flat
中的每个元素上放置注释时,它并没有被放置在我期望的位置

g = sns.FacetGrid(mapping, col=options.facetCol, row=options.facetRow, col_order=sorted(cols), hue=options.group, sharex=False)
g = g.map(sns.distplot, options.axis)

# label each facet with stats
grouped = mapping.groupby([options.facetRow, options.facetCol])
for ax, (name, df) in zip(g.axes.flat, grouped):
    df2 = df.groupby(options.group) # group by each thing that has its own color and run stats on it

    for i, (group, data) in enumerate(df2):
        x = data[options.axis]

        # calculate stats and create label
        n = len(x)
        mean = np.mean(x)
        std = np.std(x)
        label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %(group, n, mean, std)
        ax.annotate(label, xy=(0.05,0.9-(i*0.05)), xycoords='axes fraction', ha='left', size=8)

编辑 我已经创建了一个注释函数,并将其传递给
map()
[推荐];但是,我不确定如何将标签名传递给函数,以及如何使注释(每个方面有两个注释)在y方向上移动。还有什么建议吗

g = g.map(stats, options.axis)

def stats(x, **kwargs):
    ax = sns.distplot(x, **kwargs)

    # calculate stats and create label
    n = len(x)
    mean = np.mean(x)
    std = np.std(x)
    label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %('moo', n, mean, std) # temporary label, need to pass it through function
    i = 1 # temporary, needs to increment to shift annotations so they aren't on top of each other

    # create annotation
    ax.annotate(label, xy=(0.05,0.9-(i*0.05)), xycoords='axes fraction', ha='left', size=8)

最终的解决方案是:

g = sns.FacetGrid(mapping, col=options.facetCol, row=options.facetRow, col_order=sorted(cols), hue=options.group, sharex=False)
g.map(sns.distplot, options.axis)
g.map(stats, options.axis)

# custom function that allows us to create a distplot and add offset annotations to each facet that is not empty
def stats(x, label, **kwargs):

    # get a reference to the currently active axes
    ax = plt.gca()    

    # calculate stats and create label
    n = len(x)
    mean = np.mean(x)
    std = np.std(x)
    label = r"%s: n=%s, $\mu$=%.2f $\sigma$=%.2f" %(label, n, mean, std)

    # create annotation
    y = 0.9 - len(ax.texts) * 0.05
    ax.annotate(label, xy=(0.05,y), xycoords='axes fraction', ha='left', size=8)

这更像是一个大熊猫的问题,而不是一个海生的问题;您需要迭代所有组,而不仅仅是那些有数据的组。但是最好的办法是定义一个做注释的函数,然后将它传递给
g.map
。要移动注释,有点麻烦,但是如果ax.text不是.8,我会做类似
y=.7的事情。我不确定“将标签名称传递给函数”是什么意思,但是
map
会将色调级别的名称传递给
label
参数。我编辑了代码,将
distplot
调用从
stats
中取出,并将其直接传递给
map
,这使得注释代码更加通用。“ax”在这个上下文中来自哪里?我猜它一定是Axis的对象,但你是如何得到它的?谢谢我会让@mwaskom插话,因为我不确定;同时,原始答案如下: