Search code examples
pythonmatplotlibplotcustomizationaxis-labels

How to put minor ticks in completely custom locations in matplotlib?


Say I want to add minor ticks to my x axis at custom locations equivalent to this command for major ticks: plt.xticks([1,2,3]).

I have tried:

import matplotlib.pyplot as plt
x = [1,2,3]
y = [1,4,9]
plt.scatter(x,y)
ax = plt.gca()
ax.xaxis.set_minor_locator([1.1, 1.9, 2.5])

but that throws an error because the last command probably needs a ticker object, not a list in the argument. Any pythonic way to go about this?


Solution

  • You just need to add the following line and turn on the minor flag to True

    ax.set_xticks([1.1, 1.9, 2.5], minor=True)
    

    enter image description here