You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The explanation for labels says it accepts either sequence of strings or Texts:
However, it's also possible to pass as a parameter e.g. list of ints or floats or similar and the method works too.
I think this should be reflected in the doc.
importrandomimportmatplotlib.pyplotaspltx=sorted(random.sample(range(15), 5))
y=sorted(random.sample(range(10), 5))
fig, ax=plt.subplots(
figsize=(10, 6)
)
ax.plot(x, y, "o-")
ax.set_xticks(x)
ax.set_xticklabels(x) # x is `list` of INTs, not stringsax.grid(axis="x")
Gives this, without any problems:
Or if you generate list of floats:
x_labels= [random.random() for_inrange(len(x))]
And then update the tick labels:
ax.set_xticklabels(x_labels) # x is `list` of FLOATs, not strings
I'll get this:
Suggested improvement
Update the description so it's clear numeric arrays can be passed on as a parameter.
The text was updated successfully, but these errors were encountered:
This is the same (core) problem as the point about xlabel in #26858. The actual code certainly will accept any object (by virtue of having a __str__ method), but how much of that should be considered "expected" vs "implementation detail"?
One suggestion we have been discussing (from the type hinting side at least) is having an alias like StrLike which actually resolves to object or Any but communicates the intent to treat it as a str.
In the case of passing numbers, it is actually quite unintuitive what happens, as the numbers are now labeling a point (possibly) other than the point that is numerically equivalent. (Even in your (float) example, the numbers appear out of order, for instance, which is likely to cause confusion.) While it works, and if that is truly what you want, you can do it, I actually think by encouraging (if not requiring) explicit casting to string makes your code easier to tell that that was intentional and not e.g. wanting to call set_xticks, but actually calling set_xticklabels. Even moreso with floats, as I think if you were actually wanting float labels, it is likely that you would want to format them to e.g. have 3 decimal places (and also likely that you want it showing up at the numerical equivalence point, at which point, a Formatter is usually the tool for the job rather than passing tick labels directly)
As written in #26867 (comment), we’re not going to broaden the docstring types. It’s important that this is logically a str. Consider it an implementation detail that we accept other objects and convert them to str. We’re not making any guarantee that this will always work in the future.
Documentation Link
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html
Problem
The explanation for
labels
says it accepts either sequence of strings or Texts:However, it's also possible to pass as a parameter e.g.
list
ofints
orfloats
or similar and the method works too.I think this should be reflected in the doc.
Gives this, without any problems:
Or if you generate list of floats:
And then update the tick labels:
I'll get this:

Suggested improvement
Update the description so it's clear numeric arrays can be passed on as a parameter.
The text was updated successfully, but these errors were encountered: