8000 Create a hidden colorbar on-the-fly to format imshow cursor data. · matplotlib/matplotlib@f8463d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit f8463d7

Browse files
committed
Create a hidden colorbar on-the-fly to format imshow cursor data.
... if no colorbar exists yet.
1 parent bc4c4cc commit f8463d7

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Improved formatting of image values under cursor when a colorbar is present
22
```````````````````````````````````````````````````````````````````````````
33

4-
When a colorbar is present, its formatter is now used to format the image
5-
values under the mouse cursor in the status bar. For example, for an image
6-
displaying the values 10,000 and 10,001, the statusbar will now (using default
7-
settings) display the values as ``10000`` and ``10001``), whereas both values
8-
were previously displayed as ``1e+04``.
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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,14 +933,17 @@ def get_cursor_data(self, event):
933933
return arr[i, j]
934934

935935
def format_cursor_data(self, data):
936-
if self.colorbar:
937-
return (
938-
"["
936+
if not self.colorbar:
937+
from matplotlib.figure import Figure
938+
fig = Figure()
939+
ax = fig.subplots()
940+
self.colorbar = fig.colorbar(self, cax=ax)
941+
# This will call the locator and call set_locs() on the formatter.
942+
ax.yaxis._update_ticks()
943+
return ("["
939944
+ cbook.strip_math(
940945
self.colorbar.formatter.format_data_short(data)).strip()
941946
+ "]")
942-
else:
943-
return super().format_cursor_data(data)
944947

945948

946949
class NonUniformImage(AxesImage):

lib/matplotlib/tests/test_image.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ def test_cursor_data():
293293

294294

295295
@pytest.mark.parametrize(
296-
"data, text_without_colorbar, text_with_colorbar", [
297-
([[10001, 10000]], "[1e+04]", "[10001]"),
298-
([[.123, .987]], "[0.123]", "[0.123]"),
296+
"data, text", [
297+
([[10001, 10000]], "[10001]"),
298+
([[.123, .987]], "[0.123]"),
299299
])
300-
def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
300+
def test_format_cursor_data(data, text):
301301
from matplotlib.backend_bases import MouseEvent
302302

303303
fig, ax = plt.subplots()
@@ -307,14 +307,7 @@ def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar):
307307
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
308308
assert im.get_cursor_data(event) == data[0][0]
309309
assert im.format_cursor_data(im.get_cursor_data(event)) \
310-
== text_without_colorbar
311-
312-
fig.colorbar(im)
313-
fig.canvas.draw() # This is necessary to set up the colorbar formatter.
314-
315-
assert im.get_cursor_data(event) == data[0][0]
316-
assert im.format_cursor_data(im.get_cursor_data(event)) \
317-
== text_with_colorbar
310+
== text
318311

319312

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

0 commit comments

Comments
 (0)
0