8000 add ax.use_sticky_edges property · matplotlib/matplotlib@04298b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04298b8

Browse files
committed
add ax.use_sticky_edges property
1 parent 1a86b08 commit 04298b8

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

examples/pylab_examples/trigradient_demo.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,22 @@ def dipole_potential(x, y):
6464
#-----------------------------------------------------------------------------
6565
# Plot the triangulation, the potential iso-contours and the vector field
6666
#-----------------------------------------------------------------------------
67-
plt.figure()
68-
plt.gca().set_aspect('equal')
69-
plt.triplot(triang, color='0.8')
67+
fig, ax = plt.subplots()
68+
ax.set_aspect('equal')
69+
# Enforce the margins, and enlarge them to give room for the vectors.
70+
ax.use_sticky_edges = False
71+
ax.margins(0.07)
72+
73+
ax.triplot(triang, color='0.8')
7074

7175
levels = np.arange(0., 1., 0.01)
7276
cmap = cm.get_cmap(name='hot', lut=None)
73-
plt.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
74-
linewidths=[2.0, 1.0, 1.0, 1.0])
77+
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
78+
linewidths=[2.0, 1.0, 1.0, 1.0])
7579
# Plots direction of the electrical vector field
76-
plt.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
77-
units='xy', scale=10., zorder=3, color='blue',
78-
width=0.007, headwidth=3., headlength=4.)
80+
ax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
81+
units='xy', scale=10., zorder=3, color='blue',
82+
width=0.007, headwidth=3., headlength=4.)
7983

80-
plt.title('Gradient plot: an electrical dipole')
84+
ax.set_title('Gradient plot: an electrical dipole')
8185
plt.show()

lib/matplotlib/axes/_base.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ def cla(self):
10301030
self._xmargin = rcParams['axes.xmargin']
10311031
self._ymargin = rcParams['axes.ymargin']
10321032
self._tight = None
1033+
self._use_sticky_edges = True
10331034
self._update_transScale() # needed?
10341035

10351036
self._get_lines = _process_plot_var_args(self)
@@ -2034,6 +2035,28 @@ def set_autoscaley_on(self, b):
20342035
"""
20352036
self._autoscaleYon = b
20362037

2038+
@property
2039+
def use_sticky_edges(self):
2040+
"""
2041+
When autoscaling, whether to obey all `Artist.sticky_edges`.
2042+
2043+
Default is ``True``.
2044+
2045+
Setting this to ``False`` ensures that the specified margins
2046+
will be applied, even if the plot includes an image, for
2047+
example, which would otherwise force a view limit to coincide
2048+
with its data limit.
2049+
2050+
The changing this property does not change the plot until
2051+
`autoscale` or `autoscale_view` is called.
2052+
"""
2053+
return self._use_sticky_edges
2054+
2055+
@use_sticky_edges.setter
2056+
def use_sticky_edges(self, b):
2057+
self._use_sticky_edges = bool(b)
2058+
self.stale = True
2059+
20372060
def set_xmargin(self, m):
20382061
"""
20392062
Set padding of X data limits prior to autoscaling.
@@ -2202,7 +2225,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
22022225
if tight is not None:
22032226
self._tight = bool(tight)
22042227

2205-
if self._xmargin or self._ymargin:
2228+
if self.use_sticky_edges and (self._xmargin or self._ymargin):
22062229
stickies = [artist.sticky_edges for artist in self.get_children()]
22072230
x_stickies = sum([sticky.x for sticky in stickies], [])
22082231
y_stickies = sum([sticky.y for sticky in stickies], [])

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ def test_autoscale_tight():
174174
assert_allclose(ax.get_xlim(), (-0.15, 3.15))
175175
assert_allclose(ax.get_ylim(), (1.0, 4.0))
176176

177+
@cleanup(style='default')
178+
def test_use_sticky_edges():
179+
fig, ax = plt.subplots()
180+
ax.imshow([[0, 1], [2, 3]], origin='lower')
181+
assert_allclose(ax.get_xlim(), (-0.5, 1.5))
182+
assert_allclose(ax.get_ylim(), (-0.5, 1.5))
< 579F /td>183+
ax.use_sticky_edges = False
184+
ax.autoscale()
185+
xlim = (-0.5 - 2 * ax._xmargin, 1.5 + 2 * ax._xmargin)
186+
ylim = (-0.5 - 2 * ax._ymargin, 1.5 + 2 * ax._ymargin)
187+
assert_allclose(ax.get_xlim(), xlim)
188+
assert_allclose(ax.get_ylim(), ylim)
189+
# Make sure it is reversible:
190+
ax.use_sticky_edges = True
191+
ax.autoscale()
192+
assert_allclose(ax.get_xlim(), (-0.5, 1.5))
193+
assert_allclose(ax.get_ylim(), (-0.5, 1.5))
177194

178195
@image_comparison(baseline_images=['offset_points'],
179196
remove_text=True)
8107

lib/matplotlib/tests/test_triangulation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ def dipole_potential(x, y):
823823
plt.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
824824
units='xy', scale=10., zorder=3, color='blue',
825825
width=0.007, headwidth=3., headlength=4.)
826+
# We are leaving ax.use_sticky_margins as True, so the
827+
# view limits are the contour data limits.
826828

827829

828830
def test_tritools():

0 commit comments

Comments
 (0)
0