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

Skip to content

Commit 7cea22c

Browse files
committed
Merge pull request #5772 from tacaswell/min_nticks
FIX: always use at least 2 ticks and recompute
2 parents d8ebc39 + bc359be commit 7cea22c

6 files changed

Lines changed: 37 additions & 23 deletions

File tree

examples/axes_grid/inset_locator_demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def add_sizebar(ax, size):
3232
ax2.set_aspect(1.)
3333

3434
axins = zoomed_inset_axes(ax2, 0.5, loc=1) # zoom = 0.5
35+
# fix the number of ticks on the inset axes
36+
axins.yaxis.get_major_locator().set_params(nbins=7)
37+
axins.xaxis.get_major_locator().set_params(nbins=7)
3538

3639
plt.xticks(visible=False)
3740
plt.yticks(visible=False)

examples/axes_grid/inset_locator_demo2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def get_demo_image():
3434
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
3535
axins.set_xlim(x1, x2)
3636
axins.set_ylim(y1, y2)
37+
# fix the number of ticks on the inset axes
38+
axins.yaxis.get_major_locator().set_params(nbins=7)
39+
axins.xaxis.get_major_locator().set_params(nbins=7)
3740

3841
plt.xticks(visible=False)
3942
plt.yticks(visible=False)

lib/matplotlib/axis.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,6 @@ def __init__(self, axes, pickradius=15):
664664
# Initialize here for testing; later add API
665665
self._major_tick_kw = dict()
666666
self._minor_tick_kw = dict()
667-
self._tick_space = None
668667

669668
self.cla()
670669
self._set_scale('linear')
@@ -796,7 +795,6 @@ def set_tick_params(self, which='major', reset=False, **kw):
796795
for tick in self.minorTicks:
797796
tick._apply_params(**self._minor_tick_kw)
798797
self.stale = True
799-
self._tick_space = None
800798

801799
@staticmethod
802800
def _translate_tick_kw(kw, to_init_kw=True):
@@ -1996,16 +1994,14 @@ def set_default_intervals(self):
19961994
self.stale = True
19971995

19981996
def get_tick_space(self):
1999-
if self._tick_space is None:
2000-
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
2001-
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
2002-
tick = self._get_tick(True)
2003-
# There is a heuristic here that the aspect ratio of tick text
2004-
# is no more than 3:1
2005-
size = tick.label1.get_size() * 3
2006-
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2007-
self._tick_space = np.floor(length / size)
2008-
return self._tick_space
1997+
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
1998+
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
1999+
tick = self._get_tick(True)
2000+
# There is a heuristic here that the aspect ratio of tick text
2001+
# is no more than 3:1
2002+
size = tick.label1.get_size() * 3
2003+
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2004+
return np.floor(length / size)
20092005

20102006

20112007
class YAxis(Axis):
@@ -2339,12 +2335,10 @@ def set_default_intervals(self):
23392335
self.stale = True
23402336

23412337
def get_tick_space(self):
2342-
if self._tick_space is None:
2343-
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2344-
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72.0
2345-
tick = self._get_tick(True)
2346-
# Having a spacing of at least 2 just looks good.
2347-
size = tick.label1.get_size() * 2.0
2348-
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2349-
self._tick_space = np.floor(length / size)
2350-
return self._tick_space
2338+
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2339+
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72.0
2340+
tick = self._get_tick(True)
2341+
# Having a spacing of at least 2 just looks good.
2342+
size = tick.label1.get_size() * 2.0
2343+
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
2344+
return np.floor(length / size)

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4276,6 +4276,19 @@ def _helper_y(ax):
42764276
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)
42774277

42784278

4279+
@cleanup
4280+
def test_adjust_numtick_aspect():
4281+
fig, ax = plt.subplots()
4282+
ax.yaxis.get_major_locator().set_params(nbins='auto')
4283+
ax.set_xlim(0, 1000)
4284+
ax.set_aspect('equal')
4285+
fig.canvas.draw()
4286+
assert len(ax.yaxis.get_major_locator()()) == 2
4287+
ax.set_ylim(0, 1000)
4288+
fig.canvas.draw()
4289+
assert len(ax.yaxis.get_major_locator()()) > 2
4290+
4291+
42794292
@image_comparison(baseline_images=["auto_numticks"], style='default',
42804293
extensions=['png'])
42814294
def test_auto_numticks():

lib/matplotlib/ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ def set_params(self, **kwargs):
14341434
def bin_boundaries(self, vmin, vmax):
14351435
nbins = self._nbins
14361436
if nbins == 'auto':
1437-
nbins = min(self.axis.get_tick_space(), 9)
1437+
nbins = max(min(self.axis.get_tick_space(), 9), 1)
14381438
scale, offset = scale_range(vmin, vmax, nbins)
14391439
if self._integer:
14401440
scale = max(1, scale)

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def get_demo_image():
101101
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6
102102
axins.imshow(Z2, extent=extent, interpolation="nearest",
103103
origin="lower")
104-
104+
axins.yaxis.get_major_locator().set_params(nbins=7)
105+
axins.xaxis.get_major_locator().set_params(nbins=7)
105106
# sub region of the original image
106107
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
107108
axins.set_xlim(x1, x2)

0 commit comments

Comments
 (0)