8000 Add the ability for artists to display zdata in cursor · matplotlib/matplotlib@34854db · GitHub
[go: up one dir, main page]

Skip to content

Commit 34854db

Browse files
committed
Add the ability for artists to display zdata in cursor
1 parent be6a691 commit 34854db

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/matplotlib/artist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,12 @@ def get_setters(self):
10201020

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

1023+
def get_zdata(self, event):
1024+
"""
1025+
Get the zdata for a given event, as a string message
1026+
"""
1027+
return ''
1028+
10231029
def is_alias(self, o):
10241030
"""
10251031
Return *True* if method object *o* is an alias for another

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,12 @@ def mouse_move(self, event):
28002800
except (ValueError, OverflowError):
28012801
pass
28022802
else:
2803+
ax = event.inaxes
2804+
artists = ax.artists + ax.images + ax.lines
2805+
artists = [a for a in artists if a.contains(event)[0]]
2806+
if artists:
2807+
artist = artists[np.argmax([a.zorder for a in artists])]
2808+
s += artist.get_zdata(event)
28032809
if len(self.mode):
28042810
self.set_message('%s, %s' % (self.mode, s))
28052811
else:

lib/matplotlib/image.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,23 @@ def get_extent(self):
683683
else:
684684
return (-0.5, numcols-0.5, -0.5, numrows-0.5)
685685

686+
def get_zdata(self, event):
687+
"""Get the zdata message for a given event"""
688+
xmin, xmax, ymin, ymax = self.get_extent()
689+
if self.origin == 'upper':
690+
ymin, ymax = ymax, ymin
691+
arr = self.get_array()
692+
data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]])
693+
array_extent = mtransforms.Bbox([[0, 0], arr.shape[:2]])
694+
trans = mtransforms.BboxTransformFrom(data_extent) +\
695+
mtransforms.BboxTransformTo(array_extent)
696+
i, j = trans.transform_point([event.ydata, event.xdata]).astype(int)
697+
z = arr[i, j]
698+
if z.size > 1:
699+
# Override default numpy formatting for this specific case. Bad idea?
700+
z = ', '.join('{:0.3g}'.format(item) for item in z)
701+
return 'z=%s' % z
702+
686703

687704
class NonUniformImage(AxesImage):
688705
def __init__(self, ax, **kwargs):

0 commit comments

Comments
 (0)
0