Skip to content

[Bug]: using "set_major_formatter" after "set_ticks" reset ticks arragement #26947

@PlasmaThias

Description

@PlasmaThias

Bug summary

I arrange the ticks of an axis with the "set_ticks" method and then use "set_major_formatter" to specify the decimal number. The "set_major_formatter" method resets the arrangement I previously made with "set_ticks"

Code for reproduction

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

line = [5.123, 4.123, 3.123, 2.123, 1.123, 0, 1.123, 2.123, 3.123, 4.123, 5.123]

y = np.arange(len(line))

fig, ax = plt.subplots(1)

ax.plot(line, line)

ax.xaxis.set_ticks(ticks = np.sort(line), labels = line)
#comment/uncomment the following line to see the issue
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))

Actual outcome

I arrange the xticks but do not change the decimal number.
without_set_major_formatter
I remove some decimal number, but the arrangement on the x ticks disappears.
set_major formatter_reset_issue

Expected outcome

to obtain the expected outcome I must substitute:

ax.xaxis.set_ticks(ticks = np.sort(line), labels = line)
ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))

by

ax.xaxis.set_ticks(ticks = np.sort(line) , labels = np.array(["{:.1f}".format(line[i]) for i in range(len(line))]))

expected_outcome

Additional information

No response

Operating system

windows/Linux

Matplotlib Version

3.5

Matplotlib Backend

Qt5Agg

Python version

3.7.12

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions