Skip to content

Backport PR #13684 on branch v3.1.x (Use format_data_short to format image cursor data.) #13688

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions doc/users/next_whats_new/2018-10-10-AL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ Improved formatting of image values under cursor when a colorbar is present
When a colorbar is present, its formatter is now used to format the image
values under the mouse cursor in the status bar. For example, for an image
displaying the values 10,000 and 10,001, the statusbar will now (using default
settings) display the values as ``0.0+1e4`` and ``1.0+1e4`` (or ``10000.0``
and ``10001.0`` if the offset-text is disabled on the colorbar), whereas both
values were previously displayed as ``1e+04``.
settings) display the values as ``10000`` and ``10001``), whereas both values
were previously displayed as ``1e+04``.
9 changes: 5 additions & 4 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,11 @@ def get_cursor_data(self, event):

def format_cursor_data(self, data):
if self.colorbar:
return ("["
+ cbook.strip_math(self.colorbar.formatter(data))
+ cbook.strip_math(self.colorbar.formatter.get_offset())
+ "]")
return (
"["
+ cbook.strip_math(
self.colorbar.formatter.format_data_short(data)).strip()
+ "]")
else:
return super().format_cursor_data(data)

Expand Down
19 changes: 13 additions & 6 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,29 @@ def test_cursor_data():
assert im.get_cursor_data(event) is None


def test_format_cursor_data():
@pytest.mark.parametrize(
"data, text_without_colorbar, text_with_colorbar", [
([[10001, 10000]], "[1e+04]", "[10001]"),
([[.123, .987]], "[0.123]", "[0.123]"),
])
def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
from matplotlib.backend_bases import MouseEvent

fig, ax = plt.subplots()
im = ax.imshow([[10000, 10001]])
im = ax.imshow(data)

xdisp, ydisp = ax.transData.transform_point([0, 0])
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == 10000
assert im.format_cursor_data(im.get_cursor_data(event)) == "[1e+04]"
assert im.get_cursor_data(event) == data[0][0]
assert im.format_cursor_data(im.get_cursor_data(event)) \
== text_without_colorbar

fig.colorbar(im)
fig.canvas.draw() # This is necessary to set up the colorbar formatter.

assert im.get_cursor_data(event) == 10000
assert im.format_cursor_data(im.get_cursor_data(event)) == "[0.0+1e4]"
assert im.get_cursor_data(event) == data[0][0]
assert im.format_cursor_data(im.get_cursor_data(event)) \
== text_with_colorbar


@image_comparison(baseline_images=['image_clip'], style='mpl20')
Expand Down