Skip to content

Commit 79b4c27

Browse files
committed
test case for subplot stacking
1 parent 09de147 commit 79b4c27

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

pandas/tests/plotting/test_common.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
2-
2+
import numpy as np
33
from pandas import DataFrame
4+
from pandas import unique
45
from pandas.tests.plotting.common import (
56
_check_plot_works,
67
_check_ticks_props,
@@ -58,3 +59,33 @@ def test_colorbar_layout(self):
5859

5960
fig.colorbar(cs0, ax=[axes["A"], axes["B"]], location="right")
6061
DataFrame(x).plot(ax=axes["C"])
62+
63+
def test_bar_subplot_stacking(self):
64+
#GH Issue 61018
65+
#Extracts height and location data
66+
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)
69+
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"])
79+
80+
print(test_data + test_data[::-1])
81+
82+
#check number of bars matches the number of data plotted
83+
assert len(unique_x_loc) == len(test_data)
84+
85+
#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
86+
assert (plot_a_df["height"] == test_data).all()
87+
assert (plot_b_df["y_coord"] == test_data).all()
88+
assert (total_bar_height == test_data + test_data[::-1]).all()
89+
90+
91+

0 commit comments

Comments
 (0)