Skip to content

Commit 6f47a80

Browse files
committed
added unittest for colorbar creation.
1 parent be049b2 commit 6f47a80

15 files changed

+2610
-104
lines changed

lib/matplotlib/colorbar.py

Lines changed: 79 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
*ax*
154154
None | parent axes object(s) from which space for a new
155155
colorbar axes will be stolen. If a list of axes is given
156-
they will be resized to make room for the colorbar axes.
156+
they will all be resized to make room for the colorbar axes.
157157
*use_gridspec*
158158
False | If *cax* is None, a new *cax* is created as an instance of
159159
Axes. If *ax* is an instance of Subplot and *use_gridspec* is True,
@@ -258,7 +258,7 @@ def __init__(self, ax, cmap=None,
258258
values=None,
259259
boundaries=None,
260260
orientation='vertical',
261-
location='right',
261+
ticklocation='auto',
262262
extend='neither',
263263
spacing='uniform', # uniform or proportional
264264
ticks=None,
@@ -282,7 +282,6 @@ def __init__(self, ax, cmap=None,
282282
self._inside = self._slice_dict[extend]
283283
self.spacing = spacing
284284
self.orientation = orientation
285-
self.location = location
286285
self.drawedges = drawedges
287286
self.filled = filled
288287
self.extendfrac = extendfrac
@@ -292,7 +291,12 @@ def __init__(self, ax, cmap=None,
292291
self.outline = None
293292
self.patch = None
294293
self.dividers = None
295-
self.set_label('')
294+
295+
if ticklocation == 'auto':
296+
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
297+
self.ticklocation = ticklocation
298+
299+
self.set_label(label)
296300
if cbook.iterable(ticks):
297301
self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
298302
else:
@@ -342,14 +346,14 @@ def config_axis(self):
342346
if self.orientation == 'vertical':
343347
ax.xaxis.set_ticks([])
344348
# location is either one of 'bottom' or 'top'
345-
ax.yaxis.set_label_position(self.location)
346-
ax.yaxis.set_ticks_position(self.location)
349+
ax.yaxis.set_label_position(self.ticklocation)
350+
ax.yaxis.set_ticks_position(self.ticklocation)
347351
else:
348352
ax.yaxis.set_ticks([])
349353
# location is either one of 'left' or 'right'
350-
ax.xaxis.set_label_position(self.location)
354+
ax.xaxis.set_label_position(self.ticklocation)
351355
# XXX This wasn't enabled before...
352-
ax.xaxis.set_ticks_position(self.location)
356+
ax.xaxis.set_ticks_position(self.ticklocation)
353357

354358
self._set_label()
355359

@@ -956,112 +960,91 @@ def update_bruteforce(self, mappable):
956960

957961

958962
@docstring.Substitution(make_axes_kw_doc)
959-
def make_axes_orig(parent, **kw):
960-
'''
961-
Resize and reposition a parent axes, and return a child
962-
axes suitable for a colorbar::
963-
964-
cax, kw = make_axes(parent, **kw)
965-
966-
Keyword arguments may include the following (with defaults):
967-
968-
*orientation*
969-
'vertical' or 'horizontal'
970-
971-
%s
972-
973-
All but the first of these are stripped from the input kw set.
974-
975-
Returns (cax, kw), the child axes and the reduced kw dictionary.
976-
'''
977-
orientation = kw.setdefault('orientation', 'vertical')
978-
fraction = kw.pop('fraction', 0.15)
979-
shrink = kw.pop('shrink', 1.0)
980-
aspect = kw.pop('aspect', 20)
981-
#pb = transforms.PBox(parent.get_position())
982-
pb = parent.get_position(original=True).frozen()
983-
if orientation == 'vertical':
984-
pad = kw.pop('pad', 0.05)
985-
x1 = 1.0 - fraction
986-
pb1, pbx, pbcb = pb.splitx(x1 - pad, x1)
987-
pbcb = pbcb.shrunk(1.0, shrink).anchored('C', pbcb)
988-
anchor = kw.pop('anchor', (0.0, 0.5))
989-
panchor = kw.pop('panchor', (1.0, 0.5))
990-
else:
991-
pad = kw.pop('pad', 0.15)
992-
pbcb, pbx, pb1 = pb.splity(fraction, fraction + pad)
993-
pbcb = pbcb.shrunk(shrink, 1.0).anchored('C', pbcb)
994-
aspect = 1.0 / aspect
995-
anchor = kw.pop('anchor', (0.5, 1.0))
996-
panchor = kw.pop('panchor', (0.5, 0.0))
997-
parent.set_position(pb1)
998-
parent.set_anchor(panchor)
999-
fig = parent.get_figure()
1000-
cax = fig.add_axes(pbcb)
1001-
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
1002-
return cax, kw
1003-
1004-
1005-
@docstring.Substitution(make_axes_kw_doc)
1006-
def make_axes(parent, location=None, orientation=None, fraction=0.15, shrink=1.0, aspect=20, **kw):
963+
def make_axes(parents, location=None, orientation=None, fraction=0.15, shrink=1.0, aspect=20, **kw):
1007964
locations = ["left", "right", "top", "bottom"]
1008965
if orientation is not None and location is not None:
1009966
raise TypeError('position and orientation are mutually exclusive. Consider ' \
1010-
'setting the position to any of %s' % ','.join(locations))
1011-
1012-
# must pump out an orientation for colorbar creation
1013-
if location in ['left', 'right']:
1014-
kw['orientation'] = 'vertical'
1015-
kw['location'] = location
1016-
anchor = kw.pop('anchor', (0.0, 0.5))
1017-
# define the parent's anchor to be next to the new colorbar axes
1018-
panchor = kw.pop('panchor', (1.0, 0.5))
1019-
else:
1020-
kw['orientation'] = 'horizontal'
1021-
kw['location'] = location
1022-
anchor = kw.pop('anchor', (0.5, 1.0))
1023-
# define the parent's anchor to be next to the new colorbar axes
1024-
panchor = kw.pop('panchor', (0.5, 0.0))
1025-
1026-
# define padding between colorbar axes and parent axes in axes coordinates.
1027-
# For best outcomes, pad is best at 0.15 when location is "bottom"
1028-
if location == 'bottom':
1029-
pad = kw.pop('pad', 0.0)
1030-
else:
1031-
pad = kw.pop('pad', 0.00)
1032-
1033-
if isinstance(parent, list):
1034-
parents_bbox = mtrans.Bbox.union([ax.get_position(original=True).frozen() \
1035-
for ax in parent])
967+
'setting the position to any of %s' % ', '.join(locations))
968+
969+
# allow the user to not specify the location by specifying the orientation instead
970+
if location is None:
971+
location = 'right' if orientation == 'vertical' else 'bottom'
972+
973+
if location not in locations:
974+
raise ValueError('Invalid colorbar location. Must be one of %s' % ', '.join(locations))
975+
976+
default_location_settings = {'left': {'anchor': (1.0, 0.5),
977+
'panchor': (0.0, 0.5),
978+
'pad': 0.10,
979+
'orientation': 'vertical'},
980+
'right': {'anchor': (0.0, 0.5),
981+
'panchor': (1.0, 0.5),
982+
'pad': 0.05,
983+
'orientation': 'vertical'},
984+
'top': {'anchor': (0.5, 0.0),
985+
'panchor': (0.5, 1.0),
986+
'pad': 0.05,
987+
'orientation': 'horizontal'},
988+
'bottom': {'anchor': (0.5, 1.0),
989+
'panchor': (0.5, 0.0),
990+
'pad': 0.15, # backwards compat
991+
'orientation': 'horizontal'},
992+
}
993+
994+
loc_settings = default_location_settings[location]
995+
996+
# put appropriate values into the kw dict for passing back to
997+
# the Colorbar class
998+
kw['orientation'] = loc_settings['orientation']
999+
kw['ticklocation'] = location
1000+
1001+
anchor = kw.pop('anchor', loc_settings['anchor'])
1002+
parent_anchor = kw.pop('panchor', loc_settings['panchor'])
1003+
pad = kw.pop('pad', loc_settings['pad'])
1004+
1005+
1006+
# turn parents into a list if it is not already
1007+
if not isinstance(parents, (list, tuple)):
1008+
parents = [parents]
1009+
1010+
fig = parents[0].get_figure()
1011+
if not all(fig is ax.get_figure() for ax in parents):
1012+
raise ValueError('Unable to create a colorbar axes as not all ' + \
1013+
'parents share the same figure.')
1014+
1015+
# take a bounding box around all of the given axes
1016+
parents_bbox = mtrans.Bbox.union([ax.get_position(original=True).frozen() \
1017+
for ax in parents])
10361018

10371019
pb = parents_bbox
10381020
if location in ('left', 'right'):
10391021
if location == 'left':
1040-
pbcb, _, pb1 = pb.splitx(1 - fraction, fraction + pad)
1022+
pbcb, _, pb1 = pb.splitx(fraction, fraction + pad)
10411023
else:
10421024
pb1, _, pbcb = pb.splitx(1 - fraction - pad, 1 - fraction)
1043-
pbcb = pbcb.shrunk(1.0, shrink).anchored('C', pbcb)
1044-
1025+
pbcb = pbcb.shrunk(1.0, shrink).anchored(anchor, pbcb)
10451026
else:
1046-
if location == 'top':
1047-
pb1, _, pbcb = pb.splity(1 - fraction - pad, fraction)
1048-
else:
1027+
if location == 'bottom':
10491028
pbcb, _, pb1 = pb.splity(fraction, fraction + pad)
1050-
pbcb = pbcb.shrunk(shrink, 1.0).anchored('C', pbcb)
1029+
else:
1030+
pb1, _, pbcb = pb.splity(1 - fraction - pad, 1 - fraction)
1031+
pbcb = pbcb.shrunk(shrink, 1.0).anchored(anchor, pbcb)
1032+
10511033
# define the aspect ratio in terms of y's per x rather than x's per y
10521034
aspect = 1.0/aspect
10531035

1036+
# define a transform which takes us from old axes coordinates to
1037+
# new axes coordinates
10541038
shrinking_trans = mtrans.BboxTransform(parents_bbox, pb1)
10551039

1056-
for ax in parent:
1040+
# transform each of the axes in parents using the new transform
1041+
for ax in parents:
10571042
new_posn = shrinking_trans.transform(ax.get_position())
10581043
new_posn = mtrans.Bbox(new_posn)
10591044
ax.set_position(new_posn)
1060-
if panchor is not False:
1061-
ax.set_anchor(panchor)
1045+
if parent_anchor is not False:
1046+
ax.set_anchor(parent_anchor)
10621047

1063-
# XXX test all axes must be on the same figure...
1064-
fig = parent[0].get_figure()
10651048
cax = fig.add_axes(pbcb)
10661049
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
10671050
return cax, kw
@@ -1101,6 +1084,8 @@ def make_axes_gridspec(parent, **kw):
11011084
'''
11021085

11031086
orientation = kw.setdefault('orientation', 'vertical')
1087+
kw['ticklocation'] = 'auto'
1088+
11041089
fraction = kw.pop('fraction', 0.15)
11051090
shrink = kw.pop('shrink', 1.0)
11061091
aspect = kw.pop('aspect', 20)

lib/matplotlib/figure.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,17 +1451,15 @@ def subplots_adjust(self, *args, **kwargs):
14511451
14521452
"""
14531453
self.subplotpars.update(*args, **kwargs)
1454-
import matplotlib.axes
14551454
for ax in self.axes:
1456-
if not isinstance(ax, matplotlib.axes.SubplotBase):
1455+
if not isinstance(ax, SubplotBase):
14571456
# Check if sharing a subplots axis
14581457
if (ax._sharex is not None and
1459-
isinstance(ax._sharex,
1460-
matplotlib.axes.SubplotBase)):
1458+
isinstance(ax._sharex, SubplotBase)):
14611459
ax._sharex.update_params()
14621460
ax.set_position(ax._sharex.figbox)
14631461
elif (ax._sharey is not None and
1464-
isinstance(ax._sharey, matplotlib.axes.SubplotBase)):
1462+
isinstance(ax._sharey, SubplotBase)):
14651463
ax._sharey.update_params()
14661464
ax.set_position(ax._sharey.figbox)
14671465
else:

lib/matplotlib/testing/compare.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ def compare_images( expected, actual, tol, in_decorator=False ):
300300
actual = convert(actual, False)
301301
expected = convert(expected, True)
302302

303+
if not os.path.exists(expected):
304+
raise IOError('Baseline image %r does not exist.' % expected)
305+
303306
# open the image files and remove the alpha channel (if it exists)
304307
expectedImage = _png.read_png_int( expected )
305308
actualImage = _png.read_png_int( actual )
41.8 KB
Binary file not shown.
13.9 KB
Loading
32.3 KB
Binary file not shown.
11.2 KB
Loading
13.6 KB
Binary file not shown.
6.65 KB
Loading
20.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)