-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Setting xticklabels causes warning related to FixedFormatter #18848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That's on purpose. You should also explicitly specify the three ticks to go with the label and then this warning should go away. OTOH I agree the warning is pretty mysterious if u are just using set_xticklabels. |
I see. It would be good to fix the message. Also, If you do set the tick locations, you have to do it first (I revised my code sample to show this). Yes, it's cleaner to set the locations first and then the labels, but I don't think it should be a warning. As long as the locator and labels have both been set by the time the plot is drawn, it should be OK. |
The problem is when people zoom or otherwise change the view, and we continue to use an AutoLocator, the labels will often end up applied to the wrong ticks. By encouraging you to manually specify the ticks we help you avoid that possibility. |
Possibly adding a warning a matplotlib/lib/matplotlib/axis.py Line 1739 in 5ba3911
makes sense. |
Thanks, @jklymak: now I understand why the warning was included. However, I'm assuming there is a lot of code out there - the one I use the most on a day-to-day basis included - that only plots "fixed" charts. Therefore, having to add a line of code using FixedLocator (if what I want is just to format the existing labels), would make the conde unnecessarily polluted, no? Maybe the potential issue you describe should be included somewhere on matplotlib's documentation? |
Its a warning that you are doing something poorly defined. If you want to suppress it either specify the ticks ax.set_xticks([1, 2, 3])
ax.set_xticklabels(['$\pi$', '$2\pi$', '$3\pi$']) filter the Warning, or just ignore it. |
Side note: I'm tempted to modify That would allow to Note also that |
This worked fine in a similar situation:
|
fwiw I agree with @timhoffm's proposal at #18848 (comment), although a variant (that I think I suggested somewhere else) may be to support |
I would find a dict hard to remember and assemble. But two matching lists seems fine to me... |
A single parameter would work relatively well also if you start with two lists, either as dict
and/or as list of tuples
My concern is however, that we basically don't have such compound parameters in our API and mostly use separate parameters. |
I would probably assemble such a dict using a dict comprehension. |
In case we should accept a dict, you are free to create that any way you want. On a side node, note however, that for a dict comprehension |
I'm not overly wedded to the dict idea either, but it's likely that the absence of such APIs in our codebase just reflects our matlab heritage. |
@timhoffm , I disagree. Boo Matlab! |
... but |
Hm can someone summarize the suggested solution after #20047? Example: import pandas as pd
from matplotlib.ticker import FuncFormatter
pd.Series({'a': 10, 'b': 20, 'c': 30}).plot.bar()
ax = plt.gca()
formatter = FuncFormatter(lambda x, pos: f"{x}, {pos}")
ax.xaxis.set_major_formatter(formatter)
What I had before was something like
|
Those are categorical plots; the axis is not the categories, but 0, 1, 2, 3... which is transformed by
|
Thanks, I figured that the category-indexes were transformed by a formatter. I feel that requires a lot more knowledge of the internals than what I was doing before, and it's quite a common operation. Basically this is a |
Sorry if I'm not following, what is wrong with the following? It seems to work fine with categoricals?
|
If you're like me and were simply trying to rotate the axis labels without any other changes, the following answer on stack overflow avoids the warning: https://stackoverflow.com/a/52461208/768439. |
I ran into the same issue as @amueller - attempting to use The easiest solution seems to be: ax.set_xticklabels([my_func(x.get_text()) for x in ax.get_xticklabels()]) |
That seems orthogonal to this issue, and has always been the case - FuncFormatter always worked on the tick values, not the label text. |
The values for categorical should be the strings, but I can't think of a good way to do implement that given the internal mpl architecture needs it to be numeric. |
* removed automatically reading the problem in each Optimize-/Profile-/Sample-reader. Set default argument for reading problem to false as the problem can in general not be stored completely. * fix matplotlib warning. More on this here: matplotlib/matplotlib#18848 * fixed warning for missing burn in by performing geweke test within test pipeline. * fixed warning for usage of distplot, as this will be removed in a future seaborn version. * added probability as stat to normalize. * added comment regarding axis argument error in seaborn. Not sure how to fix. * changed `bw` to `bw_method`. * changed `bw` to `bw_method`. Fixed a typehint. * reworked FixedLocator warning. * remove DeprecationWarning: Flags not at the start of the expression '^(?i)(ls_)'. * fixed problem with MPI pool engine test. * fixed problem in set_yticks. Missing parentheses. * fixed ValueError in set_yticklabels. * increased possible runtime on tests. * fixed notebook `hdf5_storage.ipynb`. * put timeout minutes back to 10min again Co-authored-by: Yannik Schälte <[email protected]>
Directly setting axis labels raises a warning from matplotlib that results may not be correct in every display scenario. We don't currently hit those cases here, but this resolves the warnings in a more general way. See: matplotlib/matplotlib#18848 Co-authored-by: Ben Williams <[email protected]>
matplotlib 3.3 and above gives a warning when calling `get_shift_graph`: UserWarning: FixedFormatter should only be used together with FixedLocator This issue explains why the warning occurs and suggests setting xticks first before setting xticklabels: matplotlib/matplotlib#18848
matplotlib 3.3 and above gives a warning when calling `get_shift_graph`: UserWarning: FixedFormatter should only be used together with FixedLocator This issue explains why the warning occurs and suggests setting `xticks` first before setting `xticklabels`: matplotlib/matplotlib#18848
Bug report
Bug summary
I get a warning related to
FixedFormatter
even though I am not setting a formatter.Thanks to user Rational-IM on StackOverflow and the good folks at pandas for isolating and reproducing the bug.
Code for reproduction
Actual outcome
Expected outcome
Works on 3.2.2 with no warning.
Matplotlib version
Installed from conda
The text was updated successfully, but these errors were encountered: