matplotlib
data visualization
python
plotting
x-axis adjustment

Moving x-axis to the top of a plot in matplotlib

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Matplotlib puts the x-axis at the bottom by default, but you can move the tick labels and axis label to the top when the chart reads better that way. The most common approach is to tell the axes to place x ticks on top and disable them on the bottom. The underlying plot data does not change. Only the axis presentation does.

Move the X-Axis Ticks to the Top

A minimal example looks like this:

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [10, 15, 12, 18]
5
6fig, ax = plt.subplots()
7ax.plot(x, y)
8
9ax.xaxis.tick_top()
10ax.tick_params(axis="x", labeltop=True, labelbottom=False)
11
12plt.show()

tick_top() places the ticks on the top edge, and tick_params hides the bottom labels so you do not end up with duplicated labels.

Move the X-Axis Label Too

If the plot has an x-axis label, you usually want that label on top as well.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([1, 2, 3], [4, 5, 6])
5
6ax.set_xlabel("Time")
7ax.xaxis.set_label_position("top")
8ax.xaxis.tick_top()
9ax.tick_params(axis="x", labeltop=True, labelbottom=False)
10
11plt.show()

Without set_label_position("top"), the tick labels may move while the axis label stays at the bottom.

Hide the Bottom Spine if Needed

If you want a cleaner top-axis look, hide the bottom spine and optionally show the top spine explicitly.

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4ax.plot([0, 1, 2], [2, 1, 3])
5
6ax.spines["bottom"].set_visible(False)
7ax.spines["top"].set_visible(True)
8ax.xaxis.set_label_position("top")
9ax.xaxis.tick_top()
10ax.tick_params(axis="x", labeltop=True, labelbottom=False)
11
12plt.show()

This is useful when the design should clearly emphasize the top edge as the primary x-axis location.

Use It Intentionally

Moving the x-axis to the top is not just a styling trick. It can make sense when:

  • the bottom area is crowded with annotations
  • the chart sits under a table-like header
  • the visual reading order works better from top down
  • the plot is part of a report layout that expects labels at the top

Still, the default bottom axis is more familiar for many readers, so this change should usually be intentional rather than decorative.

It Also Works with Images and Heatmaps

The same axis controls are useful beyond line plots. For heatmaps or matrix-like graphics, top-positioned x labels are common.

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4fig, ax = plt.subplots()
5image = ax.imshow(np.arange(9).reshape(3, 3))
6
7ax.set_xticks([0, 1, 2])
8ax.set_xticklabels(["A", "B", "C"])
9ax.xaxis.tick_top()
10ax.tick_params(axis="x", labeltop=True, labelbottom=False)
11
12plt.show()

That pattern is especially common when the x labels behave like column headers.

In report-style figures, top-positioned x labels can work especially well when the plot sits directly under headings or table-like annotations. The axis then reads more like a header row than a conventional bottom scale.

Common Pitfalls

The biggest mistake is moving the x ticks to the top but forgetting to disable the bottom labels. That often leaves both sets visible and makes the figure look cluttered.

Another issue is moving the tick marks but not the axis label. If the label stays at the bottom while the ticks are on top, the plot can look half-finished.

People also sometimes hide the wrong spine and wonder why the axis still looks unchanged. Tick location, label location, and spine visibility are related but separate settings in Matplotlib.

Finally, do not move the axis just because it is possible. For many charts, the default bottom placement is still clearer and more conventional.

Summary

  • Use ax.xaxis.tick_top() to move x ticks to the top.
  • Use tick_params to show top labels and hide bottom labels.
  • Move the axis label with ax.xaxis.set_label_position("top") when needed.
  • Adjust spines separately if you want the frame to emphasize the top edge.
  • Use a top x-axis when it genuinely improves readability, not only for decoration.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.