2

我需要叠加两个 sns.catplots 一个是 kind='box' 另一个是 kind='swarm' 如下:

gbox= sns.catplot( x="Emotion",y="Threshold",hue="Group", col = 'Task',
               data=indata,palette ["skyblue","salmon"], kind="box", showmeans='True', meanline = 'True', height=6, aspect=0.8, boxprops={'facecolor':'None'}, edgecolor='gray')

sns.catplot( x="Emotion",y="Threshold",hue="Group", col = 'Task',
             data=indata, palette=["skyblue","salmon"], kind="swarm",ax=gbox.axes)

我尝试从一个中取出轴并输入另一个 catplot,但我收到如下错误。我怎样才能解决这个问题?

  ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-74-7a05cc88a396> in <module>
     17 
     18 gbox =sns.catplot( x="Emotion",y="Threshold",hue="Group", col = 'Task',
---> 19             data=indata,palette=["skyblue","salmon"],kind="swarm",ax=gbox.axes)
     20 
     21 #plt.show(gbox)

~/anaconda3/lib/python3.6/site-packages/seaborn/categorical.py in catplot(x, y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, order, hue_order, row_order, col_order, kind, height, aspect, orient, color, palette, legend, legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
   3753 
   3754     # Draw the plot onto the facets
-> 3755     g.map_dataframe(plot_func, x, y, hue, **plot_kws)
   3756 
   3757     # Special case axis labels for a count type plot

~/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py in map_dataframe(self, func, *args, **kwargs)
    818 
    819             # Draw the plot
--> 820             self._facet_plot(func, ax, args, kwargs)
    821 
    822         # Finalize the annotations and layout

~/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    836 
    837         # Draw the plot
--> 838         func(*plot_args, **plot_kwargs)
    839 
    840         # Sort out the supporting information

~/anaconda3/lib/python3.6/site-packages/seaborn/categorical.py in swarmplot(x, y, hue, data, order, hue_order, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
   2989                        linewidth=linewidth))
   2990 
-> 2991     plotter.plot(ax, kwargs)
   2992     return ax
   2993 

~/anaconda3/lib/python3.6/site-packages/seaborn/categorical.py in plot(self, ax, kws)
   1444     def plot(self, ax, kws):
   1445         """Make the full plot."""
-> 1446         self.draw_swarmplot(ax, kws)
   1447         self.add_legend_data(ax)
   1448         self.annotate_axes(ax)

~/anaconda3/lib/python3.6/site-packages/seaborn/categorical.py in draw_swarmplot(self, ax, kws)
   1374         # Set the categorical axes limits here for the swarm math
   1375         if self.orient == "v":
-> 1376             ax.set_xlim(-.5, len(self.plot_data) - .5)
   1377         else:
   1378             ax.set_ylim(-.5, len(self.plot_data) - .5)

AttributeError: 'numpy.ndarray' object has no attribute 'set_xlim'

谢谢您的帮助!SJ

4

2 回答 2

0

理论上,使用 .map 和 .map_dataframe 方法应该可以工作,但是

  • 当您不想在尝试覆盖的图上使用相同的维度时,它会失败(例如,对第一个图和第二个图使用不同的色调参数以显示以不同方式拆分但重叠的数据)

编辑它有点工作,但只有当 sns.FacetGrid 首次使用col/row参数独立启动,并且hue参数在单独的 .map_dataframe 函数中传递时?我没有探索每种组合,无论如何这有点笨拙且不一致。

  • 不可能控制两个图重叠的zorder(无论我调用g.map(sns.pointplot,x,y1)和g.map(sns.pointplot,x,y2)的顺序,顺序y1 和 y2 之间永远是一样的!!)

编辑这至少是点图的情况,请参阅问题#2339

理想情况下,catplot/lmplot/relplot/displot 函数应该允许网格参数(相当于其他 seaborn 函数的 ax 参数)!

于 2020-10-29T18:55:37.790 回答
-1

您不能将ax=参数传递给catplot. 您将需要FacetGrid“手动”创建。

像这样:(未经测试,因为您没有提供数据)

g = sns.FacetGrid(data=indata, hue="Group", col ='Task',
                  palette=["skyblue","salmon"])
g.map(sns.boxplot, x="Emotion", y="Threshold", showmeans='True', meanline = 'True',
                   height=6, aspect=0.8, boxprops={'facecolor':'None'}, edgecolor='gray')

g.map(sns.swarmplot, x="Emotion", y="Threshold")
于 2020-10-19T19:17:24.447 回答