How to show tick labels on top of a matplotlib plot?

To show tick labels on top of a matplotlib plot, we can use the set_tick_params() method with labeltop=True. This is useful when you want axis labels at the top instead of the default bottom position.

Basic Example

Here's how to move tick labels to the top of a plot −

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.linspace(0, 10, 50)
y = np.sin(x)

# Create the plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, 'b-', linewidth=2)

# Move tick labels to top
ax.xaxis.set_tick_params(labeltop=True)
ax.xaxis.set_tick_params(labelbottom=False)

# Add labels
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
ax.set_title('Plot with Top Tick Labels')

plt.tight_layout()
plt.show()

Customizing Top Tick Labels

You can further customize the appearance of top tick labels −

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]

# Create bar plot
fig, ax = plt.subplots(figsize=(8, 5))
bars = ax.bar(categories, values, color=['red', 'green', 'blue', 'orange', 'purple'])

# Configure top tick labels
ax.xaxis.set_tick_params(
    labeltop=True,      # Show labels on top
    labelbottom=False,  # Hide labels on bottom
    top=True,          # Show tick marks on top
    bottom=False       # Hide tick marks on bottom
)

# Customize label appearance
ax.tick_params(axis='x', which='major', labelsize=12, colors='darkblue')

ax.set_ylabel('Values')
ax.set_title('Bar Chart with Customized Top Labels')
plt.tight_layout()
plt.show()

Both Top and Bottom Labels

You can also show tick labels on both top and bottom simultaneously −

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.arange(0, 5)
y = x ** 2

# Create the plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, 'ro-', markersize=8)

# Show labels on both top and bottom
ax.xaxis.set_tick_params(labeltop=True, labelbottom=True)

# Different labels for top and bottom
bottom_labels = ['Start', 'Quarter', 'Half', 'Three-Quarter', 'End']
top_labels = ['0%', '25%', '50%', '75%', '100%']

ax.set_xticklabels(bottom_labels)
ax2 = ax.twiny()  # Create second x-axis
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(x)
ax2.set_xticklabels(top_labels)

ax.set_ylabel('Y values')
ax.set_xlabel('Bottom Labels')
ax2.set_xlabel('Top Labels (%)')

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Values
labeltop Show labels on top True/False
labelbottom Show labels on bottom True/False
top Show tick marks on top True/False
bottom Show tick marks on bottom True/False

Conclusion

Use ax.xaxis.set_tick_params(labeltop=True, labelbottom=False) to move tick labels to the top. You can customize appearance and even show labels on both top and bottom for different information display.

Updated on: 2026-03-26T02:34:15+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements