Skip to content

FIX: always use at least 2 ticks and recompute #5772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/axes_grid/inset_locator_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def add_sizebar(ax, size):
ax2.set_aspect(1.)

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

plt.xticks(visible=False)
plt.yticks(visible=False)
Expand Down
3 changes: 3 additions & 0 deletions examples/axes_grid/inset_locator_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def get_demo_image():
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
# fix the number of ticks on the inset axes
axins.yaxis.get_major_locator().set_params(nbins=7)
axins.xaxis.get_major_locator().set_params(nbins=7)

plt.xticks(visible=False)
plt.yticks(visible=False)
Expand Down
36 changes: 15 additions & 21 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ def __init__(self, axes, pickradius=15):
# Initialize here for testing; later add API
self._major_tick_kw = dict()
self._minor_tick_kw = dict()
self._tick_space = None

self.cla()
self._set_scale('linear')
Expand Down Expand Up @@ -796,7 +795,6 @@ def set_tick_params(self, which='major', reset=False, **kw):
for tick in self.minorTicks:
tick._apply_params(**self._minor_tick_kw)
self.stale = True
self._tick_space = None

@staticmethod
def _translate_tick_kw(kw, to_init_kw=True):
Expand Down Expand Up @@ -1996,16 +1994,14 @@ def set_default_intervals(self):
self.stale = True

def get_tick_space(self):
if self._tick_space is None:
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
tick = self._get_tick(True)
# There is a heuristic here that the aspect ratio of tick text
# is no more than 3:1
size = tick.label1.get_size() * 3
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
self._tick_space = np.floor(length / size)
return self._tick_space
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
tick = self._get_tick(True)
# There is a heuristic here that the aspect ratio of tick text
# is no more than 3:1
size = tick.label1.get_size() * 3
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
return np.floor(length / size)


class YAxis(Axis):
Expand Down Expand Up @@ -2339,12 +2335,10 @@ def set_default_intervals(self):
self.stale = True

def get_tick_space(self):
if self._tick_space is None:
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72.0
tick = self._get_tick(True)
# Having a spacing of at least 2 just looks good.
size = tick.label1.get_size() * 2.0
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
self._tick_space = np.floor(length / size)
return self._tick_space
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72.0
tick = self._get_tick(True)
# Having a spacing of at least 2 just looks good.
size = tick.label1.get_size() * 2.0
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
return np.floor(length / size)
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4276,6 +4276,19 @@ def _helper_y(ax):
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)


@cleanup
def test_adjust_numtick_aspect():
fig, ax = plt.subplots()
ax.yaxis.get_major_locator().set_params(nbins='auto')
ax.set_xlim(0, 1000)
ax.set_aspect('equal')
fig.canvas.draw()
assert len(ax.yaxis.get_major_locator()()) == 2
ax.set_ylim(0, 1000)
fig.canvas.draw()
assert len(ax.yaxis.get_major_locator()()) > 2


@image_comparison(baseline_images=["auto_numticks"], style='default',
extensions=['png'])
def test_auto_numticks():
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ def set_params(self, **kwargs):
def bin_boundaries(self, vmin, vmax):
nbins = self._nbins
if nbins == 'auto':
nbins = min(self.axis.get_tick_space(), 9)
nbins = max(min(self.axis.get_tick_space(), 9), 1)
scale, offset = scale_range(vmin, vmax, nbins)
if self._integer:
scale = max(1, scale)
Expand Down
3 changes: 2 additions & 1 deletion lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def get_demo_image():
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6
axins.imshow(Z2, extent=extent, interpolation="nearest",
origin="lower")

axins.yaxis.get_major_locator().set_params(nbins=7)
axins.xaxis.get_major_locator().set_params(nbins=7)
# sub region of the original image
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
Expand Down