Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Create new function to format zdata
  • Loading branch information
Steven Silvester
Steven Silvester committed Mar 17, 2015
commit 88b56be198c4c47fc5976c17d105d91b3ff6e53c
15 changes: 15 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,21 @@ def get_zdata(self, event):
"""
return ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you want to use a string for this? Shouldn't "data" always be numeric?

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.

This allows the Artist to control the display of numbers, similar to the existing ax.format_coord.

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.

I agree with @efiring on this one. I can think of a lot of use cases (e.g. interactive sampling) where it would be very convenient to access a "raw" value. Returning a numerical value would make this method useful for a lot more than just display.

You already have a separate method for the formatting, so it's only slightly more verbose to flip the function calls on their head and make the displayed string using artist.format_zdata(artist.get_zdata()). In my opinion, this would be a better approach overall.

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.

Ha! I originally meant to do that...


def format_zdata(self, z):
"""
Return *z* string formatted.
"""
try:
is_int = isinstance(z[0], int)
except (TypeError, IndexError):
is_int = isinstance(z, int)
z = [z]
if is_int:
z = ', '.join('{:0}'.format(item) for item in z)
else:
z = ', '.join('{:0.3g}'.format(item) for item in z)
return z


class ArtistInspector(object):
"""
Expand Down
6 changes: 1 addition & 5 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,7 @@ def get_zdata(self, event):
y, x = event.ydata, event.xdata
i, j = trans.transform_point([y, x]).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
return 'z=%s' % self.format_zdata(z)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should it be the image's job to say "z=" - and if so, should it say pixel-value=?



class NonUniformImage(AxesImage):
Expand Down