Home > database >  Matplotlib log log plot not displaying all major and minor ticks when using loglocator and different
Matplotlib log log plot not displaying all major and minor ticks when using loglocator and different

Time:09-24

I am making a log log plot and using matplotlib. I am using loglocator to make sure the relevant major and minor ticks are displayed. However, I noticed that when the x and y limits are different, some ticks and tick labels and missing: (see figure)

Below there is a simple example that produces this undesired behaviour. Any help would be greatly appreciated.

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

Npoints=100
xs=np.logspace(-8,0, 100)
ys=xs

fig=plt.figure(figsize=(4,3))
ax=fig.add_subplot(111)
ax.plot(xs, ys)
ax.set_xscale('log')
ax.set_yscale('log')

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=100) 
locmin = matplotlib.ticker.LogLocator(base=10,subs=np.arange(2, 10) * .1,numticks=100) # subs=(0.2,0.4,0.6,0.8)

ax.yaxis.set_major_locator(locmaj)
ax.yaxis.set_minor_locator(locmin)
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax.xaxis.set_major_locator(locmaj)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax.set_xlim(1.0e-3, 1.0)
ax.set_ylim(1.0e-8, 1.0)

plt.show()

CodePudding user response:

In general, Locator instances should not be reused on different axes. From the enter image description here

  • Related