bismo
bismo

Reputation: 1439

python seaborn barplot bars not centered?

I have never seen this before, but a seaborn barplot I am making just will not evenly space the bars...

Code

fig, ax = plt.subplots(figsize=(25,6))

sns.barplot(x=value.index, y="CustomerValue", data=value, 
            order=value.index, hue='Response')

plt.xticks(fontsize=10, rotation=90)

Data

    Response    OrderCount  OrderAvgSize    AvgDeliverCost  AvgOrderValue   CustomerValue
CustomerID                      
508 Walmart+    48  22  4.94    14.60   700.80
2007    Both Or None    51  21  4.91    13.46   686.46
698 Walmart+    47  21  4.91    14.02   658.94
1664    Walmart+    45  22  4.94    14.60   657.00
475 Walmart+    45  22  4.94    14.60   657.00
575 Both Or None    51  20  4.87    12.88   656.88
1675    Both Or None    50  20  4.87    12.88   644.00
678 Both Or None    48  20  4.87    12.88   618.24
361 Both Or None    50  19  4.84    12.30   615.00
1627    Walmart+    41  22  4.94    14.59   598.19
1902    Both Or None    53  17  4.76    11.14   590.42
295 Walmart+    48  18  4.80    12.28   589.44
269 Both Or None    50  18  4.80    11.72   586.00
1814    Walmart+    45  19  4.84    12.86   578.70
588 Walmart+    38  23  4.97    15.17   576.46
2204    Both Or None    49  18  4.80    11.72   574.28
65  Both Or None    54  16  4.71    10.56   570.24
868 Walmart+    36  24  4.31    15.75   567.00
1436    Walmart+    42  20  4.87    13.43   564.06
1957    Walmart+    42  20  4.87    13.43   564.06
1754    Walmart+    43  19  4.84    12.85   552.55
2036    Walmart+    43  19  4.84    12.85   552.55
679 Walmart+    39  21  4.91    14.01   546.39
108 Walmart+    44  18  4.80    12.28   540.32
605 Both Or None    46  18  4.80    11.72   539.12
1477    Walmart+    40  20  4.87    13.43   537.20
1737    Walmart+    38  21  4.91    14.01   532.38
186 Walmart+    41  19  4.84    12.85   526.85
702 Walmart+    37  21  4.91    14.01   518.37
1572    Walmart+    37  21  4.91    14.01   518.37
1797    Walmart+    38  20  4.87    13.43   510.34
572 Walmart+    38  20  4.87    13.43   510.34
642 Walmart+    32  24  4.31    15.74   503.68
1787    Walmart+    39  19  4.84    12.85   501.15
387 Walmart+    39  19  4.31    12.85   501.15
991 Walmart+    39  19  4.84    12.85   501.15
1394    Walmart+    39  19  4.84    12.85   501.15
1966    Walmart+    33  23  4.97    15.16   500.28
1053    Walmart+    35  21  4.91    14.00   490.00
216 Walmart+    38  19  4.84    12.85   488.30
918 Walmart+    38  19  4.84    12.85   488.30
1518    Walmart+    49  14  4.62    9.96    488.04
565 Both Or None    46  16  4.71    10.56   485.76
845 Walmart+    36  20  4.87    13.43   483.48
1757    Walmart+    33  22  4.94    14.58   481.14
1983    Walmart+    33  22  4.94    14.58   481.14
2152    Walmart+    41  17  4.76    11.69   479.29
198 Both Or None    48  15  4.67    9.98    479.04
1990    Both Or None    48  15  4.67    9.98    479.04
1082    Walmart+    39  18  4.80    12.27   478.53

Any ideas why this could be? Could it be something with the tick marks? I tried changing the size and rotation to no avail. It would just look a lot more aesthetically pleasing if the bars were evenly spaced and directly above their respective tick marks. It only appears to happen whenever there is a red bar after multiple blue bars. I am not sure what to try in order to fix this...

enter image description here

Upvotes: 3

Views: 4472

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

That's because your data has only one bar per ticks while with hue it's supposed to have 2 bars per tick (one for each category). Try passing dodge=False to sns.barplot:

sns.barplot(x=value.index, y="CustomerValue", data=value, 
            order=value.index, hue='Response', dodge=False)

Output:

enter image description here

Upvotes: 7

Related Questions