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 1 commit
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
Next Next commit
Add the ability for artists to display zdata in cursor
  • Loading branch information
blink1073 committed Mar 17, 2015
commit 34854dbcff97f9199afce0a9492fb04ff63bccdd
6 changes: 6 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ def get_setters(self):

return [prop for prop, target in self._get_setters_and_targets()]

def get_zdata(self, event):
"""
Get the zdata for a given event, as a string message
"""
return ''

def is_alias(self, o):
"""
Return *True* if method object *o* is an alias for another
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,12 @@ def mouse_move(self, event):
except (ValueError, OverflowError):
pass
else:
ax = event.inaxes
artists = ax.artists + ax.images + ax.lines
Copy link
Member

Choose a reason for hiding this comment

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

Should also look at ax.collections (that is where things like scatter live) and maybe ax.patches

Copy link
Member

Choose a reason for hiding this comment

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

and the order should be ax.collections + ax.patches + ax.lines + ax.artists + ax.images to match the order used for sorting out the rendering order in Axes.draw (around https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes/_base.py#L1993 images are special cased around line 2030).

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Right, but the ordering is a stable sort so ties are broken by which was in the list first. This is the source of at least one un-fixable (with out major overhaul of the internal data structure, see #3944) bug ( #409 ).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok then. So how to handle composite images? It does not seem feasible to recreate the composite every time.

Copy link
Member Author

Choose a reason for hiding this comment

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

The AxesImage could composite the specific pixel on demand.

Copy link
Member

Choose a reason for hiding this comment

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

I think to first order, just ignore the compositing and use the top image.

The in-place compositing is only for some of the vector backends which can't do it them selves (ex pdf) to try and save space (so you don't have multiple overlapping images) but it leads to other bugs as all images get smashed to the same zorder relative to the other lines etc. In Agg (which backs all but 3 (gtkcairo, macsox and windows) of the interactive backends) the whole if _do_composite block is skipped and the compsiting is taken care of at the c++ layer.

artists = [a for a in artists if a.contains(event)[0]]
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.

artist = artists[np.argmax([a.zorder for a in artists])]
s += artist.get_zdata(event)
if len(self.mode):
self.set_message('%s, %s' % (self.mode, s))
else:
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,23 @@ def get_extent(self):
else:
return (-0.5, numcols-0.5, -0.5, numrows-0.5)

def get_zdata(self, event):
"""Get the zdata message 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.BboxTransformFrom(data_extent) +\
mtransforms.BboxTransformTo(array_extent)
i, j = trans.transform_point([event.ydata, event.xdata]).astype(int)
z = arr[i, j]
if z.size > 1:
# Override default numpy formatting for this specific case. Bad idea?
z = ', '.join('{:0.3g}'.format(item) for item in z)
return 'z=%s' % z


class NonUniformImage(AxesImage):
def __init__(self, ax, **kwargs):
Expand Down