Skip to content
Closed
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/2018-10-10-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Improved formatting of image values under cursor when a colorbar is present
```````````````````````````````````````````````````````````````````````````

To format image values under the mouse cursor into the status bar, we now
use the image's colorbar formatter (creating a hidden colorbar on-the-fly if
necessary). For example, for an image displaying the values 10,000 and 10,001,
the statusbar will now (using default settings) display the values as ``10000``
and ``10001``), whereas both values were previously displayed as ``1e+04``.
15 changes: 10 additions & 5 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,19 @@ def get_cursor_data(self, event):
return arr[i, j]

def format_cursor_data(self, data):
if np.ndim(data) == 0 and self.colorbar:
return (
"["
if np.ndim(data):
return super().format_cursor_data(data)
if not self.colorbar:
from matplotlib.figure import Figure
fig = Figure()
ax = fig.subplots()
self.colorbar = fig.colorbar(self, cax=ax)
# This will call the locator and call set_locs() on the formatter.
ax.yaxis._update_ticks()
return ("["
+ cbook.strip_math(
self.colorbar.formatter.format_data_short(data)).strip()
+ "]")
else:
return super().format_cursor_data(data)


class NonUniformImage(AxesImage):
Expand Down
17 changes: 5 additions & 12 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ def test_cursor_data():


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

fig, ax = plt.subplots()
Expand All @@ -302,14 +302,7 @@ def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
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) == data[0][0]
assert im.format_cursor_data(im.get_cursor_data(event)) \
== text_with_colorbar
== text


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