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()
tick_params parameters
| Parameter | Values | What 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, width | points | Tick size and line thickness |
color | colour | Colour of the tick marks only |
labelcolor | colour | Colour of the tick labels only |
colors | colour | Colour of both tick marks and labels |
labelsize | points or “small”, “large”… | Font size of the tick labels |
labelrotation | degrees | Rotate the tick labels |
pad | points | Distance between tick and label |
bottom, top, left, right | True / False | Show or hide the tick marks on each side |
labelbottom, labeltop, labelleft, labelright | True / False | Show or hide the tick labels on each side |
grid_color, grid_alpha, grid_linewidth, grid_linestyle | grid line properties | Style 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()
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()
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()
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()
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
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()
set_axisbelow(False) draws them on top.tick_params direction=’out’ not working with seaborn
Seaborn themes such as darkgrid and whitegrid turn the tick marks off (xtick.bottom = False), so changing their direction or length has no visible effect. Turn them back on first:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="darkgrid") # this theme hides the tick marks
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3.6))
for ax in (ax1, ax2):
ax.plot([1, 2, 3, 4], [3, 1, 4, 2])
ax.tick_params(direction="out", length=6)
ax1.set_title("direction='out' seems ignored")
ax2.tick_params(bottom=True, left=True) # turn the tick marks back on
ax2.set_title("bottom=True, left=True")
plt.tight_layout()
plt.show()
bottom=True, left=True.Set tick styles for every plot with rcParams
import matplotlib.pyplot as plt
plt.rcParams["xtick.direction"] = "in"
plt.rcParams["ytick.direction"] = "in"
plt.rcParams["xtick.major.size"] = 6
plt.rcParams["xtick.labelsize"] = 11
Continue with these Matplotlib tick and axis guides:
- Remove ticks and tick labels in Matplotlib
- Rotate tick labels in Matplotlib
- Set the x-axis label in Matplotlib
- Change the title font size in Matplotlib
Frequently asked questions
What does tick_params do in Matplotlib?
It changes the appearance of ticks, tick labels and gridlines: direction, length, width, colours, label size, rotation, padding and which sides show ticks and labels.
What is the difference between plt.tick_params and ax.tick_params?
They take the same arguments. plt.tick_params() works on the current axes, ax.tick_params() on a specific axes, which is safer with subplots.
How do I change the tick label font size?
ax.tick_params(labelsize=12), or axis="x" / "y" to change only one axis.
How do I make ticks point outward or inward?
ax.tick_params(direction="out") or direction="in"; "inout" makes them cross the axis.
What is the difference between color, labelcolor and colors in tick_params?
color sets the tick marks, labelcolor the tick labels, and colors both at once.
Why does tick_params(bottom=’off’) not hide the ticks?
That was Matplotlib 1.x syntax. In current versions use booleans: ax.tick_params(bottom=False).
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn