1

In matplotlib (version 3.2.2), I want to put minor ticks on a plot. However, when the axis limits are multiples of the minor tick stepsize, the first and sometimes last minor ticks are not plotted. Below is a minimal example with output plots. I assume that ax.minorticks_on() yields the same result (for the x-axis) as ax.xaxis.set_minor_locator(AutoMinorLocator()).

I would have expected that minor ticks at -1.9 and 1.9 as well as at -5 are shown, given the axis limits of [-1.9, 1.9] and [-5, 5] and minor tick stepsizes of 0.1 and 0.5, respectively.

My guess is that this could be a floating point issue, as the tick locations appear to not be exact when printed using ax.xaxis.get_ticklocs(minor=True). Or is this an intentional behavior and if so, why (to me it makes sense to put minor ticks at the beginning and end for the shown cases)? Is there a way to display the ticks while keeping the AutoMinorLocator() (the minor ticks appear for example when setting the minor tick locator to MultipleLocator(0.1) and MultipleLocator(0.5), respectively)?

Update: Issued a bug report on the matplotlib github page.


Minimal working example (MWE):

import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

plt.ion()

fig = plt.figure()
ax = plt.gca()
ax.set_xlim(-1.9,1.9)
ax.minorticks_on()
ax.xaxis.set_minor_locator(AutoMinorLocator())
print(ax.xaxis.get_ticklocs(minor=True))
# [-1.8, -1.7, -1.5999999999999999, -1.4, -1.2999999999999998, -1.1999999999999997, -1.0999999999999999, -0.8999999999999997, -0.7999999999999996, -0.6999999999999997, -0.5999999999999996, -0.3999999999999997, -0.2999999999999996, -0.1999999999999995, -0.09999999999999942, 0.10000000000000053, 0.20000000000000107, 0.3000000000000007, 0.4000000000000008, 0.600000000000001, 0.7000000000000011, 0.8000000000000012, 0.9000000000000012, 1.100000000000001, 1.200000000000001, 1.3000000000000012, 1.4000000000000012, 1.6000000000000014, 1.7000000000000015, 1.8000000000000016]
fig.savefig('~/so_minor_ticks_first_last_1.png')
ax.set_xlim(-5,5)
print(ax.xaxis.get_ticklocs(minor=True))
# [-4.5, -3.5, -3.0, -2.5, -1.5, -1.0, -0.5, 0.5, 1.0, 1.5, 2.5, 3.0, 3.5, 4.5, 5.0]
fig.savefig('~/so_minor_ticks_first_last_2.png')
plt.close()

no first and last minor ticks no first minor tick

bproxauf
  • 1,076
  • 12
  • 23
  • 1
    Did you try `ax.set_xlim(-1.90001,1.90001)`? – JohanC Jan 26 '22 at 11:11
  • @JohanC: Yes, the ticks appear. But suppose you automatically determine some integer `xlims`. To me it seems unnecessarily complicated to by default have to add some epsilon amount to avoid floating issues and get the ticks to appear... – bproxauf Jan 26 '22 at 12:54

0 Answers0