Skip to content

Commit 20a409c

Browse files
authored
Merge pull request #13684 from anntzer/format_cursor_data
Use format_data_short to format image cursor data.
2 parents 34970ed + 351a080 commit 20a409c

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

doc/users/next_whats_new/2018-10-10-AL.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ Improved formatting of image values under cursor when a colorbar is present
44
When a colorbar is present, its formatter is now used to format the image
55
values under the mouse cursor in the status bar. For example, for an image
66
displaying the values 10,000 and 10,001, the statusbar will now (using default
7-
settings) display the values as ``0.0+1e4`` and ``1.0+1e4`` (or ``10000.0``
8-
and ``10001.0`` if the offset-text is disabled on the colorbar), whereas both
9-
values were previously displayed as ``1e+04``.
7+
settings) display the values as ``10000`` and ``10001``), whereas both values
8+
were previously displayed as ``1e+04``.

lib/matplotlib/image.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,11 @@ def get_cursor_data(self, event):
934934

935935
def format_cursor_data(self, data):
936936
if self.colorbar:
937-
return ("["
938-
+ cbook.strip_math(self.colorbar.formatter(data))
939-
+ cbook.strip_math(self.colorbar.formatter.get_offset())
940-
+ "]")
937+
return (
938+
"["
939+
+ cbook.strip_math(
940+
self.colorbar.formatter.format_data_short(data)).strip()
941+
+ "]")
941942
else:
942943
return super().format_cursor_data(data)
943944

lib/matplotlib/tests/test_image.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,29 @@ def test_cursor_data():
291291
assert im.get_cursor_data(event) is None
292292

293293

294-
def test_format_cursor_data():
294+
@pytest.mark.parametrize(
295+
"data, text_without_colorbar, text_with_colorbar", [
296+
([[10001, 10000]], "[1e+04]", "[10001]"),
297+
([[.123, .987]], "[0.123]", "[0.123]"),
298+
])
299+
def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
295300
from matplotlib.backend_bases import MouseEvent
296301

297302
fig, ax = plt.subplots()
298-
im = ax.imshow([[10000, 10001]])
303+
im = ax.imshow(data)
299304

300305
xdisp, ydisp = ax.transData.transform_point([0, 0])
301306
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
302-
assert im.get_cursor_data(event) == 10000
303-
assert im.format_cursor_data(im.get_cursor_data(event)) == "[1e+04]"
307+
assert im.get_cursor_data(event) == data[0][0]
308+
assert im.format_cursor_data(im.get_cursor_data(event)) \
309+
== text_without_colorbar
304310

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

308-
assert im.get_cursor_data(event) == 10000
309-
assert im.format_cursor_data(im.get_cursor_data(event)) == "[0.0+1e4]"
314+
assert im.get_cursor_data(event) == data[0][0]
315+
assert im.format_cursor_data(im.get_cursor_data(event)) \
316+
== text_with_colorbar
310317

311318

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

0 commit comments

Comments
 (0)