Skip to content

Fix error that occurs when minorticks are on multi-Axes Figure with more than one boxplot #27647

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

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

saranti
Copy link
Contributor

@saranti saranti commented Jan 14, 2024

PR summary

Closes #26484. Having minorticks_on() in a multi-Axes Figure that contains more than one boxplot and shares the x-axis was raising an OverflowError.

As @ksunden pointed out in #26484 (comment), the boxplots set the Locator and Formatter of one axis to be FixedLocator/FixedFormatter, without handling repeated values. This leads to multiple repeated values for the Major ticks, which then the AutoMinorLocator does major[1] - major[0] to get the spacing of the major, which returns zero and then divides by 0, leading to the error.

I've implemented the option on the first bullet point of the above comment to call np.unique on the majorticklocs in AutoMinorLocator. This fix remove repeated values from majorticklocs to avoid the zero division that was causing the error.

PR checklist

Copy link
Member

@ksunden ksunden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case could be further simplified

  • remove random (the values from the boxplot don't matter, may as well just do np.arange(10) or something
  • reduce number of subplots (2 will do, there is likely a way of getting it with just one plot, but may be slightly contrived, as the sharex is part of the cause for the specific presentation)
    • plotting 2 boxplots at the same positions should probably do it)
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.boxplot(np.arange(10), positions=[0])
ax.boxplot(np.arange(10), positions=[0])
ax.boxplot(np.arange(10), positions=[1])

ax.minorticks_on()
fig.draw_without_rendering()

has the same behavior (I included one at another position such that np.unique has at least 2 values (and thus doesn't go into the return [] case just under the changed line of code)

Assertions then don't need the all, as there is only one Axes.

@dstansby dstansby added this to the v3.8.3 milestone Jan 17, 2024
@dstansby dstansby merged commit b03407b into matplotlib:main Jan 17, 2024
Copy link

lumberbot-app bot commented Jan 17, 2024

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v3.8.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 b03407b375d8a4b5101f40178f9d19b84bfce822
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #27647: Fix error that occurs when minorticks are on multi-Axes Figure with more than one boxplot'
  1. Push to a named branch:
git push YOURFORK v3.8.x:auto-backport-of-pr-27647-on-v3.8.x
  1. Create a PR against branch v3.8.x, I would have named this PR:

"Backport PR #27647 on branch v3.8.x (Fix error that occurs when minorticks are on multi-Axes Figure with more than one boxplot)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

dstansby added a commit to dstansby/matplotlib that referenced this pull request Jan 17, 2024
Fix error that occurs when minorticks are on multi-Axes Figure with more than one boxplot

(cherry picked from commit b03407b)
@saranti saranti deleted the minorticks branch January 24, 2024 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Turning on minor gridlines in a multi-Axes Figure, created with subplots(), that contains >1 boxplot results in a ValueError
3 participants