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

Skip to content

Commit 55e39ef

Browse files
author
Akshay Nair
committed
Share viewLim instead of calling set_ticks
1 parent fda65b4 commit 55e39ef

File tree

2 files changed

+128
-30
lines changed

2 files changed

+128
-30
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 118 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,67 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
31433143
self.stale = True
31443144
return left, right
31453145

3146+
def _set_xviewlim(self, left=None, right=None, emit=True):
3147+
"""
3148+
Set the view limits for the x-axis directly
3149+
3150+
.. ACCEPTS: (left: float, right: float)
3151+
3152+
Parameters
3153+
----------
3154+
left : scalar, optional
3155+
The left xlim (default: None, which leaves the left limit
3156+
unchanged).
3157+
3158+
right : scalar, optional
3159+
The right xlim (default: None, which leaves the right limit
3160+
unchanged).
3161+
3162+
emit : bool, optional
3163+
Whether to notify observers of limit change (default: True).
3164+
3165+
xlimits : tuple, optional
3166+
The left and right xlims may be passed as the tuple
3167+
(`left`, `right`) as the first positional argument (or as
3168+
the `left` keyword argument).
3169+
3170+
Returns
3171+
-------
3172+
xlimits : tuple
3173+
Returns the new x-axis view limits as (`left`, `right`).
3174+
3175+
Notes
3176+
-----
3177+
The `left` value may be greater than the `right` value, in which
3178+
case the x-axis values will decrease from left to right.
3179+
3180+
Examples
3181+
--------
3182+
>>> _set_xviewlim(left, right)
3183+
>>> _set_xviewlim((left, right))
3184+
>>> left, right = _set_xviewlim(left, right)
3185+
3186+
"""
3187+
if right is None and iterable(left):
3188+
left, right = left
3189+
if left is None or right is None:
3190+
raise ValueError('Invalid view limits provided: %s, %s',
3191+
left, right)
3192+
3193+
self.viewLim.intervalx = (left, right)
3194+
3195+
if emit:
3196+
self.callbacks.process('xlim_changed', self)
3197+
# Call all of the other x-axes that are shared with this one
3198+
for other in self._shared_x_axes.get_siblings(self):
3199+
if other is not self:
3200+
other._set_xviewlim(self.viewLim.intervalx, emit=False)
3201+
if (other.figure != self.figure and
3202+
other.figure.canvas is not None):
3203+
other.figure.canvas.draw_idle()
3204+
self.stale = True
3205+
return left, right
3206+
31463207
def get_xscale(self):
31473208
return self.xaxis.get_scale()
31483209
get_xscale.__doc__ = "Return the xaxis scale string: %s""" % (
@@ -3203,12 +3264,6 @@ def set_xticks(self, ticks, minor=False):
32033264
"""
32043265
ret = self.xaxis.set_ticks(ticks, minor=minor)
32053266
self.stale = True
3206-
3207-
g = self.get_shared_x_axes()
3208-
for ax in g.get_siblings(self):
3209-
ax.xaxis.set_ticks(ticks, minor=minor)
3210-
ax.stale = True
3211-
32123267
return ret
32133268

32143269
def get_xmajorticklabels(self):
@@ -3296,12 +3351,6 @@ def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
32963351
ret = self.xaxis.set_ticklabels(labels,
32973352
minor=minor, **kwargs)
32983353
self.stale = True
3299-
3300-
g = self.get_shared_x_axes()
3301-
for ax in g.get_siblings(self):
3302-
ax.xaxis.set_ticklabels(labels, minor=minor, **kwargs)
3303-
ax.stale = True
3304-
33053354
return ret
33063355

33073356
def invert_yaxis(self):
@@ -3475,6 +3524,62 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
34753524
self.stale = True
34763525
return bottom, top
34773526

3527+
def _set_yviewlim(self, bottom=None, top=None, emit=True):
3528+
"""
3529+
Set the view limits for the y-axis directly
3530+
3531+
.. ACCEPTS: (bottom: float, top: float)
3532+
3533+
Parameters
3534+
----------
3535+
bottom : scalar, optional
3536+
The bottom ylim (default: None, which leaves the bottom
3537+
limit unchanged).
3538+
3539+
top : scalar, optional
3540+
The top ylim (default: None, which leaves the top limit
3541+
unchanged).
3542+
3543+
emit : bool, optional
3544+
Whether to notify observers of limit change (default: True).
3545+
3546+
ylimits : tuple, optional
3547+
The bottom and top yxlims may be passed as the tuple
3548+
(`bottom`, `top`) as the first positional argument (or as
3549+
the `bottom` keyword argument).
3550+
3551+
Returns
3552+
-------
3553+
ylimits : tuple
3554+
Returns the new y-axis limits as (`bottom`, `top`).
3555+
3556+
Examples
3557+
--------
3558+
>>> _set_yviewlim(bottom, top)
3559+
>>> _set_yviewlim((bottom, top))
3560+
>>> bottom, top = _set_yviewlim(bottom, top)
3561+
"""
3562+
3563+
if top is None and iterable(bottom):
3564+
bottom, top = bottom
3565+
if top is None or bottom is None:
3566+
raise ValueError('Invalid view limits provided: %s, %s',
3567+
bottom, top)
3568+
3569+
self.viewLim.intervaly = (bottom, top)
3570+
3571+
if emit:
3572+
self.callbacks.process('ylim_changed', self)
3573+
# Call all of the other y-axes that are shared with this one
3574+
for other in self._shared_y_axes.get_siblings(self):
3575+
if other is not self:
3576+
other._set_yviewlim(self.viewLim.intervaly, emit=False)
3577+
if (other.figure != self.figure and
3578+
other.figure.canvas is not None):
3579+
other.figure.canvas.draw_idle()
3580+
self.stale = True
3581+
return bottom, top
3582+
34783583
def get_yscale(self):
34793584
return self.yaxis.get_scale()
34803585
get_yscale.__doc__ = "Return the yaxis scale string: %s""" % (
@@ -3533,9 +3638,6 @@ def set_yticks(self, ticks, minor=False):
35333638
Default is ``False``.
35343639
"""
35353640
ret = self.yaxis.set_ticks(ticks, minor=minor)
3536-
g = self.get_shared_y_axes()
3537-
for ax in g.get_siblings(self):
3538-
ax.yaxis.set_ticks(ticks, minor=minor)
35393641
return ret
35403642

35413643
def get_ymajorticklabels(self):
@@ -3620,12 +3722,8 @@ def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
36203722
"""
36213723
if fontdict is not None:
36223724
kwargs.update(fontdict)
3623-
ret = self.yaxis.set_ticklabels(labels,
3725+
return self.yaxis.set_ticklabels(labels,
36243726
minor=minor, **kwargs)
3625-
g = self.get_shared_y_axes()
3626-
for ax in g.get_siblings(self):
3627-
ax.yaxis.set_ticklabels(labels, minor=minor, **kwargs)
3628-
return ret
36293727

36303728
def xaxis_date(self, tz=None):
36313729
"""

lib/matplotlib/axis.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,15 +2070,15 @@ def set_view_interval(self, vmin, vmax, ignore=False):
20702070
20712071
"""
20722072
if ignore:
2073-
self.axes.viewLim.intervalx = vmin, vmax
2073+
self.axes._set_xviewlim(vmin, vmax)
20742074
else:
20752075
Vmin, Vmax = self.get_view_interval()
20762076
if Vmin < Vmax:
2077-
self.axes.viewLim.intervalx = (min(vmin, vmax, Vmin),
2078-
max(vmin, vmax, Vmax))
2077+
self.axes._set_xviewlim(min(vmin, vmax, Vmin),
2078+
max(vmin, vmax, Vmax))
20792079
else:
2080-
self.axes.viewLim.intervalx = (max(vmin, vmax, Vmin),
2081-
min(vmin, vmax, Vmax))
2080+
self.axes._set_xviewlim(max(vmin, vmax, Vmin),
2081+
min(vmin, vmax, Vmax))
20822082

20832083
def get_minpos(self):
20842084
return self.axes.dataLim.minposx
@@ -2448,15 +2448,15 @@ def set_view_interval(self, vmin, vmax, ignore=False):
24482448
24492449
"""
24502450
if ignore:
2451-
self.axes.viewLim.intervaly = vmin, vmax
2451+
self.axes._set_yviewlim(vmin, vmax)
24522452
else:
24532453
Vmin, Vmax = self.get_view_interval()
24542454
if Vmin < Vmax:
2455-
self.axes.viewLim.intervaly = (min(vmin, vmax, Vmin),
2456-
max(vmin, vmax, Vmax))
2455+
self.axes._set_yviewlim(min(vmin, vmax, Vmin),
2456+
max(vmin, vmax, Vmax))
24572457
else:
2458-
self.axes.viewLim.intervaly = (max(vmin, vmax, Vmin),
2459-
min(vmin, vmax, Vmax))
2458+
self.axes._set_yviewlim(max(vmin, vmax, Vmin),
2459+
min(vmin, vmax, Vmax))
24602460
self.stale = True
24612461

24622462
def get_minpos(self):

0 commit comments

Comments
 (0)