4

Firstly, this question is directly related to Matplotlib semi-log plot: minor tick marks are gone when range is large.
I am trying to get the same result on the y-axis for values in the range of 1E-15 to 1E-4.
I do not have enough reputation to comment in the referenced question yet - sorry for doubling!

Testsetup: I use Jupyter 4.3.0 with matplotlib 2.0.2 and numpy 1.13.1

The following code (from referenced question) does not show minor ticks on x-axis on my Jupyter installations. I need y-axis but should be same procedure.

Thanks for any input pushing me in the right direction.

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

#Used this to reset any changes to the rc params
#plt.rcParams.update(plt.rcParamsDefault)

x = np.arange(10) # np.arange(9) is working as expected - well documented issue with >10 range for ticker.LocLocator
y = 10.0**x

fig, ax=plt.subplots()
ax.plot(y,x)
ax.set_xscale("log")

locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, ))
ax.xaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10 )) 
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

Resulting Plot -

enter image description here

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90

1 Answers1

5

The parameter that needs to be change ind numticks which limits the number of ticks that will be emitted.

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

#Used this to reset any changes to the rc params
#plt.rcParams.update(plt.rcParamsDefault)

x = np.arange(10) 
y = 10.0**x

fig, ax=plt.subplots()
ax.plot(y,x)
ax.set_xscale("log")

locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(1.0, ), numticks=100)
ax.xaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0, subs=np.arange(2, 10) * .1,
                                      numticks=100)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.show()

dense ticks

Once the Locator decides to start skipping decades, if subs is not (1,) all ticks are suppressed to prevent some very confusing tick labels.

Also not that the ticks will be at [s * base ** i for s in subs for i in range(min_decade, max_decade, decade_stride] so putting values which are different only by multiples of base in subs just re-ticks the same points.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • I updated my original anwer for [the linked question](https://stackoverflow.com/questions/44078409/matplotlib-semi-log-plot-minor-tick-marks-are-gone-when-range-is-large). – ImportanceOfBeingErnest Aug 27 '17 at 17:57