@@ -6782,3 +6782,58 @@ def test_ylabel_ha_with_position(ha):
67826782 ax .set_ylabel ("test" , y = 1 , ha = ha )
67836783 ax .yaxis .set_label_position ("right" )
67846784 assert ax .yaxis .get_label ().get_ha () == ha
6785+
6786+
6787+ def test_bar_label_location_vertical ():
6788+ ax = plt .gca ()
6789+ xs , heights = [1 , 2 ], [3 , - 4 ]
6790+ rects = ax .bar (xs , heights )
6791+ labels = ax .bar_label (rects )
6792+ assert labels [0 ].xy == (xs [0 ], heights [0 ])
6793+ assert labels [0 ].get_ha () == 'center'
6794+ assert labels [0 ].get_va () == 'bottom'
6795+ assert labels [1 ].xy == (xs [1 ], heights [1 ])
6796+ assert labels [1 ].get_ha () == 'center'
6797+ assert labels [1 ].get_va () == 'top'
6798+
6799+
6800+ def test_bar_label_location_horizontal ():
6801+ ax = plt .gca ()
6802+ ys , widths = [1 , 2 ], [3 , - 4 ]
6803+ rects = ax .barh (ys , widths )
6804+ labels = ax .bar_label (rects )
6805+ assert labels [0 ].xy == (widths [0 ], ys [0 ])
6806+ assert labels [0 ].get_ha () == 'left'
6807+ assert labels [0 ].get_va () == 'center'
6808+ assert labels [1 ].xy == (widths [1 ], ys [1 ])
6809+ assert labels [1 ].get_ha () == 'right'
6810+ assert labels [1 ].get_va () == 'center'
6811+
6812+
6813+ def test_bar_label_location_center ():
6814+ ax = plt .gca ()
6815+ ys , widths = [1 , 2 ], [3 , - 4 ]
6816+ rects = ax .barh (ys , widths )
6817+ labels = ax .bar_label (rects , mode = 'center' )
6818+ assert labels [0 ].xy == (widths [0 ] / 2 , ys [0 ])
6819+ assert labels [0 ].get_ha () == 'center'
6820+ assert labels [0 ].get_va () == 'center'
6821+ assert labels [1 ].xy == (widths [1 ] / 2 , ys [1 ])
6822+ assert labels [1 ].get_ha () == 'center'
6823+ assert labels [1 ].get_va () == 'center'
6824+
6825+
6826+ def test_bar_label_fmt ():
6827+ ax = plt .gca ()
6828+ rects = ax .bar ([1 , 2 ], [3 , - 4 ])
6829+ labels = ax .bar_label (rects , fmt = '%.2f' )
6830+ assert labels [0 ].get_text () == '3.00'
6831+ assert labels [1 ].get_text () == '-4.00'
6832+
6833+
6834+ def test_bar_label_captions ():
6835+ ax = plt .gca ()
6836+ rects = ax .bar ([1 , 2 ], [3 , - 4 ])
6837+ labels = ax .bar_label (rects , captions = ['A' , 'B' ])
6838+ assert labels [0 ].get_text () == 'A'
6839+ assert labels [1 ].get_text () == 'B'
0 commit comments