@@ -62,28 +62,43 @@ def test_colorbar_layout(self):
6262
6363 def test_bar_subplot_stacking (self ):
6464 #GH Issue 61018
65- #Extracts height and location data
6665 test_data = np .random .default_rng (3 ).integers (0 ,100 ,5 )
67- df = DataFrame ({"a " : test_data , "b " : test_data [::- 1 ]})
68- ax = _check_plot_works ( df .plot , subplots = [('a ' ,'b ' )], kind = "bar" , stacked = True )
66+ df = DataFrame ({"A " : test_data , "B " : test_data [::- 1 ], "C" : test_data [ 0 ]})
67+ ax = df .plot ( subplots = [('A ' ,'B ' )], kind = "bar" , stacked = True )
6968
70- #get xy and height of squares that represent the data graphed from the df
71- #we would expect the height value of A to be reflected in the Y coord of B
72- data_from_plot_mat = [(x .get_x (), x .get_y (), x .get_height ()) for x in ax [0 ].findobj (plt .Rectangle ) if x .get_height () in test_data ]
73- data_from_plot_df = DataFrame (data = data_from_plot_mat , columns = ["x_coord" , "y_coord" , "height" ])
74- unique_x_loc = unique (data_from_plot_df ["x_coord" ])
75-
76- plot_a_df = data_from_plot_df .iloc [:len (test_data )]
77- plot_b_df = data_from_plot_df .iloc [len (test_data ):].reset_index ()
78- total_bar_height = plot_a_df ["height" ].add (plot_b_df ["height" ])
69+ #finds all the rectangles that represent the values from both subplots
70+ data_from_subplots = [[(x .get_x (), x .get_y (), x .get_height ()) for x in ax [i ].findobj (plt .Rectangle ) if x .get_height () in test_data ] for i in range (0 ,2 )]
7971
72+ #get xy and height of squares that represent the data graphed from the df
73+ #we would expect the height value of A to be reflected in the Y coord of B in subplot 1
74+ subplot_data_df_list = []
75+ unique_x_loc_list = []
76+ for i in range (0 ,len (data_from_subplots )):
77+ subplot_data_df = DataFrame (data = data_from_subplots [i ], columns = ["x_coord" , "y_coord" , "height" ])
78+ unique_x_loc = unique (subplot_data_df ["x_coord" ])
79+
80+ subplot_data_df_list .append (subplot_data_df )
81+ unique_x_loc_list .append (unique_x_loc )
82+
83+ #Checks subplot 1
84+ plot_A_df = subplot_data_df_list [0 ].iloc [:len (test_data )]
85+ plot_B_df = subplot_data_df_list [0 ].iloc [len (test_data ):].reset_index ()
86+ total_bar_height = plot_A_df ["height" ].add (plot_B_df ["height" ])
8087 #check number of bars matches the number of data plotted
81- assert len (unique_x_loc ) == len (test_data )
82-
88+ assert len (unique_x_loc_list [0 ]) == len (test_data )
8389 #checks that the first set of bars are the correct height and that the second one starts at the top of the first, additional checks the combined height of the bars are correct
84- assert (plot_a_df ["height" ] == test_data ).all ()
85- assert (plot_b_df ["y_coord" ] == test_data ).all ()
90+ assert (plot_A_df ["height" ] == test_data ).all ()
91+ assert (plot_B_df ["y_coord" ] == test_data ).all ()
8692 assert (total_bar_height == test_data + test_data [::- 1 ]).all ()
8793
94+ #Checks subplot 2
95+ plot_C_df = subplot_data_df_list [1 ].iloc [:len (test_data )]
96+ #check number of bars matches the number of data plotted
97+ assert len (unique_x_loc_list [1 ]) == len (test_data )
98+ #checks that all the bars start at zero and are the correct height
99+ assert (plot_C_df ["height" ] == test_data [0 ]).all ()
100+ assert (plot_C_df ["y_coord" ] == 0 ).all ()
101+
102+
88103
89104
0 commit comments