Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f6bb90

Browse files
committed
Merge branch 'v2.0.x'
2 parents 23bc09d + b530062 commit 9f6bb90

File tree

7 files changed

+143
-8
lines changed

7 files changed

+143
-8
lines changed

lib/matplotlib/cbook.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ class Grouper(object):
15591559
False
15601560
15611561
"""
1562-
def __init__(self, init=[]):
1562+
def __init__(self, init=()):
15631563
mapping = self._mapping = {}
15641564
for x in init:
15651565
mapping[ref(x)] = [ref(x)]
@@ -1611,6 +1611,14 @@ def joined(self, a, b):
16111611
except KeyError:
16121612
return False
16131613

1614+
def remove(self, a):
1615+
self.clean()
1616+
1617+
mapping = self._mapping
1618+
seta = mapping.pop(ref(a), None)
1619+
if seta is not None:
1620+
seta.remove(ref(a))
1621+
16141622
def __iter__(self):
16151623
"""
16161624
Iterate over each of the disjoint sets as a list.

lib/matplotlib/figure.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def add_axes(self, *args, **kwargs):
917917

918918
self._axstack.add(key, a)
919919
self.sca(a)
920-
a._remove_method = lambda ax: self.delaxes(ax)
920+
a._remove_method = self.__remove_ax
921921
self.stale = True
922922
a.stale_callback = _stale_figure_callback
923923
return a
@@ -1007,7 +1007,7 @@ def add_subplot(self, *args, **kwargs):
10071007

10081008
self._axstack.add(key, a)
10091009
self.sca(a)
1010-
a._remove_method = lambda ax: self.delaxes(ax)
1010+
a._remove_method = self.__remove_ax
10111011
self.stale = True
10121012
a.stale_callback = _stale_figure_callback
10131013
return a
@@ -1144,6 +1144,32 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
11441144
# Returned axis array will be always 2-d, even if nrows=ncols=1.
11451145
return axarr
11461146

1147+
def __remove_ax(self, ax):
1148+
def _reset_loc_form(axis):
1149+
axis.set_major_formatter(axis.get_major_formatter())
1150+
axis.set_major_locator(axis.get_major_locator())
1151+
axis.set_minor_formatter(axis.get_minor_formatter())
1152+
axis.set_minor_locator(axis.get_minor_locator())
1153+
1154+
def _break_share_link(ax, grouper):
1155+
siblings = grouper.get_siblings(ax)
1156+
if len(siblings) > 1:
1157+
grouper.remove(ax)
1158+
for last_ax in siblings:
1159+
if ax is last_ax:
1160+
continue
1161+
return last_ax
1162+
return None
1163+
1164+
self.delaxes(ax)
1165+
last_ax = _break_share_link(ax, ax._shared_y_axes)
1166+
if last_ax is not None:
1167+
_reset_loc_form(last_ax.yaxis)
1168+
1169+
last_ax = _break_share_link(ax, ax._shared_x_axes)
1170+
if last_ax is not None:
1171+
_reset_loc_form(last_ax.xaxis)
1172+
11471173
def clf(self, keep_observers=False):
11481174
"""
11491175
Clear the figure.

lib/matplotlib/tests/test_axes.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,6 +4197,49 @@ def test_auto_numticks():
41974197
fig, axes = plt.subplots(4, 4)
41984198

41994199

4200+
def test_remove_shared_axes():
4201+
def _helper_x(ax):
4202+
ax2 = ax.twinx()
4203+
ax2.remove()
4204+
ax.set_xlim(0, 15)
4205+
r = ax.xaxis.get_major_locator()()
4206+
assert r[-1] > 14
4207+
4208+
def _helper_y(ax):
4209+
ax2 = ax.twiny()
4210+
ax2.remove()
4211+
ax.set_ylim(0, 15)
4212+
r = ax.yaxis.get_major_locator()()
4213+
assert r[-1] > 14
4214+
4215+
# test all of the ways to get fig/ax sets
4216+
fig = plt.figure()
4217+
ax = fig.gca()
4218+
yield _helper_x, ax
4219+
yield _helper_y, ax
4220+
4221+
fig, ax = plt.subplots()
4222+
yield _helper_x, ax
4223+
yield _helper_y, ax
4224+
4225+
fig, ax_lst = plt.subplots(2, 2, sharex='all', sharey='all')
4226+
ax = ax_lst[0][0]
4227+
yield _helper_x, ax
4228+
yield _helper_y, ax
4229+
4230+
fig = plt.figure()
4231+
ax = fig.add_axes([.1, .1, .8, .8])
4232+
yield _helper_x, ax
4233+
yield _helper_y, ax
4234+
4235+
fig, ax_lst = plt.subplots(2, 2, sharex='all', sharey='all')
4236+
ax = ax_lst[0][0]
4237+
orig_xlim = ax_lst[0][1].get_xlim()
4238+
ax.remove()
4239+
ax.set_xlim(0, 5)
4240+
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)
4241+
4242+
42004243
if __name__ == '__main__':
42014244
import nose
42024245
import sys

lib/matplotlib/tests/test_cbook.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3+
import itertools
4+
from weakref import ref
35

46
from matplotlib.externals import six
57

@@ -376,3 +378,40 @@ def test_step_fails():
376378
np.arange(12))
377379
assert_raises(ValueError, cbook._step_validation,
378380
np.arange(12), np.arange(3))
381+
382+
383+
def test_grouper():
384+
class dummy():
385+
pass
386+
a, b, c, d, e = objs = [dummy() for j in range(5)]
387+
g = cbook.Grouper()
388+
g.join(*objs)
389+
assert set(list(g)[0]) == set(objs)
390+
assert set(g.get_siblings(a)) == set(objs)
391+
392+
for other in objs[1:]:
393+
assert g.joined(a, other)
394+
395+
g.remove(a)
396+
for other in objs[1:]:
397+
assert not g.joined(a, other)
398+
399+
for A, B in itertools.product(objs[1:], objs[1:]):
400+
assert g.joined(A, B)
401+
402+
403+
def test_grouper_private():
404+
class dummy():
405+
pass
406+
objs = [dummy() for j in range(5)]
407+
g = cbook.Grouper()
408+
g.join(*objs)
409+
# reach in and touch the internals !
410+
mapping = g._mapping
411+
412+
for o in objs:
413+
assert ref(o) in mapping
414+
415+
base_set = mapping[ref(objs[0])]
416+
for o in objs[1:]:
417+
assert mapping[ref(o)] is base_set

lib/mpl_toolkits/axes_grid1/colorbar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,8 @@ def __init__(self, ax, cmap=None,
374374
if format is None:
375375
if isinstance(self.norm, colors.LogNorm):
376376
# change both axis for proper aspect
377-
self.ax.xaxis.set_scale("log")
378-
self.ax.yaxis.set_scale("log")
379-
self.ax._update_transScale()
377+
self.ax.set_xscale("log")
378+
self.ax.set_yscale("log")
380379
self.cbar_axis.set_minor_locator(ticker.NullLocator())
381380
formatter = ticker.LogFormatter()
382381
else:

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import matplotlib
77
import matplotlib.pyplot as plt
8-
from matplotlib.testing.decorators import image_comparison
9-
from mpl_toolkits.axes_grid1 import make_axes_locatable, host_subplot
8+
from matplotlib.testing.decorators import image_comparison, cleanup
9+
from mpl_toolkits.axes_grid1 import make_axes_locatable, host_subplot, AxesGrid
10+
from matplotlib.colors import LogNorm
1011
from itertools import product
12+
1113
import numpy as np
1214

1315

@@ -83,6 +85,22 @@ def test_twin_axes_empty_and_removed():
8385
plt.subplots_adjust(wspace=0.5, hspace=1)
8486

8587

88+
@cleanup
89+
def test_axesgrid_colorbar_log_smoketest():
90+
fig = plt.figure()
91+
grid = AxesGrid(fig, 111, # modified to be only subplot
92+
nrows_ncols=(1, 1),
93+
label_mode="L",
94+
cbar_location="top",
95+
cbar_mode="single",
96+
)
97+
98+
Z = 10000 * np.random.rand(10, 10)
99+
im = grid[0].imshow(Z, interpolation="nearest", norm=LogNorm())
100+
101+
grid.cbar_axes[0].colorbar(im)
102+
103+
86104
if __name__ == '__main__':
87105
import nose
88106
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def run(self):
137137
print("Matplotlib does not support running tests with "
138138
"'python setup.py test'. Please run 'python tests.py'")
139139

140+
140141
class BuildExtraLibraries(BuildExtCommand):
141142
def run(self):
142143
for package in good_packages:
@@ -149,6 +150,7 @@ def run(self):
149150
cmdclass['test'] = NoopTestCommand
150151
cmdclass['build_ext'] = BuildExtraLibraries
151152

153+
152154
# patch bdist_wheel for a bug on windows
153155
# https://bitbucket.org/pypa/wheel/issues/91/cannot-create-a-file-when-that-file
154156
if os.name == 'nt':

0 commit comments

Comments
 (0)