Closed
Closed
Copy link
Description
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.
I remove some decimal number, but the arrangement on the x ticks disappears.
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))]))
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
Labels
Users in need of help.Users in need of help.