X 轴未与条形图中的条形正确对齐(seaborn) [英] X-axis not properly aligned with bars in barplot (seaborn)

查看:45
本文介绍了X 轴未与条形图中的条形正确对齐(seaborn)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图表最终看起来像这样:

My graph is ending up looking like this:

我采用了原始的 Titanic 数据集并切片了一些列并通过以下代码创建了一个新的数据框.

I took the original titanic dataset and sliced some columns and created a new dataframe via the following code.

Cabin_group = titanic[['Fare', 'Cabin', 'Survived']] #selecting certain columns from dataframe
Cabin_group.Cabin = Cabin_group.Cabin.str[0] #cleaning the Cabin column
Cabin_group = Cabin_group.groupby('Cabin', as_index =False).Survived.mean()
Cabin_group.drop([6,7], inplace = True) #drop Cabin G and T as instances are too low
Cabin_group['Status']= ('Poor', 'Rich', 'Rich', 'Medium', 'Medium', 'Poor') #giving each Cabin a status value.

所以我的新数据框Cabin_group"最终看起来像这样:

So my new dataframe `Cabin_group' ends up looking like this:

  Cabin  Survived  Status
0     A  0.454545    Poor
1     B  0.676923    Rich
2     C  0.574468    Rich
3     D  0.652174  Medium
4     E  0.682927  Medium
5     F  0.523810    Poor

这是我尝试绘制数据框的方法

Here is how I tried to plot the dataframe

fig = plt.subplots(1,1, figsize = (10,4))
sns.barplot(x ='Cabin', y='Survived', hue ='Status', data = Cabin_group )
plt.show()

所以这张图有一些问题;首先,我们将条形 A、D、E 和 F 从它们各自的 x 轴标签移开.其次,条形本身似乎比我通常的条形图更薄/更薄.

So a couple of things are off with this graph; First we have the bars A, D, E and F shifted away from their respective x-axis labels. Secondly, the bars itself seem to appear thinner/skinnier than my usual barplots.

不确定如何将条形移动到适当的位置,以及如何控制条形的宽度.

Not sure how to shift the bars to their proper place, as well as how to control the width of the bars.

谢谢.

推荐答案

条形未对齐,因为它期望每个 x 有 3 个条形(Status 的每个不同值都有 1 个条形)代码>) 并且只提供了一个.我认为解决方案之一是将颜色映射到 Status.据我所知,不可能轻易做到这一点.但是,这是一个如何做到这一点的示例.我不确定这一点,因为简单地将颜色映射到类别似乎很复杂(并且不显示图例).

The bar are not aligned since it expects 3 bars for each x (1 for each distinct value of Status) and only one is provided. I think one of the solution is to map a color to the Status. As far as i know it is not possible to do thaht easily. However, here is an example of how to do that. I'm not sure about that since it seems complicated to simply map a color to a category (and the legend is not displayed).

# Creating a color mapping
Cabin_group['Color'] = Series(pd.factorize(Cabin_group['Status'])[0]).map(
                              lambda x: sns.color_palette()[x])

g = sns.barplot(x ='Cabin', y='Survived', data=Cabin_group, palette=Cabin_group['Color'])

当我看到它在 R 中是多么简单时......但不幸的是,Python 中的 ggplot 实现不允许使用 stat = 'identity' 绘制 geom_bar.

When I see how simple it is in R ... But infortunately the ggplot implementation in Python does not allow to plot a geom_bar with stat = 'identity'.

library(tidyverse)

Cabin_group %>% ggplot() +
  geom_bar(aes(x = Cabin, y= Survived, fill = Status), 
           stat = 'identity')

这篇关于X 轴未与条形图中的条形正确对齐(seaborn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆