|
1 | 1 | import pytest |
2 | | - |
| 2 | +import numpy as np |
3 | 3 | from pandas import DataFrame |
| 4 | +from pandas import unique |
4 | 5 | from pandas.tests.plotting.common import ( |
5 | 6 | _check_plot_works, |
6 | 7 | _check_ticks_props, |
@@ -58,3 +59,33 @@ def test_colorbar_layout(self): |
58 | 59 |
|
59 | 60 | fig.colorbar(cs0, ax=[axes["A"], axes["B"]], location="right") |
60 | 61 | 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