Reputation: 43
In matplot lib, I want to space out the grids in a linearly spaced order, while the axis scale is logarithmic.
The code so far is this:
ax.grid(True, which="major", ls="-")
ax.set_yscale('log')
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.8f}'))
ax.yaxis.set_minor_formatter(StrMethodFormatter('{x:.8f}'))
Upvotes: 0
Views: 657
Reputation: 80339
You can explicitly set the major ticks at the desired positions. Either simply ticks=[2**i for i in range(30]
or the more elaborate ticks from the example (these will be close to being evenly-spaced, but not fully). Optionally, the numbers can be displayed with a thousands-separator. The minor ticks that are automatically added by the log scale look very confusing and can be removed.
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.ticker import FixedLocator, StrMethodFormatter, NullFormatter, NullLocator
fig, ax = plt.subplots()
ax.plot(np.exp(10+np.random.normal(0.01, 0.6, 1000).cumsum()))
ax.grid(True, which="major", ls="-")
ax.set_yscale('log')
ticks = [j * k for j in (1, 1000, 1000000) for k in [3, 6, 12] + [25*2**i for i in range(7)] ]
ax.yaxis.set_major_locator(FixedLocator(ticks))
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}'))
ax.yaxis.set_minor_locator(NullLocator())
#ax.yaxis.set_minor_formatter(NullFormatter())
ax.set_ylim(ymin=2)
plt.tight_layout()
plt.show()
Upvotes: 1