【问题标题】:FacetPlot/CatPlot (seaborn) - how to change the bar positions?FacetPlot/CatPlot (seaborn) - 如何改变柱的位置?
【发布时间】:2020-08-10 19:56:05
【问题描述】:

我无法确定秤上条形的位置。我理解它是一些色相量为 0,所以这会偏离条形的位置。在图像中,右上角的图显示了“劳动力”的绿色和棕色条,它们之间有一个间隙,大概是因为该颜色为 0。有没有办法将这些条放在一起,并与它们在 y- 上的对应关系一致轴?

grid = sns.catplot(x='Type', y='count', 
               row='Age', col='Gender', 
               hue='Nationality',
               data=dfNorthumbria2, kind='bar', ci=None,legend=True) 
grid.set(ylim=(0,5), yticks=[0,1,2,3,4,5])
grid.set(xlabel="Type of Exploitation",ylabel="Total Referrals")

for ax in grid.axes.flatten():
ax.tick_params(labelbottom=True, rotation=90)
ax.tick_params(labelleft=True)
grid.fig.tight_layout() 
leg = grid._legend
leg.set_bbox_to_anchor([1.1,0.5])

【问题讨论】:

    标签: python matplotlib seaborn facet-grid


    【解决方案1】:

    您可以通过sns.catplothue_order 参数传递给sns.barplot(),例如

    grid = sns.catplot(..., hue_order=['British', 'Romanian', 'Vietnamese', 
                                       'Albanian', 'Pakistani', 'Slovak'])
    

    这应该会缩小绿色和棕色条之间的间隙,它们将位于刻度线的中心,因为它们现在位于列表的中间。但是,其他条形组仍不会以它们的刻度线为中心。

    这可能是此绘图功能如何工作的不可避免的结果,它不是为这种稀疏数据设计的。因此,如果您希望所有不同的条组以各自的刻度线为中心,您可能必须使用更灵活的 matplotlib 绘图功能并手动创建颜色子集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 2019-01-20
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2019-08-30
      • 2019-11-10
      • 2023-01-17
      相关资源
      最近更新 更多