Skip to content

Commit 383e7a4

Browse files
committed
TST: Add test for plotting MultiIndex bar plot
A fix to issue pandas-dev#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 a1d9a53 commit 383e7a4

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
@@ -3478,6 +3478,33 @@ def test_bar_numeric(self):
34783478
ticklocs = ax.xaxis.get_ticklocs()
34793479
tm.assert_numpy_array_equal(ticklocs, index)
34803480

3481+
def test_bar_multiindex(self):
3482+
# Test from pandas/doc/source/user_guide/visualization.rst
3483+
# at section Plotting With Error Bars
3484+
# Related to issue GH: 26186
3485+
3486+
ix3 = pd.MultiIndex.from_arrays(
3487+
[
3488+
["a", "a", "a", "a", "b", "b", "b", "b"],
3489+
["foo", "foo", "bar", "bar", "foo", "foo", "bar", "bar"],
3490+
],
3491+
names=["letter", "word"],
3492+
)
3493+
3494+
df3 = pd.DataFrame(
3495+
{"data1": [3, 2, 4, 3, 2, 4, 3, 2], "data2": [6, 5, 7, 5, 4, 5, 6, 5]},
3496+
index=ix3,
3497+
)
3498+
3499+
# Group by index labels and take the means and standard deviations
3500+
# for each group
3501+
gp3 = df3.groupby(level=("letter", "word"))
3502+
means = gp3.mean()
3503+
errors = gp3.std()
3504+
3505+
# No assertion we just ensure that we can plot a MultiIndex bar plot
3506+
means.plot.bar(yerr=errors, capsize=4)
3507+
34813508

34823509
def _generate_4_axes_via_gridspec():
34833510
import matplotlib as mpl

0 commit comments

Comments
 (0)