Matplotlib tick_params(): Customize Ticks and Tick Labels

tick_params() changes how ticks, tick labels and gridlines look in Matplotlib: their direction, length, width, colour, label size, rotation and which sides of the plot show them. Call it on an axes, ax.tick_params(...), or use plt.tick_params(...) for the current axes. It changes existing and future ticks, so you do not need to recreate them. This guide explains every important Matplotlib tick_params parameter with real plots, plus colorbars, zorder, seaborn and two mistakes that silently do nothing.

Tested with Python 3.12.5, Matplotlib 3.11.2 and seaborn 0.13.2; the plots are real Matplotlib windows. The full parameter list is in the Axes.tick_params documentation.

Basic tick_params example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(x, np.sin(x))

ax.tick_params(axis="both", direction="in", length=8, width=1.5,
               colors="darkblue", labelsize=12, pad=8)
ax.set_title("tick_params: direction, length, width, colors, labelsize")
plt.tight_layout()
plt.show()
Matplotlib sine plot with inward dark blue ticks of length 8, width 1.5, label size 12 and padding 8 set with ax.tick_params
One call styles both axes.

tick_params parameters

ParameterValuesWhat it does
axis“x”, “y”, “both” (default)Which axis to change
which“major” (default), “minor”, “both”Major ticks, minor ticks or both
direction“out”, “in”, “inout”Ticks point out of, into, or across the axes
length, widthpointsTick size and line thickness
colorcolourColour of the tick marks only
labelcolorcolourColour of the tick labels only
colorscolourColour of both tick marks and labels
labelsizepoints or “small”, “large”…Font size of the tick labels
labelrotationdegreesRotate the tick labels
padpointsDistance between tick and label
bottom, top, left, rightTrue / FalseShow or hide the tick marks on each side
labelbottom, labeltop, labelleft, labelrightTrue / FalseShow or hide the tick labels on each side
grid_color, grid_alpha, grid_linewidth, grid_linestylegrid line propertiesStyle the gridlines at the ticks

direction: out, in and inout

The default is "out". "in" is common in scientific plots and "inout" crosses the axis line:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 3, figsize=(9, 3))
for ax, direction in zip(axes, ["out", "in", "inout"]):
    ax.plot([0, 1, 2, 3], [1, 3, 2, 4])
    ax.tick_params(direction=direction, length=8, width=1.2)
    ax.set_title(f'direction="{direction}"')
plt.tight_layout()
plt.show()
Three Matplotlib plots showing tick_params direction out, direction in and direction inout
direction="out", "in" and "inout".

Major and minor ticks, and label colours

Use which= to style major and minor ticks separately, and axis= to change only one axis. color changes the tick marks, labelcolor the labels, and colors both:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(x, np.exp(x / 4))
ax.minorticks_on()                                            # add minor ticks

ax.tick_params(which="major", length=10, width=1.5, color="black", labelsize=11)
ax.tick_params(which="minor", length=4, width=0.8, color="gray")
ax.tick_params(axis="y", labelcolor="tab:red")                # only the y tick labels
plt.tight_layout()
plt.show()
Matplotlib exponential curve with long black major ticks, short gray minor ticks and red y-axis tick labels set with tick_params which and axis
Major and minor ticks styled separately; red labels on the y-axis only.

Rotate tick labels and show ticks on all sides

import matplotlib.pyplot as plt

months = ["January", "February", "March", "April", "May", "June"]
fig, ax = plt.subplots(figsize=(7, 4.2))
ax.bar(months, [12, 15, 11, 18, 21, 19], color="tab:olive")

ax.tick_params(axis="x", labelrotation=45)                    # rotate the x tick labels
ax.tick_params(top=True, right=True, direction="in")          # ticks on all four sides
ax.tick_params(labelright=True)                               # y labels on the right as well
plt.tight_layout()
plt.show()
Matplotlib bar chart of months with x tick labels rotated 45 degrees and inward ticks on all four sides with labels on the left and right
labelrotation=45, top=True, right=True and labelright=True.

For alignment options when rotating, see rotate tick labels in Matplotlib; to hide ticks or labels instead, see remove tick labels in Matplotlib.

plt.tick_params vs ax.tick_params

They accept exactly the same arguments. plt.tick_params() changes the current axes (the last one created or selected); ax.tick_params() changes the axes you call it on. With several subplots, use the axes version so you know which plot changes; to style all subplots, loop over them: for ax in axes.flat: ax.tick_params(labelsize=8).

Colorbar ticks: cbar.ax.tick_params

A colorbar has its own axes, so style it through cbar.ax:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.default_rng(2).normal(size=(15, 15)).cumsum(axis=1)
fig, ax = plt.subplots(figsize=(6.5, 5))
im = ax.imshow(data, cmap="magma")
cbar = fig.colorbar(im, ax=ax)

ax.tick_params(labelsize=9, length=0)                          # the image axes
cbar.ax.tick_params(labelsize=11, direction="in", length=6, colors="tab:blue")   # the colorbar
plt.show()
Matplotlib heat map with a colorbar whose ticks are styled with cbar.ax.tick_params: inward blue ticks and larger labels
The colorbar ticks are changed with cbar.ax.tick_params().

Check the current settings, and why bottom=’off’ does not work

Matplotlib 3.7 added get_tick_params() to read what you set. Older tutorials use bottom='off', which was the Matplotlib 1.x syntax; today the string 'off' is simply a truthy value, so the ticks stay visible without any error:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.tick_params(axis="x", direction="out", length=6, labelsize=9)
print("x tick settings:", ax.xaxis.get_tick_params(which="major"))    # Matplotlib 3.7+

# old Matplotlib 1.x style: the string "off" is truthy, so the ticks stay visible
ax.tick_params(axis="x", bottom="off")
fig.canvas.draw()
print("bottom='off' -> tick drawn:", bool(ax.xaxis.get_major_ticks()[0].tick1line.get_visible()))

ax.tick_params(axis="x", bottom=False)                                # correct: a boolean
fig.canvas.draw()
print("bottom=False -> tick drawn:", bool(ax.xaxis.get_major_ticks()[0].tick1line.get_visible()))

Output:

x tick settings: {'length': 6, 'direction': 'out', 'bottom': True, 'top': False, 'labelbottom': True, 'labeltop': False, 'gridOn': False, 'labelsize': 9}
bottom='off' -> tick drawn: True
bottom=False -> tick drawn: False
Command Prompt output of get_tick_params and a test showing bottom='off' still draws the tick while bottom=False hides it
bottom='off' still draws the tick; bottom=False hides it.

tick_params zorder: draw ticks above the data

tick_params() accepts a zorder argument, but in practice the ticks are drawn together with their axis, so what decides whether ticks appear above or below your data is the axis order. Use ax.set_axisbelow(False) to put ticks and gridlines on top (or True to put them behind everything):

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 60)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3.6))
for ax in (ax1, ax2):
    ax.plot(x, 3 + np.sin(x), color="tab:blue")
    ax.axhline(0.35, color="gold", linewidth=22)             # a thick band along the bottom edge
    ax.set_ylim(0, 5)
    ax.tick_params(direction="in", length=14, width=2.5, color="red")

ax1.set_title("default: the band hides the x ticks")
ax2.set_axisbelow(False)                                      # draw ticks (and grid) above the data
ax2.set_title("ax.set_axisbelow(False)")
plt.tight_layout()
plt.show()