python - skipping empty facets in seaborn facetgrid for annotations -
i'm working on putting seaborn facetgrid data leaves grids empty. furthermore, i'm annotating each facet statistics, i'm not sure how "skip" empty facets annotations fall on proper facet.
g.axes.flat
of length 9 (9 facets have data); when place annotation on each element in g.axes.flat
, isn't being placed expect.
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 stats grouped = mapping.groupby([options.facetrow, options.facetcol]) ax, (name, df) in zip(g.axes.flat, grouped): df2 = df.groupby(options.group) # group each thing has own color , run stats on i, (group, data) in enumerate(df2): x = data[options.axis] # calculate stats , 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)
edit
i've created annotation function , i'm passing map()
[as recommended]; i'm uncertain how pass label names function , how annotations (there 2 each facet) shift in y direction. more suggestions?
g = g.map(stats, options.axis) def stats(x, **kwargs): ax = sns.distplot(x, **kwargs) # calculate stats , 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 pass through function = 1 # temporary, needs increment shift annotations 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)
the final solution was:
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 allows create distplot , add offset annotations each facet not empty def stats(x, label, **kwargs): # reference active axes ax = plt.gca() # calculate stats , 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)
Comments
Post a Comment