问题描述
我需要覆盖两个sns.catplots 一个是kind ='Box',另一个是kind ='warm',如下所示:
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",palette=["skyblue",kind="swarm",ax=gBox.axes)
我尝试从一个轴上取下轴并送入另一个猫形图,但是出现以下错误。我该如何解决?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-74-7a05cc88a396> in <module>
17
18 gBox =sns.catplot( x="Emotion",---> 19 data=indata,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,**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,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,dodge,size,edgecolor,linewidth,**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,kws)
1444 def plot(self,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,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
解决方法
理论上,使用.map和.map_dataframe方法应该可以,但是
- 当您不想在要覆盖的图上使用相同的维数时(例如,对第一和第二个图使用不同的色相参数以不同的方式显示但拆分的数据),失败
编辑它的工作原理,但是仅当使用
col/row
参数首先独立启动sns.FacetGrid时,并且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参数)!
,您无法将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,y="Threshold")