Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit af18238

Browse filesBrowse files
committed
Create a hidden colorbar on-the-fly to format imshow cursor data.
... if no colorbar exists yet.
1 parent e285dde commit af18238
Copy full SHA for af18238

File tree

3 files changed

+23
-17
lines changed
Filter options

3 files changed

+23
-17
lines changed
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Improved formatting of image values under cursor when a colorbar is present
2+
```````````````````````````````````````````````````````````````````````````
3+
4+
To format image values under the mouse cursor into the status bar, we now
5+
use the image's colorbar formatter (creating a hidden colorbar on-the-fly if
6+
necessary). For example, for an image displaying the values 10,000 and 10,001,
7+
the statusbar will now (using default settings) display the values as ``10000``
8+
and ``10001``), whereas both values were previously displayed as ``1e+04``.

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+10-5Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,19 @@ def get_cursor_data(self, event):
936936
return arr[i, j]
937937

938938
def format_cursor_data(self, data):
939-
if np.ndim(data) == 0 and self.colorbar:
940-
return (
941-
"["
939+
if np.ndim(data):
940+
return super().format_cursor_data(data)
941+
if not self.colorbar:
942+
from matplotlib.figure import Figure
943+
fig = Figure()
944+
ax = fig.subplots()
945+
self.colorbar = fig.colorbar(self, cax=ax)
946+
# This will call the locator and call set_locs() on the formatter.
947+
ax.yaxis._update_ticks()
948+
return ("["
942949
+ cbook.strip_math(
943950
self.colorbar.formatter.format_data_short(data)).strip()
944951
+ "]")
945-
else:
946-
return super().format_cursor_data(data)
947952

948953

949954
class NonUniformImage(AxesImage):

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+5-12Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ def test_cursor_data():
288288

289289

290290
@pytest.mark.parametrize(
291-
"data, text_without_colorbar, text_with_colorbar", [
292-
([[10001, 10000]], "[1e+04]", "[10001]"),
293-
([[.123, .987]], "[0.123]", "[0.123]"),
291+
"data, text", [
292+
([[10001, 10000]], "[10001]"),
293+
([[.123, .987]], "[0.123]"),
294294
])
295-
def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
295+
def test_format_cursor_data(data, text):
296296
from matplotlib.backend_bases import MouseEvent
297297

298298
fig, ax = plt.subplots()
@@ -302,14 +302,7 @@ def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
302302
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
303303
assert im.get_cursor_data(event) == data[0][0]
304304
assert im.format_cursor_data(im.get_cursor_data(event)) \
305-
== text_without_colorbar
306-
307-
fig.colorbar(im)
308-
fig.canvas.draw() # This is necessary to set up the colorbar formatter.
309-
310-
assert im.get_cursor_data(event) == data[0][0]
311-
assert im.format_cursor_data(im.get_cursor_data(event)) \
312-
== text_with_colorbar
305+
== text
313306

314307

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.