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

Skip to content

Commit 2374c9b

Browse files
authored
Merge pull request #7850 from dstansby/grid1-loc-codes
FIX: Allow AnchoredOffset to take a string-like location code
2 parents fa95b58 + c2dc353 commit 2374c9b

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

examples/axes_grid1/inset_locator_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def add_sizebar(ax, size):
3131
# second subplot
3232
ax2.set_aspect(1.)
3333

34-
axins = zoomed_inset_axes(ax2, 0.5, loc=1) # zoom = 0.5
34+
axins = zoomed_inset_axes(ax2, 0.5, loc='upper right') # zoom = 0.5
3535
# fix the number of ticks on the inset axes
3636
axins.yaxis.get_major_locator().set_params(nbins=7)
3737
axins.xaxis.get_major_locator().set_params(nbins=7)

lib/matplotlib/offsetbox.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from matplotlib import rcParams
3434

3535
from matplotlib import docstring
36+
from matplotlib.cbook import is_string_like
3637

3738
#from bboximage import BboxImage
3839
from matplotlib.image import BboxImage
@@ -986,6 +987,19 @@ class AnchoredOffsetbox(OffsetBox):
986987
"""
987988
zorder = 5 # zorder of the legend
988989

990+
# Location codes
991+
codes = {'upper right': 1,
992+
'upper left': 2,
993+
'lower left': 3,
994+
'lower right': 4,
995+
'right': 5,
996+
'center left': 6,
997+
'center right': 7,
998+
'lower center': 8,
999+
'upper center': 9,
1000+
'center': 10,
1001+
}
1002+
9891003
def __init__(self, loc,
9901004
pad=0.4, borderpad=0.5,
9911005
child=None, prop=None, frameon=True,
@@ -1028,6 +1042,14 @@ def __init__(self, loc,
10281042
self.set_bbox_to_anchor(bbox_to_anchor, bbox_transform)
10291043
self.set_child(child)
10301044

1045+
if is_string_like(loc):
1046+
try:
1047+
loc = self.codes[loc]
1048+
except KeyError:
1049+
raise ValueError('Unrecognized location "%s". Valid '
1050+
'locations are\n\t%s\n'
1051+
% (loc, '\n\t'.join(self.codes)))
1052+
10311053
self.loc = loc
10321054
self.borderpad = borderpad
10331055
self.pad = pad

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,27 @@ def test_offsetbox_clip_children():
8282
da.clip_children = True
8383
assert_true(fig.stale)
8484

85+
86+
@cleanup
87+
def test_offsetbox_loc_codes():
88+
# Check that valid string location codes all work with an AnchoredOffsetbox
89+
codes = {'upper right': 1,
90+
'upper left': 2,
91+
'lower left': 3,
92+
'lower right': 4,
93+
'right': 5,
94+
'center left': 6,
95+
'center right': 7,
96+
'lower center': 8,
97+
'upper center': 9,
98+
'center': 10,
99+
}
100+
fig, ax = plt.subplots()
101+
da = DrawingArea(100, 100)
102+
for code in codes:
103+
anchored_box = AnchoredOffsetbox(loc=code, child=da)
104+
ax.add_artist(anchored_box)
105+
fig.canvas.draw()
106+
85107
if __name__ == '__main__':
86108
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)