@@ -1255,43 +1255,40 @@ def _check_bar_alignment(self, df, kind='bar', stacked=False,
1255
1255
align = align , width = width , position = position ,
1256
1256
grid = True )
1257
1257
1258
- tick_pos = np .arange (len (df ))
1259
-
1260
1258
axes = self ._flatten_visible (axes )
1261
1259
1262
1260
for ax in axes :
1263
1261
if kind == 'bar' :
1264
1262
axis = ax .xaxis
1265
1263
ax_min , ax_max = ax .get_xlim ()
1264
+ min_edge = min ([p .get_x () for p in ax .patches ])
1265
+ max_edge = max ([p .get_x () + p .get_width () for p in ax .patches ])
1266
1266
elif kind == 'barh' :
1267
1267
axis = ax .yaxis
1268
1268
ax_min , ax_max = ax .get_ylim ()
1269
+ min_edge = min ([p .get_y () for p in ax .patches ])
1270
+ max_edge = max ([p .get_y () + p .get_height () for p in ax .patches ])
1269
1271
else :
1270
1272
raise ValueError
1271
1273
1274
+ # GH 7498
1275
+ # compare margins between lim and bar edges
1276
+ self .assertAlmostEqual (ax_min , min_edge - 0.25 )
1277
+ self .assertAlmostEqual (ax_max , max_edge + 0.25 )
1278
+
1272
1279
p = ax .patches [0 ]
1273
1280
if kind == 'bar' and (stacked is True or subplots is True ):
1274
1281
edge = p .get_x ()
1275
1282
center = edge + p .get_width () * position
1276
- tickoffset = width * position
1277
1283
elif kind == 'bar' and stacked is False :
1278
1284
center = p .get_x () + p .get_width () * len (df .columns ) * position
1279
1285
edge = p .get_x ()
1280
- if align == 'edge' :
1281
- tickoffset = width * (position - 0.5 ) + p .get_width () * 1.5
1282
- else :
1283
- tickoffset = width * position + p .get_width ()
1284
1286
elif kind == 'barh' and (stacked is True or subplots is True ):
1285
1287
center = p .get_y () + p .get_height () * position
1286
1288
edge = p .get_y ()
1287
- tickoffset = width * position
1288
1289
elif kind == 'barh' and stacked is False :
1289
1290
center = p .get_y () + p .get_height () * len (df .columns ) * position
1290
1291
edge = p .get_y ()
1291
- if align == 'edge' :
1292
- tickoffset = width * (position - 0.5 ) + p .get_height () * 1.5
1293
- else :
1294
- tickoffset = width * position + p .get_height ()
1295
1292
else :
1296
1293
raise ValueError
1297
1294
@@ -1307,59 +1304,43 @@ def _check_bar_alignment(self, df, kind='bar', stacked=False,
1307
1304
else :
1308
1305
raise ValueError
1309
1306
1310
- # Check starting point and axes limit margin
1311
- self .assertEqual (ax_min , tick_pos [0 ] - tickoffset - 0.25 )
1312
- self .assertEqual (ax_max , tick_pos [- 1 ] - tickoffset + 1 )
1313
- # Check tick locations and axes limit margin
1314
- t_min = axis .get_ticklocs ()[0 ] - tickoffset
1315
- t_max = axis .get_ticklocs ()[- 1 ] - tickoffset
1316
- self .assertAlmostEqual (ax_min , t_min - 0.25 )
1317
- self .assertAlmostEqual (ax_max , t_max + 1.0 )
1318
1307
return axes
1319
1308
1320
1309
@slow
1321
1310
def test_bar_stacked_center (self ):
1322
1311
# GH2157
1323
1312
df = DataFrame ({'A' : [3 ] * 5 , 'B' : lrange (5 )}, index = lrange (5 ))
1324
- axes = self ._check_bar_alignment (df , kind = 'bar' , stacked = True )
1325
- # Check the axes has the same drawing range before fixing # GH4525
1326
- self .assertEqual (axes [0 ].get_xlim (), (- 0.5 , 4.75 ))
1327
-
1313
+ self ._check_bar_alignment (df , kind = 'bar' , stacked = True )
1328
1314
self ._check_bar_alignment (df , kind = 'bar' , stacked = True , width = 0.9 )
1329
-
1330
- axes = self ._check_bar_alignment (df , kind = 'barh' , stacked = True )
1331
- self .assertEqual (axes [0 ].get_ylim (), (- 0.5 , 4.75 ))
1332
-
1315
+ self ._check_bar_alignment (df , kind = 'barh' , stacked = True )
1333
1316
self ._check_bar_alignment (df , kind = 'barh' , stacked = True , width = 0.9 )
1334
1317
1335
1318
@slow
1336
1319
def test_bar_center (self ):
1337
1320
df = DataFrame ({'A' : [3 ] * 5 , 'B' : lrange (5 )}, index = lrange (5 ))
1338
- axes = self ._check_bar_alignment (df , kind = 'bar' , stacked = False )
1339
- self .assertEqual (axes [0 ].get_xlim (), (- 0.75 , 4.5 ))
1340
-
1321
+ self ._check_bar_alignment (df , kind = 'bar' , stacked = False )
1341
1322
self ._check_bar_alignment (df , kind = 'bar' , stacked = False , width = 0.9 )
1342
-
1343
- axes = self ._check_bar_alignment (df , kind = 'barh' , stacked = False )
1344
- self .assertEqual (axes [0 ].get_ylim (), (- 0.75 , 4.5 ))
1345
-
1323
+ self ._check_bar_alignment (df , kind = 'barh' , stacked = False )
1346
1324
self ._check_bar_alignment (df , kind = 'barh' , stacked = False , width = 0.9 )
1347
1325
1348
1326
@slow
1349
1327
def test_bar_subplots_center (self ):
1350
1328
df = DataFrame ({'A' : [3 ] * 5 , 'B' : lrange (5 )}, index = lrange (5 ))
1351
- axes = self ._check_bar_alignment (df , kind = 'bar' , subplots = True )
1352
- for ax in axes :
1353
- self .assertEqual (ax .get_xlim (), (- 0.5 , 4.75 ))
1354
-
1329
+ self ._check_bar_alignment (df , kind = 'bar' , subplots = True )
1355
1330
self ._check_bar_alignment (df , kind = 'bar' , subplots = True , width = 0.9 )
1356
-
1357
- axes = self ._check_bar_alignment (df , kind = 'barh' , subplots = True )
1358
- for ax in axes :
1359
- self .assertEqual (ax .get_ylim (), (- 0.5 , 4.75 ))
1360
-
1331
+ self ._check_bar_alignment (df , kind = 'barh' , subplots = True )
1361
1332
self ._check_bar_alignment (df , kind = 'barh' , subplots = True , width = 0.9 )
1362
1333
1334
+ @slow
1335
+ def test_bar_align_single_column (self ):
1336
+ df = DataFrame (randn (5 ))
1337
+ self ._check_bar_alignment (df , kind = 'bar' , stacked = False )
1338
+ self ._check_bar_alignment (df , kind = 'bar' , stacked = True )
1339
+ self ._check_bar_alignment (df , kind = 'barh' , stacked = False )
1340
+ self ._check_bar_alignment (df , kind = 'barh' , stacked = True )
1341
+ self ._check_bar_alignment (df , kind = 'bar' , subplots = True )
1342
+ self ._check_bar_alignment (df , kind = 'barh' , subplots = True )
1343
+
1363
1344
@slow
1364
1345
def test_bar_edge (self ):
1365
1346
df = DataFrame ({'A' : [3 ] * 5 , 'B' : lrange (5 )}, index = lrange (5 ))
0 commit comments