Skip to content

Improve formatting of imshow() cursor data when a colorbar exists. #12459

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
Dec 10, 2018

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 9, 2018

When a colorbar exists, use its formatter to format cursor data.
For example, after

imshow([[10000, 10001]]); colorbar()

currently the cursor data on either pixel is rendered as 1e4, but after
this patch it becomes 0.0+1e4 / 1.0+1e4, or 10000.0 / 10001.0 if
rcParams["axes.formatter.useoffset"] is set to False. (Even though
the version with the offset text may not be the most esthetic, it's
clearly more informative than the current behavior...)

It would be nice if this worked even for ScalarMappables that don't
have a colorbar; this may include extracting the Formatter selection
code out of the colorbar code into something generally applicable to
ScalarMappables, or just generating a hidden colorbar "on-the-fly" if
needed just for the purpose of getting its Formatter.

PR Summary

PR Checklist

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

@anntzer anntzer added this to the v3.1 milestone Oct 9, 2018
@anntzer anntzer force-pushed the imshow-cursordata branch from 4481b0c to 02a0455 Compare October 9, 2018 15:05

Only handles fully math and fully non-math strings.
"""
if s[:1] == s[-1:] == "$":
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to write if s[0] == s[-1] == "$"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was to handle empty strings as well (which do occur here if e.g. you disable offsettexts), but I realized this would still give the wrong result with s = "$". So I added an explicit check on len(s) instead.

@anntzer anntzer force-pushed the imshow-cursordata branch from 02a0455 to 1c9dbc5 Compare October 9, 2018 18:47
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.

Looks good; needs tests, and an API note I think.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 10, 2018

added whatsnew (it's not an API change) and test

When a colorbar exists, use its formatter to format cursor data.
For example, after
```
imshow([[10000, 10001]]); colorbar()
```
currently the cursor data on either pixel is rendered as 1e4, but after
this patch it becomes 0.0+1e4 / 1.0+1e4, or 10000.0 / 10001.0 if
`rcParams["axes.formatter.useoffset"]` is set to False.  (Even though
the version with the offset text may not be the most esthetic, it's
clearly more informative than the current behavior...)

It would be nice if this worked even for ScalarMappables that don't
have a colorbar; this may include extracting the Formatter selection
code out of the colorbar code into something generally applicable to
ScalarMappables, or just generating a hidden colorbar "on-the-fly" if
needed just for the purpose of getting its Formatter.
@anntzer
Copy link
Contributor Author

anntzer commented Nov 12, 2018

todo: confirm that nothing horrible happens with boundarynorms.

@anntzer
Copy link
Contributor Author

anntzer commented Dec 8, 2018

Actually one can just try with examples/images_contours_and_fields/image_masked.py or examples/images_contours_and_fields/image_annotated_heatmap.py: BoundaryNorms work just fine with this PR.

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.

4 participants