I'm plotting points on a log-log scatterplot with matplotlib
. The x
coordinate of these points rises twice as fast (exponentially speaking) than the y
coordinate, which means that the x
axis is twice as densely packed when plotted on a square plot.
Here's the code and the graph it produces:
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
fig, ax = plt.subplots(figsize=(10,8))
ax: plt.Axes
# Log scale
ax.set_xscale("log") # Needed for a log scatterplot. https://stackoverflow.com/a/52573929/9352077
ax.set_yscale("log")
# ax.xaxis.set_major_locator(tkr.LogLocator(base=10))
# ax.xaxis.set_major_formatter(tkr.LogFormatterSciNotation())
# ax.yaxis.set_major_locator(tkr.LogLocator(base=10))
# ax.yaxis.set_major_formatter(tkr.LogFormatterSciNotation())
# Plot points
t = range(1, 10_000_000, 1000)
x = [e**2 for e in t]
y = t
ax.scatter(x, y, marker=".", linewidths=0.05)
# Grid
ax.set_axisbelow(True)
ax.grid(True)
fig.savefig("./test.pdf", bbox_inches='tight')
By default, matplotlib seems to want a square grid, and hence skips every other power of 10 on the x
axis. Supposedly, the 4 lines I have commented out -- a combination of a locator and a formatter -- are the way to customise the spacing of the major ticks. See e.g. this post. Yet, uncommenting them produces the same exact figure, with missing major ticks.
How else, if not with LogLocator(base=10)
, can I change the spacing between the ticks on the log scale? Here's roughly what it should look like (I've highlighted the changes in red, but they should of course be gray):