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
IndexFormatter doesn't round to the nearest integer #16631
Comments
|
I think you are correct that this is a bug, and we should use round rather than |
|
The issue is that |
|
|
|
I am amazed how cascading and tedious is your process. No wonder why there are more than 1200 issues pending resolution. At the end of who knows how many days, maybe even weeks if not months, of deliberations, I won't be able to use |
|
Welcome to reality. ;-)
|
|
Some more points:
from matplotlib import pyplot as plt
from matplotlib.ticker import IndexFormatter
x = [1, 2, 3, 4, 5]
y = [1, 3, 5, 6, 0]
x_labels = ['under', 'a', 'b', 'c', 'd', 'e', 'over']
fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(IndexFormatter(x_labels))
ax.plot(x, y, '-o')
plt.show()The correct fix is to use FuncFormatter, for which you can literally arrange to use whatever logic you want; for example: from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter
x = [1, 2, 3, 4, 5]
y = [1, 3, 5, 6, 0]
x_labels = ['under', 'a', 'b', 'c', 'd', 'e', 'over']
fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(FuncFormatter(
lambda x, _: dict(zip(range(len(x_labels)), x_labels)).get(x, "")))
ax.plot(x, y, '-o')
plt.show() |
|
If the inner workings of This way you would conserve backward compatibility. |
It fails because the test requires the bug to be present. It expects the label for index -1 to display the label for index 0 instead of being empty as it should be. |
|
As you can see, everything we include (or keep) in the library comes at a maintenance cost. In this case the value of the formatter was deemed to not be worth the cost in maintaining it. However, as said above, if there is a good use case for it, we can reconsider. I hope you will have patience with us. This library is used by many thousand people, so there are indeed procedures to follow. Part of getting a "simple fix" into the code base is proving it doesn't break anything. |
|
Any code needs to be maintained, documented, tested, generates bug reports and questions from the user that need to be fielded, etc.; meanwhile, developer time is finite. Hence it makes sense to remove little-used functionality, especially when reasonably simple alternatives exist. |
|
It obviously doesn't make sense to use an |
|
Well, then there should be some sanity checks (as we have implemented for FixedFormatter) to avoid nonsensical results. |
Not at all. You are not an omniscient mind reader. It can and more likely suggests that most users of IndexFormatter don't notice or aren't bothered by this bug. Or they don't bother going through the hassle of reporting this little bug. I didn't notice it for months until I updated a software to insert data points prior index 0, and at first, I blamed myself. It also demonstrates that even some of the simplest bugs don't get fixed after more than a decade. Whoever wrote the test tailored it to disguise the bug as the expected behaviour. And, to be frank, if I had to write a convoluted, barely readable lambda function the first time I encountered matplotlib, I would have never used it. For most users, Try to put yourself in the shoes of a casual user. What you propose, from a casual user standpoint, it's no different than you, someone familiar with the intricacies of matplotlib, having to decipher You are deprecating a feature nobody complained about for a decade to have more time servicing hard to understand and hard to maintain features. It's like deprecating the trouble free I don't see the usefulness of a system that make simple fixes tedious and evolves toward increased complexity by killing the simple things that work trouble free. |
|
It by now seems clear that the example code in the bug report is a misuse of that formatter, because no ticks are specified, and an index of
Point 2. is considerably less work than point 1. But if it turns out that the A final note here: The system actually works fine; and we see it in action just here: A bug was filed, an apparent solution proposed, a test was hit, making people aware of a problem, more people joining in, finding the actual problem, which is a different one than the bug suggested, and now one can work towards a solution. |
|
I would also very kindly suggest that resorting to hyperbole and questioning the intent of the developers is not the best way to encourage us to take your input into consideration. In case this needs to be clarified, we are nearly all unpaid volunteers and have contributed thousands of hours of our free time to this project, which is available to you for free. |
|
Can everyone please take a breath. We all have the same goal (make Matplotlib the best it can be for our users). It looks like the tests were added in #15962 as an exercise in increasing the test coverage and codified the existing behavior (rather than representing the intention of the original author). These tests also identified that the proposed change affects how ever-other half integer is rounded (as An alternative fix would be to discard any negative values and leave the rounding behavior as-is. |
|
I recommend leaving the IndexFormatter deprecated and removing it as scheduled. @Wlodarski, what are some examples of real-world usage of the IndexFormatter's functionality? Would it make sense to add an example to the docs or elsewhere using FuncFormatter? There is no need for the lambda; a normal function definition can be used, with as many lines as are needed to make it easy to read and understand. |
|
@ImportanceOfBeingErnest With the proposed solution, neither option 1 nor option 2 is needed. And surely, those are not the only 2 options, nor the only conceivable solution. @tacaswell just proposed an alternative fix, the first fix that actually makes sense to me. @ImportanceOfBeingErnest If an index of In summary: The system is beyond tedious. It allowed a bug to remain hidden for more than a decade and gives the false impression the developers can infer actual usage based on saliency of problems. Trouble free functionalities are mistaken for useless functionalities, while functionalities that frequently suck the attention of the developers are promoted to further monopolize their efforts. A simple evident solution, rejected by a test that requires the bug to be present, is about to be swept under the rug in favour of increased complexity imposed on the users and malpractices like deprecations within a minor version. Explaining away malpractices by pointing out others also are tempted to commit them is not a valid point. What should have taken no time to solve definitively years ago while maintaining backward compatibility evolves toward a situation driven by a sunk cost fallacy. Criticism of the system is perceived as criticism of the volunteer developers, surely overworked in view of the heaviness of the system. You can tell yourself whatever you want but the fact is that a simple fix like rounding a value instead of truncating it ―replacing a @anntzer I am not blaming any of you. You are the primary victims of this bureaucratic mess. The way the system is set up, for historical reasons I assume, robs of agility and makes the loss of perspective almost ineluctable. The handling of this extremely simple bug is symptomatic of this involuntary loss of perspective. It's bound to happen, especially after you have contributed thousands of hours of our free time to this project. When the easy solutions seem inconceivable and the hard convoluted ones seem far easier to implement, the system is not working for you anymore: you are working for it. |
If you are going the deprecation route, a recipe how to refactor
|
|
@Wlodarski I will take you at your word that you intended to criticize the system, however your text reads as very direct personal criticisms of our volunteers which is not acceptable behavior. |
|
As noted by @efiring the lambda is not optimal for legibility, I simply crammed everything in one line as this was easier to type quickly but a more legible version would be e.g. As for most API deprecations, an even simpler route for people not wanting to move to the new API would be to just copy-paste the old IndexFormatter implementation to their codebase (and possibly fix the round() bug at the same time). In any case, I think this issue is in good hands now and will thus unsubscribe from it to avoid further miscommunication problems. |
|
I am inclined to close this with no-action because the If someone wants to open a PR that checks for being less than 0 before rounding (and correct 1 test case + document the behavior change) that would probably be merged for 3.3, but I do not think we need an open issue to track it. I want to remind everyone again that we expect everyone to engage with each other in a professional manner and follow the PSF Code of Conduct (https://www.python.org/psf/conduct/) on github. I think the OP's use case could be solved using the string categorical support: y = [np.nan, 1, 3, 5, 6, 0, np.nan]
x_labels = ['under', 'a', 'b', 'c', 'd', 'e', 'over']
fig, ax = plt.subplots()
ax.set_xlim(-1, 7)
ax.plot(x_labels, y, '-o') |






Bug report
Bug summary
Because
IndexFormatter.__call__doesn't round to the nearest integer but truncates/round up to the next integer, x ticks labels are duplicated when user pans plot to left, displaying values below minimum.Code for reproduction
Actual outcome
IndexFormatter returns self.labels[0] for x = -1 instead of an empty string
Expected outcome
Proposed solution to correct [IndexFormatter] (https://matplotlib.org/3.1.3/_modules/matplotlib/ticker.html#IndexFormatter)
Matplotlib version
The text was updated successfully, but these errors were encountered: