@@ -3367,24 +3367,34 @@ def test_colors_of_columns_with_same_name(self):
3367
3367
@pytest .mark .parametrize ("method" , ["bar" , "barh" ])
3368
3368
def test_bar_ticklabel_consistence (self , method ):
3369
3369
# Draw two consecutiv bar plot with consistent ticklabels
3370
+ # The labels positions should not move between two drawing on the same axis
3370
3371
# GH: 26186
3371
3372
def get_main_axis (ax ):
3372
3373
if method == "barh" :
3373
3374
return ax .yaxis
3374
3375
elif method == "bar" :
3375
3376
return ax .xaxis
3376
3377
3378
+ # Plot the first bar plot
3377
3379
data = {"A" : 0 , "B" : 3 , "C" : - 4 }
3378
3380
df = pd .DataFrame .from_dict (data , orient = "index" , columns = ["Value" ])
3379
3381
ax = getattr (df .plot , method )()
3380
3382
ax .get_figure ().canvas .draw ()
3383
+
3384
+ # Retrieve the label positions for the first drawing
3381
3385
xticklabels = [t .get_text () for t in get_main_axis (ax ).get_ticklabels ()]
3382
3386
label_positions_1 = dict (zip (xticklabels , get_main_axis (ax ).get_ticklocs ()))
3387
+
3388
+ # Modify the dataframe order and values and plot on same axis
3383
3389
df = df .sort_values ("Value" ) * - 2
3384
3390
ax = getattr (df .plot , method )(ax = ax , color = "red" )
3385
3391
ax .get_figure ().canvas .draw ()
3392
+
3393
+ # Retrieve the label positions for the second drawing
3386
3394
xticklabels = [t .get_text () for t in get_main_axis (ax ).get_ticklabels ()]
3387
3395
label_positions_2 = dict (zip (xticklabels , get_main_axis (ax ).get_ticklocs ()))
3396
+
3397
+ # Assert that the label positions did not change between the plotting
3388
3398
assert label_positions_1 == label_positions_2
3389
3399
3390
3400
def test_bar_numeric (self ):
0 commit comments