matplotlib
xticks
data visualization
plotting
Python

How to remove xticks from a plot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Removing x-axis ticks is common when a chart is decorative, uses dense categories, or appears as a small panel in a dashboard. In Matplotlib, there are several ways to do it, and each method affects tick marks, labels, and layout differently. Picking the correct method avoids broken axes and inconsistent styling across figures.

Decide What to Hide

Before writing code, decide whether you want to hide:

  • Tick marks and labels.
  • Labels only.
  • Major ticks but keep minor ticks.

These are different operations. Many plots look wrong because code hides everything when only labels should disappear.

Remove All X Ticks with set_xticks([])

The most direct method is setting an empty tick list.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4, 5]
4y = [2, 3, 5, 4, 6]
5
6fig, ax = plt.subplots(figsize=(6, 3))
7ax.plot(x, y, marker="o")
8ax.set_title("Line chart without x ticks")
9ax.set_xticks([])
10
11plt.tight_layout()
12plt.show()

This removes major ticks and labels. Use this when x-axis values are not needed by readers.

Hide Labels but Keep Tick Positions

Sometimes you want the visual rhythm of ticks but no text labels.

python
1import matplotlib.pyplot as plt
2
3x = [0, 1, 2, 3, 4]
4y = [10, 12, 9, 14, 13]
5
6fig, ax = plt.subplots(figsize=(6, 3))
7ax.bar(x, y)
8ax.set_title("Tick marks visible, labels hidden")
9ax.tick_params(axis="x", labelbottom=False)
10
11plt.tight_layout()
12plt.show()

This is useful in faceted charts where only the bottom subplot shows labels.

Remove Tick Marks and Labels with tick_params

If you want a fully clean axis line:

python
1import matplotlib.pyplot as plt
2
3x = ["A", "B", "C", "D"]
4y = [7, 5, 8, 6]
5
6fig, ax = plt.subplots(figsize=(6, 3))
7ax.plot(x, y, linewidth=2)
8ax.set_title("No x ticks and no labels")
9ax.tick_params(axis="x", which="both", bottom=False, top=False, labelbottom=False)
10
11plt.tight_layout()
12plt.show()

This keeps the axis itself but removes tick artifacts.

Use NullLocator for Programmatic Control

For reusable plotting functions, a locator can be cleaner than setting empty lists.

python
1import matplotlib.pyplot as plt
2from matplotlib.ticker import NullLocator
3
4x = list(range(50))
5y = [v * 0.5 for v in x]
6
7fig, ax = plt.subplots(figsize=(7, 3))
8ax.plot(x, y)
9ax.xaxis.set_major_locator(NullLocator())
10ax.set_title("X major ticks disabled via locator")
11
12plt.tight_layout()
13plt.show()

This approach works well when styling is applied in one central utility.

Subplot Example: Hide X Ticks on Upper Panels

In multi-panel plots, hide upper x ticks to reduce clutter and keep labels only on the bottom panel.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4x = np.linspace(0, 2 * np.pi, 200)
5
6fig, axes = plt.subplots(3, 1, sharex=True, figsize=(7, 6))
7axes[0].plot(x, np.sin(x))
8axes[1].plot(x, np.cos(x))
9axes[2].plot(x, np.sin(2 * x))
10
11for ax in axes[:-1]:
12    ax.tick_params(axis="x", labelbottom=False)
13
14axes[0].set_title("Shared x-axis with reduced tick noise")
15plt.tight_layout()
16plt.show()

This preserves readability without losing x-axis context.

Seaborn Integration

Seaborn uses Matplotlib axes underneath, so apply the same methods on the returned axis.

python
1import seaborn as sns
2import pandas as pd
3import matplotlib.pyplot as plt
4
5df = pd.DataFrame({"category": ["A", "B", "C"], "value": [4, 7, 3]})
6ax = sns.barplot(data=df, x="category", y="value")
7ax.set_xticks([])
8ax.set_title("Seaborn chart without x ticks")
9plt.show()

Common Pitfalls

  • Removing ticks when only labels needed to be hidden.
  • Hiding x-axis on all subplots and losing orientation cues for readers.
  • Applying tick changes before creating the correct axis object.
  • Mixing global plt.xticks([]) with object style ax calls in complex figures.
  • Forgetting that shared axes can propagate tick behavior to linked subplots.

Summary

  • Choose between hiding tick marks, labels, or both based on chart purpose.
  • Use set_xticks([]) for full removal in simple cases.
  • Use tick_params when you need finer control.
  • For reusable utilities, consider NullLocator.
  • In multi-panel layouts, keep labels only where they add value.

Course illustration
Course illustration

All Rights Reserved.