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 f33913f

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

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
@@ -908,13 +908,17 @@ def get_cursor_data(self, event):
908908
return arr[i, j]
909909

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

919923

920924
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.