When I turn on minor ticks in a plot with something like plt.minorticks_on()
, I often want to have a larger number of minor ticks.
Is there a simple method to achieve that?
When I turn on minor ticks in a plot with something like plt.minorticks_on()
, I often want to have a larger number of minor ticks.
Is there a simple method to achieve that?
I found a good answer to my question, so in order to be able to find it more quickly next time, I'm including it here:
To have minor ticks every 10 and major ticks every 100 on the x-axis:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
plt.plot(my_data)
plt.minorticks_on()
ax = plt.gca()
ax.xaxis.set_major_locator(MultipleLocator(100)) # major ticks every 100 (optional)
ax.xaxis.set_minor_locator(MultipleLocator(10)) # minor ticks every 10
In my original plot, ticks defaulted to 100 for major ticks and 20 for minor (5 minor for every major). With this code I get 10 minor ticks for every major.
This is not quite what I was after, but makes it easy enough to get the desired effect.