Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow Artists to show pixel data in cursor display #3989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
34854db
Add the ability for artists to display zdata in cursor
blink1073 Jan 9, 2015
7e0d1ca
PEP8 fix
blink1073 Jan 9, 2015
97640da
Another Pep8 fix
blink1073 Jan 10, 2015
139d820
Pep8: fix trailing whitespace
blink1073 Jan 10, 2015
17e4211
Style update
blink1073 Jan 10, 2015
fcda62b
Standardize the hit test and zordering
blink1073 Jan 19, 2015
8ea9504
Fix the artist sorting behaviour
blink1073 Jan 19, 2015
02456b8
Fix syntax error
blink1073 Jan 19, 2015
641304c
Specify hitlist on axes instead of figure
blink1073 Jan 19, 2015
3f20d3a
Put get_zdata on the correct class
blink1073 Jan 19, 2015
5850150
Ignore the axes patch
blink1073 Jan 19, 2015
88b56be
Create new function to format zdata
blink1073 Jan 19, 2015
aece695
Clean up string formatting
blink1073 Jan 19, 2015
07745c8
Add a whats new blurb.
blink1073 Jan 20, 2015
7626394
Add a test for image cursor data
blink1073 Feb 21, 2015
7b0b6b1
Rename to get_pixel_data and return raw data
blink1073 Feb 21, 2015
f5ff73f
Update the whats new
blink1073 Feb 21, 2015
ea565c6
Fix name of method
blink1073 Feb 21, 2015
1429206
Fix default pixel data and handle no pixel data
blink1073 Feb 21, 2015
e62b0e3
Update test for new method name
blink1073 Feb 21, 2015
34b4df4
Simplify transform
blink1073 Feb 21, 2015
8f266a1
Update display
blink1073 Feb 21, 2015
70983ff
Simplify format_pixel_data
blink1073 Mar 8, 2015
cd0e52b
Update name to cursor_data
blink1073 Mar 8, 2015
8ce3a6f
Move artist sort to within if block
blink1073 Mar 10, 2015
bac8dff
Fix method name in cursor_test
blink1073 Mar 17, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/users/whats_new/2015-01-19_cursor_pixel_data.rst
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
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
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
Contributor

Choose a reason for hiding this comment

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

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
Member Author

Choose a reason for hiding this comment

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

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