In order to set the labels of an axis object, one uses the xticklabels
method:
fig, ax = plt.subplots()
ax.imshow(np.eye(101))
labels = np.linspace(2, 4, 4)
ax.set_xticks([0, 33, 66, 100])
ax.set_xticklabels(labels)
Which gives:
It is also possible to format the labels using a formatter:
fig, ax = plt.subplots()
labels = np.linspace(2, 4, 4)
ax.imshow(np.eye(101))
ax.set_xticks([0, 33, 66, 100])
ax.set_xticklabels(labels)
ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
But the formatter only uses the tick position, and not his label:
The workaround is to manually format the labels before using set_xticklabels
:
fig, ax = plt.subplots()
labels = ["{0:.1f}".format(x) for x in np.linspace(2, 4, 4)]
ax.imshow(np.eye(101))
ax.set_xticks([0, 33, 66, 100])
ax.set_xticklabels(labels)
So, there are two questions:
set_xticklabels
function? For example, by using a format string.set_xticklabels
and the Formatter? It appears that the Formatter is not taking set_xticklabels
at all into account, and produces the labels by itself.Tick formatters can be set in one of two ways, either by passing a str or function to set_major_formatter or set_minor_formatter , or by creating an instance of one of the various Formatter classes and providing that to set_major_formatter or set_minor_formatter .
set_xticklabels() function in axes module of matplotlib library is used to Set the x-tick labels with list of string labels.
Setting the xticklabels is equivalent to using a FixedFormatter
.
ax.set_xticklabels(ticks)
is the same as
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticks))
(see source code)
If you have set the ticklabels and then set a formatter, it will simply overwrite the previously set ticklabels. And vice versa.
Because set_xticklabels
uses a FixedFormatter
and not e.g. a FormatStrFormatter
, you need to format the ticklabels yourself. In that sense, using a FormatStrFormatter
might be the better method than using a preformatted list.
There is also another alternative, by using the extent
parameter of the imshow
function: If you just want an automatic position and format for the ticks, it will handle everything for you:
fig, ax = plt.subplots()
ax.imshow(np.eye(101), extent=[2, 4, 2, 4])
Just be careful to arrange your matrix in the good order: with extent
, the minimum tick values on the x and y axes will always be on the bottom-left corner! (So matrix[-1, 0]
).
By now (tested on matplotlib 3.4.2) you can also use the .format_ticks
method of a Formatter
.
ax.set_xticklabels(FormatStrFormatter('%.2f').format_ticks(ticks))
The Formatter
formats the ticks before passing them to set_xticklabels
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With