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

Skip to content

Commit 9617dfe

Browse files
tacaswellmdboom
authored andcommitted
Merge pull request #5583 from mdboom/padding
API: Use data limits plus a little padding by default
1 parent 3423912 commit 9617dfe

16 files changed

+5184
-5038
lines changed

lib/matplotlib/artist.py

Lines changed: 95 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
@@ -897,6 +899,99 @@ def set_zorder(self, level):
897899
self.pchanged()
898900
self.stale = True
899901

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

52665272
kwargs.setdefault('snap', False)
52675273

5268-
collection = mcoll.PolyCollection(verts, **kwargs)
5274+
collection = mcoll.PolyCollection(verts, margins=False, **kwargs)
52695275

52705276
collection.set_alpha(alpha)
52715277
collection.set_array(C)
@@ -5300,9 +5306,9 @@ def pcolor(self, *args, **kwargs):
53005306
maxy = np.amax(y)
53015307

53025308
corners = (minx, miny), (maxx, maxy)
5309+
self.add_collection(collection, autolim=False)
53035310
self.update_datalim(corners)
53045311
self.autoscale_view()
5305-
self.add_collection(collection, autolim=False)
53065312
return collection
53075313

53085314
@unpack_labeled_data(label_namer=None)
@@ -5417,7 +5423,8 @@ def pcolormesh(self, *args, **kwargs):
54175423

54185424
collection = mcoll.QuadMesh(
54195425
Nx - 1, Ny - 1, coords,
5420-
antialiased=antialiased, shading=shading, **kwargs)
5426+
antialiased=antialiased, shading=shading, margins=False,
5427+
**kwargs)
54215428
collection.set_alpha(alpha)
54225429
collection.set_array(C)
54235430
if norm is not None and not isinstance(norm, mcolors.Normalize):
@@ -5449,9 +5456,9 @@ def pcolormesh(self, *args, **kwargs):
54495456
maxy = np.amax(Y)
54505457

54515458
corners = (minx, miny), (maxx, maxy)
5459+
self.add_collection(collection, autolim=False)
54525460
self.update_datalim(corners)
54535461
self.autoscale_view()
5454-
self.add_collection(collection, autolim=False)
54555462
return collection
54565463

54575464
@unpack_labeled_data(label_namer=None)
@@ -5601,7 +5608,8 @@ def pcolorfast(self, *args, **kwargs):
56015608
# The QuadMesh class can also be changed to
56025609
# handle relevant superclass kwargs; the initializer
56035610
# should do much more than it does now.
5604-
collection = mcoll.QuadMesh(nc, nr, coords, 0, edgecolors="None")
5611+
collection = mcoll.QuadMesh(nc, nr, coords, 0, edgecolors="None",
5612+
margins=False)
56055613
collection.set_alpha(alpha)
56065614
collection.set_array(C)
56075615
collection.set_cmap(cmap)
@@ -5647,15 +5655,19 @@ def contour(self, *args, **kwargs):
56475655
if not self._hold:
56485656
self.cla()
56495657
kwargs['filled'] = False
5650-
return mcontour.QuadContourSet(self, *args, **kwargs)
5658+
contours = mcontour.QuadContourSet(self, *args, **kwargs)
5659+
self.autoscale_view()
5660+
return contours
56515661
contour.__doc__ = mcontour.QuadContourSet.contour_doc
56525662

56535663
@unpack_labeled_data()
56545664
def contourf(self, *args, **kwargs):
56555665
if not self._hold:
56565666
self.cla()
56575667
kwargs['filled'] = True
5658-
return mcontour.QuadContourSet(self, *args, **kwargs)
5668+
contours = mcontour.QuadContourSet(self, *args, **kwargs)
5669+
self.autoscale_view()
5670+
return contours
56595671
contourf.__doc__ = mcontour.QuadContourSet.contour_doc
56605672

56615673
def clabel(self, CS, *args, **kwargs):
@@ -6035,6 +6047,11 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
60356047
else:
60366048
n = [m[slc].cumsum()[slc] for m in n]
60376049

6050+
if orientation == 'horizontal':
6051+
margins = {'left': False}
6052+
else:
6053+
margins = {'bottom': False}
6054+
60386055
patches = []
60396056

60406057
if histtype.startswith('bar'):
@@ -6175,14 +6192,16 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
61756192
patches.append(self.fill(
61766193
x, y,
61776194
closed=True,
6178-
facecolor=c))
6195+
facecolor=c,
6196+
margins=margins))
61796197
else:
61806198
for x, y, c in reversed(list(zip(xvals, yvals, color))):
61816199
split = 2 * len(bins)
61826200
patches.append(self.fill(
61836201
x[:split], y[:split],
61846202
closed=False, edgecolor=c,
6185-
fill=False))
6203+
fill=False,
6204+
margins=margins))
61866205

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

0 commit comments

Comments
 (0)