本文介绍了FacetGrid 图例为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 seaborn FacetGrid 来显示我拥有的某些数据中集群之间的转换概率.数据具有一堆主题和4个群集(因此,每个主题都有16个数据点,每对旧的和新的群集都一个).因为这些都是概率,所以所有具有相同旧聚类(针对每个主题)的所有值总和为1.我想使用FacetGrid点图(或堆叠式条形图)显示此数据,但对于seaborn来说似乎不可能),例如每个旧集群是一个单独的列,新集群是一个单独的色调,每个方面都显示主题与概率.

I'm trying to create a seaborn FacetGrid to show the transition probabilities between clusters in some data I have. The data has a bunch of subjects and 4 clusters (so that each subject has 16 data points, one for each pair of old and new clusters). Because these are probabilities, all values that have the same old cluster (for each subject) sum to 1. I would like to show this data using a FacetGrid pointplot (or stacked barplot, but that doesn't seem possible with seaborn), such that each old cluster is a separate column, new cluster is a separate hue, and each facet shows subject vs. probability.

这里的代码生成与我的玩具具有相同功能的玩具DataFrame,以及我想制作的facetgrid的代码.但是,当我完成所有这些操作时,图例为空.

Here's code to generate a toy DataFrame with the same features as mine, as well as code for the facetgrid I'd like to make. However, when I do all this, the legend is empty.

df,index = [],0
for subj in [1,2]:
    for i,j in itertools.product(range(4),range(4)):
        df.append(pd.DataFrame(index=[index],data=dict(subject=subj,old=i,new=j,prob=[.23,.24,.26,.27][j])))
        index+=1
df = pd.concat(df)
g = sns.FacetGrid(df,col='old',hue='new',margin_titles=False,legend_out=True)
g.map(sns.pointplot,'subject','prob',join=False)
g.add_legend()

查看 g._legend_data 表明它是空的,并且每个轴 get_legend_handles_labels() 函数都不返回任何内容.发生了什么事?

Looking at g._legend_data shows that it's empty and each of the axes get_legend_handles_labels() functions returns nothing. What's going on?

因为我没有10个声誉,所以我无法发布自己的地块图像;传说的名字出现在最右边,但里面什么都没有......

I can't post an image of my plot because I don't have 10 reputation; the name of the legend appears over on the far right, but nothing is in it...

EDIT:另外,我想稍微避开这些值,但是 g.map(sns.pointplot,'subject','prob',join=False,dodge=.1) 看起来和上面完全一样,对于各种不同的闪避值...

EDIT: Additionally, I'd like to dodge the values a little, but g.map(sns.pointplot,'subject','prob',join=False,dodge=.1) looks the exact same as above, for a variety of different dodge values...

EDIT 2: 所以看起来使用 sns.factorplot 而不是 FacetGrid,图例和闪避都有效.我唯一不能使用sns.factorplot的方法是旋转xtick标签,但是无论如何在Facet Grid中不能与 col_wrap 一起使用...为什么FacetGrid出现此问题?

EDIT 2: So it appears that using sns.factorplot, instead of FacetGrid, both the legend and the dodge work. The only thing I can't do using sns.factorplot is rotate the xtick labels, but that's not working with col_wrap in Facet Grid anyway... Why is FacetGrid having this problem?

推荐答案

如您所见,factorplot 提供了一个更简单的界面来使用 pointplotFacetGrid在一起.图例和闪避与原始操作方式不符的原因是,在 FacetGrid 中设置 hue ,并在 pointplot 中进行设置是不同的.当你在没有hue变量的情况下调用pointplot时,它不知道如何躲避,它认为它不需要添加任何图例数据.

As you note, factorplot provides an easier interface to using pointplot and FacetGrid together. The reason the legend and dodging doesn't work the original way you're doing it is that setting hue in FacetGrid and setting it in pointplot are different. When you call pointplot without a hue variable, it doesn't know how to dodge, and it doesn't think it needs to add any legend data.

第一种方法是将移动到使用 hue 变量的位置:

Option one would be to move where you use the hue variable:

g = sns.FacetGrid(df,col='old', col_wrap=2)
g.map(sns.pointplot, 'subject', 'prob', 'new', dodge=.1, join=False)
g.add_legend()

但我想不出有什么理由以这种方式使用 FacetGrid 而不是仅仅使用 factorplot:

But I can't think of any reason to use FacetGrid this way instead of just using factorplot:

g = sns.factorplot(x="subject", y="prob", hue="new", data=df,
               col="old", col_wrap=2, size=3,
               dodge=.1, join=False)

所以唯一剩下的问题是旋转 x 轴刻度标签. factorplot 返回一个 FacetGrid 实例,因此原则上应该容易使用其他的 FacetGrid 方法来调整该图.我怀疑您遇到的问题是这个错误,它将在下一个版本中修复释放.同时,您可以像这样解决它:

So the only remaining issue is rotating the x axis tick labels. factorplot returns a FacetGrid instance, so in principle it should be easy to use further FacetGrid methods to tweak the plot. I suspect the problem you're getting is this bug, which will be fixed in the next release. In the meantime, you can work around it like so:

g = sns.factorplot(x="subject", y="prob", hue="new", data=df,
                   col="old", col_wrap=2, size=3,
                   dodge=.1, join=False)
for ax in g.axes:
    plt.setp(ax.get_xticklabels(), rotation=45)

这篇关于FacetGrid 图例为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:14