108

How can I turn the minor ticks only on y axis on a linear vs linear plot?

When I use the function minor_ticks_on to turn minor ticks on, they appear on both x and y axis.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
emad
  • 2,869
  • 3
  • 18
  • 18

6 Answers6

76

Nevermind, I figured it out.

ax.tick_params(axis='x', which='minor', bottom=False)
a_guest
  • 34,165
  • 12
  • 64
  • 118
emad
  • 2,869
  • 3
  • 18
  • 18
  • 6
    Using this I get a `MatplotlibDeprecationWarning` for using `bottom='off'`. Apparently `ax.tick_params(axis='x',which='minor',bottom=False)` should be used instead. – Thomas Kühn Aug 20 '18 at 06:26
  • I still had the problem that the minor tick labels were showing. You can turn them off with `labelbottom=False`. – M. Steiner Jan 03 '23 at 10:05
34

Here's another way I found in the matplotlib documentation:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator

a = np.arange(100)
ml = MultipleLocator(5)
plt.plot(a)
plt.axes().yaxis.set_minor_locator(ml)
plt.show()

This will place minor ticks on only the y-axis, since minor ticks are off by default.

John Vinyard
  • 12,997
  • 3
  • 30
  • 43
  • 3
    I would recommend using `plt.axes().yaxis.set_minor_locator(MultipleLocator(5))` instead of first initializing `ml`. If you reuse `ml` this can lead to horrible mistakes which are hard to find. Also the more natural choice could be `AutoMinorLocator` unless something specific is requested. – DerWeh Jul 24 '18 at 14:37
22

To clarify the procedure of @emad's answer, the steps to show minor ticks at default locations are:

  1. Turn on minor ticks for an axes object, so locations are initialized as Matplotlib sees fit.
  2. Turn off minor ticks that are not desired.

A minimal example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
plt.plot([1,2])

# Currently, there are no minor ticks,
#   so trying to make them visible would have no effect
ax.yaxis.get_ticklocs(minor=True)     # []

# Initialize minor ticks
ax.minorticks_on()

# Now minor ticks exist and are turned on for both axes

# Turn off x-axis minor ticks
ax.xaxis.set_tick_params(which='minor', bottom=False)

Alternative Method

Alternatively, we can get minor ticks at default locations using AutoMinorLocator:

import matplotlib.pyplot as plt
import matplotlib.ticker as tck

fig, ax = plt.subplots()
plt.plot([1,2])

ax.yaxis.set_minor_locator(tck.AutoMinorLocator())

Result

Either way, the resulting plot has minor ticks on the y-axis only.

plot with minor ticks on y-axis only

SpinUp __ A Davis
  • 5,016
  • 1
  • 30
  • 25
21

To set minor ticks at custom locations:

ax.set_xticks([0, 10, 20, 30], minor=True)
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
5

Also, if you only want minor ticks on the actual y-axis, rather than on both the left and right-hand sides of the graph, you can follow the plt.axes().yaxis.set_minor_locator(ml) with plt.axes().yaxis.set_tick_params(which='minor', right = 'off'), like so:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator

a = np.arange(100)
ml = MultipleLocator(5)
plt.plot(a)
plt.axes().yaxis.set_minor_locator(ml)
plt.axes().yaxis.set_tick_params(which='minor', right = 'off')
plt.show()
marisano
  • 571
  • 6
  • 10
3

The following snippets should help:

from matplotlib.ticker import MultipleLocator
ax.xaxis.set_minor_locator(MultipleLocator(#))
ax.yaxis.set_minor_locator(MultipleLocator(#))

# refers to the desired interval between minor ticks.