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 56a5cc9

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

File tree

3 files changed

+18
-19
lines changed
Filter options

3 files changed

+18
-19
lines changed
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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 ``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``.
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 one on-the-fly if
6+
necessary). For example, for an image displaying the values 10,000 and
7+
10,001, the statusbar will now (using default settings) display the values as
8+
``0.0+1e4`` and ``1.0+1e4`` (or ``10000.0`` and ``10001.0`` if the offset-text
9+
is disabled on colorbars), whereas both values were previously displayed as
10+
``1e+04``.

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+11-7Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,13 +907,17 @@ def get_cursor_data(self, event):
907907
return arr[i, j]
908908

909909
def format_cursor_data(self, data):
910-
if self.colorbar:
911-
return ("["
912-
+ cbook.strip_math(self.colorbar.formatter(data))
913-
+ cbook.strip_math(self.colorbar.formatter.get_offset())
914-
+ "]")
915-
else:
916-
return super().format_cursor_data(data)
910+
if not self.colorbar:
911+
from matplotlib.figure import Figure
912+
fig = Figure()
913+
ax = fig.subplots()
914+
self.colorbar = fig.colorbar(self, cax=ax)
915+
# This will call the locator and call set_locs() on the formatter.
916+
list(ax.yaxis.iter_ticks())
917+
return ("["
918+
+ cbook.strip_math(self.colorbar.formatter(data))
919+
+ cbook.strip_math(self.colorbar.formatter.get_offset())
920+
+ "]")
917921

918922

919923
class NonUniformImage(AxesImage):

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
-6Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,6 @@ def test_format_cursor_data():
270270

271271
xdisp, ydisp = ax.transData.transform_point([0, 0])
272272
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
273-
assert im.get_cursor_data(event) == 10000
274-
assert im.format_cursor_data(im.get_cursor_data(event)) == "[1e+04]"
275-
276-
fig.colorbar(im)
277-
fig.canvas.draw() # This is necessary to set up the colorbar formatter.
278-
279273
assert im.get_cursor_data(event) == 10000
280274
assert im.format_cursor_data(im.get_cursor_data(event)) == "[0.0+1e4]"
281275

0 commit comments

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