diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 768e39e9126e..f1f550ed5416 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3853,7 +3853,7 @@ def apply_mask(arrays, mask): (data_line, tuple(caplines), tuple(barcols)), has_xerr=(xerr is not None), has_yerr=(yerr is not None), label=label) - self.containers.append(errorbar_container) + self.add_container(errorbar_container) return errorbar_container # (l0, caplines, barcols) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cb0646820dab..beac9850539e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4274,6 +4274,24 @@ def test_errorbar_nonefmt(): assert np.all(errbar.get_color() == mcolors.to_rgba('C0')) +def test_errorbar_remove(): + x = np.arange(5) + y = np.arange(5) + + fig, ax = plt.subplots() + ec = ax.errorbar(x, y, xerr=1, yerr=1) + + assert len(ax.containers) == 1 + assert len(ax.lines) == 5 + assert len(ax.collections) == 2 + + ec.remove() + + assert not ax.containers + assert not ax.lines + assert not ax.collections + + def test_errorbar_line_specific_kwargs(): # Check that passing line-specific keyword arguments will not result in # errors.