FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow Artists to show pixel data in cursor display by blink1073 · Pull Request #3989 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Allow Artists to Display Pixel Data in Cursor
---------------------------------------------

Adds `get_pixel_data` and `format_pixel_data` methods to artists
which can be used to add zdata to the cursor display
in the status bar. Also adds an implementation for Images.
16 changes: 16 additions & 0 deletions lib/matplotlib/artist.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,22 @@ def matchfunc(x):
artists.append(self)
return artists

def get_cursor_data(self, event):
"""
Get the cursor data for a given event.
"""
return None

def format_cursor_data(self, data):
"""
Return *cursor data* string formatted.
"""
try:
data[0]
except (TypeError, IndexError):
data = [data]
return ', '.join('{:0.3g}'.format(item) for item in data)


class ArtistInspector(object):
"""
Expand Down
20 changes: 12 additions & 8 deletions lib/matplotlib/backend_bases.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1694,15 +1694,9 @@ def onRemove(self, ev):

canvas.mpl_connect('mouse_press_event',canvas.onRemove)
"""
def sort_artists(artists):
# This depends on stable sort and artists returned
# from get_children in z order.
L = [(h.zorder, h) for h in artists]
L.sort()
return [h for zorder, h in L]

# Find the top artist under the cursor
under = sort_artists(self.figure.hitlist(ev))
under = self.figure.hitlist(ev)
under.sort(key=lambda x: x.zorder)
h = None
if under:
h = under[-1]
Expand Down Expand Up @@ -2800,6 +2794,16 @@ def mouse_move(self, event):
except (ValueError, OverflowError):
pass
else:
artists = event.inaxes.hitlist(event)
if event.inaxes.patch in artists:
artists.remove(event.inaxes.patch)

if artists:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Something seems odd about checking if artists is empty after sorting the list. I know (provided artists is a list) it won't error out, but still seems to me that the sort should go ahead and move within the if block.

Also, +1 for removing ye olden style DSU in favor of sort(key=).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good call, done.

artists.sort(key=lambda x: x.zorder)
a = artists[-1]
data = a.get_cursor_data(event)
if data is not None:
s += ' [%s]' % a.format_cursor_data(data)
if len(self.mode):
self.set_message('%s, %s' % (self.mode, s))
else:
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,20 @@ def get_extent(self):
else:
return (-0.5, numcols-0.5, -0.5, numrows-0.5)

def get_cursor_data(self, event):
"""Get the cursor data for a given event"""
xmin, xmax, ymin, ymax = self.get_extent()
if self.origin == 'upper':
ymin, ymax = ymax, ymin
arr = self.get_array()
data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]])
array_extent = mtransforms.Bbox([[0, 0], arr.shape[:2]])
trans = mtransforms. BboxTransform(boxin=data_extent,
boxout=array_extent)
y, x = event.ydata, event.xdata
i, j = trans.transform_point([y, x]).astype(int)
return arr[i, j]


class NonUniformImage(AxesImage):
def __init__(self, ax, **kwargs):
Expand Down
33 changes: 33 additions & 0 deletions lib/matplotlib/tests/test_image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,39 @@ def test_imsave_color_alpha():

assert_array_equal(data, arr_buf)


@cleanup
def test_cursor_data():
from matplotlib.backend_bases import MouseEvent

fig, ax = plt.subplots()
im = ax.imshow(np.arange(100).reshape(10, 10), origin='upper')

x, y = 4, 4
xdisp, ydisp = ax.transData.transform_point([x, y])

event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == 44

ax.clear()
im = ax.imshow(np.arange(100).reshape(10, 10), origin='lower')

x, y = 4, 4
xdisp, ydisp = ax.transData.transform_point([x, y])

event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == 44

fig, ax = plt.subplots()
im = ax.imshow(np.arange(100).reshape(10, 10), extent=[0, 0.5, 0, 0.5])

x, y = 0.25, 0.25
xdisp, ydisp = ax.transData.transform_point([x, y])

event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == 55


@image_comparison(baseline_images=['image_clip'])
def test_image_clip():
from math import pi
Expand Down

Back | FazBrowse Home | New Git URL