How to remove the digits after the decimal point in axis ticks in Matplotlib?

To remove the digits after the decimal point in axis ticks in Matplotlib, you can use several approaches. The most common methods involve formatting the tick labels to display only integer values.

Method 1: Using set_xticklabels() with astype(int)

This method converts the tick values to integers, effectively removing decimal places ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.array([1.110, 2.110, 4.110, 5.901, 6.00, 7.90, 8.90])
y = np.array([2.110, 1.110, 3.110, 9.00, 4.001, 2.095, 5.890])

fig, ax = plt.subplots()
ax.plot(x, y)

# Remove decimal points from x-axis ticks
ax.set_xticklabels(x.astype(int))

plt.show()

Method 2: Using FuncFormatter

This approach provides more control over the formatting using a custom formatter function ?

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter

x = np.array([1.110, 2.110, 4.110, 5.901, 6.00, 7.90, 8.90])
y = np.array([2.110, 1.110, 3.110, 9.00, 4.001, 2.095, 5.890])

fig, ax = plt.subplots()
ax.plot(x, y)

# Define a formatter function to remove decimals
def integer_formatter(x, pos):
    return f'{int(x)}'

# Apply the formatter to x-axis
ax.xaxis.set_major_formatter(FuncFormatter(integer_formatter))

plt.show()

Method 3: Using FixedLocator and FixedFormatter

This method gives you complete control over tick positions and labels ?

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FixedLocator, FixedFormatter

x = np.array([1.110, 2.110, 4.110, 5.901, 6.00, 7.90, 8.90])
y = np.array([2.110, 1.110, 3.110, 9.00, 4.001, 2.095, 5.890])

fig, ax = plt.subplots()
ax.plot(x, y)

# Set custom tick positions and integer labels
tick_positions = [1, 2, 4, 6, 8]
tick_labels = ['1', '2', '4', '6', '8']

ax.xaxis.set_major_locator(FixedLocator(tick_positions))
ax.xaxis.set_major_formatter(FixedFormatter(tick_labels))

plt.show()

Comparison

Method Complexity Best For
set_xticklabels() Simple Quick fixes with existing data
FuncFormatter Medium Custom formatting logic
FixedLocator/Formatter Advanced Complete control over ticks

Conclusion

Use set_xticklabels() with astype(int) for simple decimal removal. For more complex formatting needs, use FuncFormatter or FixedLocator with FixedFormatter.

Updated on: 2026-03-26T00:23:53+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements