Skip to content

Fix multiple zero labels when using SymLogNorm #10129

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 4, 2018

Conversation

Raab70
Copy link
Contributor

@Raab70 Raab70 commented Dec 29, 2017

PR Summary

Use the previously unused position of a tick label to ensure that only a single zero label is rendered when using SymLogNorm. I store the first time a zero label is encountered and return blanks for all following zeros. The documentation on the ticker.SymmetricalLogLocator.tick_values shows how these multiple zeros come to be through the linear approximation near zero.

As per discussion below I have taken a different approach, trying to fix the root cause of the problem in the locator (ticker.SymmetricalLogLocator.tick_values) so that the extra zero ticks are never created in the first place.

Fixes #10122

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • N/A New features are documented, with examples if plot related
  • N/A Documentation is sphinx and numpydoc compliant
  • N/A Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • N/A Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

This seems OK to me. But isn't the root problem that there are two zeros returned by the Locator? Can't we just remove the duplicate entry from the Locator?

@jklymak
Copy link
Member

jklymak commented Dec 29, 2017

class SymmetricalLogLocator(Locator):

return self.raise_if_exceeds(np.array(ticklocs))

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

The strategy of this PR can't work. If you run e.g. examples/pyplots/pyplot_scales.py with this PR applied, and move or zoom the symlog axes, the label at 0 disappears (because it is never redrawn).

I believe that @jklymak's idea is the correct approach.

@Raab70
Copy link
Contributor Author

Raab70 commented Dec 29, 2017 via email

@tacaswell tacaswell added this to the v2.2 milestone Dec 30, 2017
@Raab70 Raab70 force-pushed the bug-fix/logsymnorm-zero-labels branch from fd130c1 to b26b598 Compare December 30, 2017 15:22
@Raab70 Raab70 force-pushed the bug-fix/logsymnorm-zero-labels branch from b26b598 to 7fe5e4e Compare December 30, 2017 15:27
@Raab70
Copy link
Contributor Author

Raab70 commented Dec 30, 2017

@jklymak @anntzer Thanks for your help and patience, I believe this is the root of the issue. The multiple zeros are stemming from subticks being added. This change just prevents the addition of subticks to the zero label (since they will all be zero).

@tacaswell
Copy link
Member

👍 That makes sense and looks like a reasonable fix.

Could you add a test? I suspect creating a symlog axis and then asserting that there is only 0 zero label it in is the best approach (no need for an image test). The OP example is maybe a bit complex to trigger this, but as a last resort would be ok to use a test.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Fix looks good, just needs a test.

Anyone can dismiss this when a test is added.

@anntzer anntzer dismissed their stale review January 2, 2018 00:42

implementation changed

@jklymak jklymak dismissed tacaswell’s stale review January 2, 2018 17:26

Dismissing @tacaswell review as test now added per his wishes

@jklymak
Copy link
Member

jklymak commented Jan 2, 2018

Just for completeness, the test is:

from matplotlib import pyplot as plt
import matplotlib as mpl

fig, axes = plt.subplots(1, 2, True, True)

for i, ax in enumerate(axes): 
    im = ax.imshow([[0]], norm=mpl.colors.SymLogNorm(1e-5, vmin=-1, vmax=1))
    cb = plt.colorbar(im, ax=ax)

# Clean up the labels
zero_labelled = False
for label in cb.ax.yaxis.get_ticklabels():
    if label.get_text() == r'$\mathdefault{0}$':
        if zero_labelled:
            label.set_visible(False)
        zero_labelled = True
plt.show()

@dstansby
Copy link
Member

dstansby commented Jan 4, 2018

Thanks a lot @Raab70!

@dstansby dstansby merged commit a1e9db1 into matplotlib:master Jan 4, 2018
@Raab70 Raab70 deleted the bug-fix/logsymnorm-zero-labels branch January 4, 2018 20:09
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Color bar has multiple labels for 0 if matplotlib.colors.SymLogNorm is used
6 participants