Skip to content

Commit 0994b3f

Browse files
committed
Scale margins to data, plus padding, by default
Fix matplotlib#4891
1 parent 6245d4e commit 0994b3f

11 files changed

Lines changed: 310 additions & 127 deletions

File tree

‎lib/matplotlib/artist.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def __init__(self):
118118
self._sketch = rcParams['path.sketch']
119119
self._path_effects = rcParams['path.effects']
120120

121+
self._margins = {}
122+
121123
def __getstate__(self):
122124
d = self.__dict__.copy()
123125
# remove the unpicklable remove method, this will get re-added on load
@@ -898,6 +900,89 @@ def set_zorder(self, level):
898900
self.pchanged()
899901
self.stale = True
900902

903+
def get_top_margin(self):
904+
"""
905+
Get whether a margin should be applied to the top of the Artist.
906+
"""
907+
return self._margins.get('top', True)
908+
909+
def set_top_margin(self, margin):
910+
"""
911+
Set whether a margin should be applied to the top of the Artist.
912+
"""
913+
if margin != self._margins.get('top', True):
914+
self.stale = True
915+
self._margins['top'] = margin
916+
917+
def get_bottom_margin(self):
918+
"""
919+
Get whether a margin should be applied to the bottom of the Artist.
920+
"""
921+
return self._margins.get('bottom', True)
922+
923+
def set_bottom_margin(self, margin):
924+
"""
925+
Set whether a margin should be applied to the bottom of the Artist.
926+
"""
927+
if margin != self._margins.get('bottom', True):
928+
self.stale = True
929+
self._margins['bottom'] = margin
930+
931+
def get_left_margin(self):
932+
"""
933+
Get whether a margin should be applied to the left of the Artist.
934+
"""
935+
return self._margins.get('left', True)
936+
937+
def set_left_margin(self, margin):
938+
"""
939+
Set whether a margin should be applied to the left of the Artist.
940+
"""
941+
if margin != self._margins.get('left', True):
942+
self.stale = True
943+
self._margins['left'] = margin
944+
945+
def get_right_margin(self):
946+
"""
947+
Get whether a margin should be applied to the right of the Artist.
948+
"""
949+
return self._margins.get('right', True)
950+
951+
def set_right_margin(self, margin):
952+
"""
953+
Set whether a margin should be applied to the right of the Artist.
954+
"""
955+
if margin != self._margins.get('right', True):
956+
self.stale = True
957+
self._margins['right'] = margin
958+
959+
def get_margins(self):
960+
"""
961+
Returns a dictionary describing whether a margin should be applied on
962+
each of the sides (top, bottom, left and right).
963+
"""
964+
return self._margins
965+
966+
def set_margins(self, margins):
967+
"""
968+
Set the dictionary describing whether a margin should be applied on
969+
each of the sides (top, bottom, left and right). Missing keys are
970+
assumed to be `True`. If `True` or `False` are passed in, all
971+
sides are set to that value.
972+
"""
973+
if margins in (True, False):
974+
margins = {
975+
'top': margins,
976+
'bottom': margins,
977+
'left': margins,
978+
'right': margins
979+
}
980+
981+
if margins != self._margins:
982+
self.stale = True
983+
984+
self._margins = margins
985+
901986
def update_from(self, other):
902987
'Copy properties from *other* to *self*.'
903988
self._transform = other._transform

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,16 +2104,21 @@ def make_iterable(x):
21042104
if yerr is not None:
21052105
yerr = self.convert_yunits(yerr)
21062106

2107-
if align == 'edge':
2108-
pass
2109-
elif align == 'center':
2107+
margins = {}
2108+
2109+
if orientation == 'vertical':
2110+
margins = {'bottom': False}
2111+
elif orientation == 'horizontal':
2112+
margins = {'left': False}
2113+
2114+
if align == 'center':
21102115
if orientation == 'vertical':
21112116
left = [left[i] - width[i] / 2. for i in xrange(len(left))]
21122117
elif orientation == 'horizontal':
21132118
bottom = [bottom[i] - height[i] / 2.
21142119
for i in xrange(len(bottom))]
21152120

2116-
else:
2121+
elif align != 'edge':
21172122
raise ValueError('invalid alignment: %s' % align)
21182123

21192124
args = zip(left, bottom, width, height, color, edgecolor, linewidth)
@@ -2129,7 +2134,8 @@ def make_iterable(x):
21292134
facecolor=c,
21302135
edgecolor=e,
21312136
linewidth=lw,
2132-
label='_nolegend_'
2137+
label='_nolegend_',
2138+
margins=margins
21332139
)
21342140
r.update(kwargs)
21352141
r.get_path()._interpolation_steps = 100
@@ -5256,7 +5262,7 @@ def pcolor(self, *args, **kwargs):
52565262

52575263
kwargs.setdefault('snap', False)
52585264

5259-
collection = mcoll.PolyCollection(verts, **kwargs)
5265+
collection = mcoll.PolyCollection(verts, margins=False, **kwargs)
52605266

52615267
collection.set_alpha(alpha)
52625268
collection.set_array(C)
@@ -5291,9 +5297,9 @@ def pcolor(self, *args, **kwargs):
52915297
maxy = np.amax(y)
52925298

52935299
corners = (minx, miny), (maxx, maxy)
5300+
self.add_collection(collection, autolim=False)
52945301
self.update_datalim(corners)
52955302
self.autoscale_view()
5296-
self.add_collection(collection, autolim=False)
52975303
return collection
52985304

52995305
@unpack_labeled_data(label_namer=None)
@@ -5408,7 +5414,8 @@ def pcolormesh(self, *args, **kwargs):
54085414

54095415
collection = mcoll.QuadMesh(
54105416
Nx - 1, Ny - 1, coords,
5411-
antialiased=antialiased, shading=shading, **kwargs)
5417+
antialiased=antialiased, shading=shading, margins=False,
5418+
**kwargs)
54125419
collection.set_alpha(alpha)
54135420
collection.set_array(C)
54145421
if norm is not None and not isinstance(norm, mcolors.Normalize):
@@ -5440,9 +5447,9 @@ def pcolormesh(self, *args, **kwargs):
54405447
maxy = np.amax(Y)
54415448

54425449
corners = (minx, miny), (maxx, maxy)
5450+
self.add_collection(collection, autolim=False)
54435451
self.update_datalim(corners)
54445452
self.autoscale_view()
5445-
self.add_collection(collection, autolim=False)
54465453
return collection
54475454

54485455
@unpack_labeled_data(label_namer=None)
@@ -5592,7 +5599,8 @@ def pcolorfast(self, *args, **kwargs):
55925599
# The QuadMesh class can also be changed to
55935600
# handle relevant superclass kwargs; the initializer
55945601
# should do much more than it does now.
5595-
collection = mcoll.QuadMesh(nc, nr, coords, 0, edgecolors="None")
5602+
collection = mcoll.QuadMesh(nc, nr, coords, 0, edgecolors="None",
5603+
margins=False)
55965604
collection.set_alpha(alpha)
55975605
collection.set_array(C)
55985606
collection.set_cmap(cmap)
@@ -5638,15 +5646,19 @@ def contour(self, *args, **kwargs):
56385646
if not self._hold:
56395647
self.cla()
56405648
kwargs['filled'] = False
5641-
return mcontour.QuadContourSet(self, *args, **kwargs)
5649+
contours = mcontour.QuadContourSet(self, *args, **kwargs)
5650+
self.autoscale_view()
5651+
return contours
56425652
contour.__doc__ = mcontour.QuadContourSet.contour_doc
56435653

56445654
@unpack_labeled_data()
56455655
def contourf(self, *args, **kwargs):
56465656
if not self._hold:
56475657
self.cla()
56485658
kwargs['filled'] = True
5649-
return mcontour.QuadContourSet(self, *args, **kwargs)
5659+
contours = mcontour.QuadContourSet(self, *args, **kwargs)
5660+
self.autoscale_view()
5661+
return contours
56505662
contourf.__doc__ = mcontour.QuadContourSet.contour_doc
56515663

56525664
def clabel(self, CS, *args, **kwargs):
@@ -6026,6 +6038,11 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
60266038
else:
60276039
n = [m[slc].cumsum()[slc] for m in n]
60286040

6041+
if orientation == 'horizontal':
6042+
margins = {'left': False}
6043+
else:
6044+
margins = {'bottom': False}
6045+
60296046
patches = []
60306047

60316048
if histtype.startswith('bar'):
@@ -6166,14 +6183,16 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61666183
patches.append(self.fill(
61676184
x, y,
61686185
closed=True,
6169-
facecolor=c))
6186+
facecolor=c,
6187+
margins=margins))
61706188
else:
61716189
for x, y, c in reversed(list(zip(xvals, yvals, color))):
61726190
split = 2 * len(bins)
61736191
patches.append(self.fill(
61746192
x[:split], y[:split],
61756193
closed=False, edgecolor=c,
6176-
fill=False))
6194+
fill=False,
6195+
margins=margins))
61776196

61786197
# we return patches, so put it back in the expected order
61796198
patches.reverse()

0 commit comments

Comments
 (0)