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