Skip to content

Commit 66dff6b

Browse files
committed
TST: Add test for plotting MultiIndex bar plot
A fix to issue #26186 revealed no tests existed about plotting a bar plot for a MultiIndex, but a section of the user guide visualization did. This section of the user guide is now in the test suite.
1 parent f122ad8 commit 66dff6b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/tests/plotting/test_frame.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,33 @@ def test_bar_numeric(self):
32823282
ticklocs = ax.xaxis.get_ticklocs()
32833283
tm.assert_numpy_array_equal(ticklocs, index)
32843284

3285+
def test_bar_multiindex(self):
3286+
# Test from pandas/doc/source/user_guide/visualization.rst
3287+
# at section Plotting With Error Bars
3288+
# Related to issue GH: 26186
3289+
3290+
ix3 = pd.MultiIndex.from_arrays(
3291+
[
3292+
["a", "a", "a", "a", "b", "b", "b", "b"],
3293+
["foo", "foo", "bar", "bar", "foo", "foo", "bar", "bar"],
3294+
],
3295+
names=["letter", "word"],
3296+
)
3297+
3298+
df3 = pd.DataFrame(
3299+
{"data1": [3, 2, 4, 3, 2, 4, 3, 2], "data2": [6, 5, 7, 5, 4, 5, 6, 5]},
3300+
index=ix3,
3301+
)
3302+
3303+
# Group by index labels and take the means and standard deviations
3304+
# for each group
3305+
gp3 = df3.groupby(level=("letter", "word"))
3306+
means = gp3.mean()
3307+
errors = gp3.std()
3308+
3309+
# No assertion we just ensure that we can plot a MultiIndex bar plot
3310+
means.plot.bar(yerr=errors, capsize=4)
3311+
32853312

32863313
def _generate_4_axes_via_gridspec():
32873314
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)