Fix multiple zero labels when using SymLogNorm by Raab70 · Pull Request #10129 · matplotlib/matplotlib · GitHub
Skip to content

Fix multiple zero labels when using SymLogNorm#10129

Merged
dstansby merged 2 commits into
matplotlib:masterfrom
Raab70:bug-fix/logsymnorm-zero-labels
Jan 4, 2018
Merged

Fix multiple zero labels when using SymLogNorm#10129
dstansby merged 2 commits into
matplotlib:masterfrom
Raab70:bug-fix/logsymnorm-zero-labels

Conversation

@Raab70

@Raab70 Raab70 commented Dec 29, 2017

Copy link
Copy Markdown
Contributor

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

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

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

jklymak commented Dec 29, 2017

Copy link
Copy Markdown
Member

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

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

Raab70 commented Dec 29, 2017 via email

Copy link
Copy Markdown
Contributor Author

@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

Raab70 commented Dec 30, 2017

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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.

@tacaswell tacaswell left a comment

Copy link
Copy Markdown
Member

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

jklymak commented Jan 2, 2018

Copy link
Copy Markdown
Member

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

dstansby commented Jan 4, 2018

Copy link
Copy Markdown
Member

@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