Fix twin remove by tacaswell · Pull Request #5682 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/matplotlib/cbook.py
30 changes: 28 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def add_axes(self, *args, **kwargs):

self._axstack.add(key, a)
self.sca(a)
a._remove_method = lambda ax: self.delaxes(ax)
a._remove_method = self.__remove_ax
self.stale = True
a.stale_callback = _stale_figure_callback
return a
Expand Down Expand Up @@ -1006,11 +1006,37 @@ def add_subplot(self, *args, **kwargs):

self._axstack.add(key, a)
self.sca(a)
a._remove_method = lambda ax: self.delaxes(ax)
a._remove_method = self.__remove_ax
self.stale = True
a.stale_callback = _stale_figure_callback
return a

def __remove_ax(self, ax):
def _reset_loc_form(axis):
axis.set_major_formatter(axis.get_major_formatter())
axis.set_major_locator(axis.get_major_locator())
axis.set_minor_formatter(axis.get_minor_formatter())
axis.set_minor_locator(axis.get_minor_locator())

def _break_share_link(ax, grouper):
siblings = grouper.get_siblings(ax)
if len(siblings) > 1:
grouper.remove(ax)
for last_ax in siblings:
if ax is last_ax:
continue
return last_ax
return None

self.delaxes(ax)
last_ax = _break_share_link(ax, ax._shared_y_axes)
if last_ax is not None:
_reset_loc_form(last_ax.yaxis)

last_ax = _break_share_link(ax, ax._shared_x_axes)
if last_ax is not None:
_reset_loc_form(last_ax.xaxis)

def clf(self, keep_observers=False):
"""
Clear the figure.
Expand Down
46 changes: 46 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4086,11 +4086,57 @@ def test_shared_scale():
assert_equal(ax.get_yscale(), 'linear')
assert_equal(ax.get_xscale(), 'linear')


@cleanup
def test_violin_point_mass():
"""Violin plot should handle point mass pdf gracefully."""
plt.violinplot(np.array([0, 0]))


@cleanup
def test_remove_shared_axes():

def _helper_x(ax):
ax2 = ax.twinx()
ax2.remove()
ax.set_xlim(0, 15)
r = ax.xaxis.get_major_locator()()
assert r[-1] > 14

def _helper_y(ax):
ax2 = ax.twiny()
ax2.remove()
ax.set_ylim(0, 15)
r = ax.yaxis.get_major_locator()()
assert r[-1] > 14

# test all of the ways to get fig/ax sets
fig = plt.figure()
ax = fig.gca()
yield _helper_x, ax
yield _helper_y, ax

fig, ax = plt.subplots()
yield _helper_x, ax
yield _helper_y, ax

fig, ax_lst = plt.subplots(2, 2, sharex='all', sharey='all')
ax = ax_lst[0][0]
yield _helper_x, ax
yield _helper_y, ax

fig = plt.figure()
ax = fig.add_axes([.1, .1, .8, .8])
yield _helper_x, ax
yield _helper_y, ax

fig, ax_lst = plt.subplots(2, 2, sharex='all', sharey='all')
ax = ax_lst[0][0]
orig_xlim = ax_lst[0][1].get_xlim()
ax.remove()
ax.set_xlim(0, 5)
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)

if __name__ == '__main__':
import nose
import sys
Expand Down
39 changes: 39 additions & 0 deletions lib/matplotlib/tests/test_cbook.py