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

Skip to content

Fix format_cursor_data with nans. #21145

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 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,11 +1274,18 @@ def format_cursor_data(self, data):
# Artist.format_cursor_data would always have precedence over
# ScalarMappable.format_cursor_data.
n = self.cmap.N
# Midpoints of neighboring color intervals.
neighbors = self.norm.inverse(
(int(self.norm(data) * n) + np.array([0, 1])) / n)
delta = abs(neighbors - data).max()
return "[{:-#.{}g}]".format(data, cbook._g_sig_digits(data, delta))
if np.ma.getmask(data):
return "[]"
normed = self.norm(data)
if np.isfinite(normed):
# Midpoints of neighboring color intervals.
neighbors = self.norm.inverse(
(int(self.norm(data) * n) + np.array([0, 1])) / n)
delta = abs(neighbors - data).max()
g_sig_digits = cbook._g_sig_digits(data, delta)
else:
g_sig_digits = 3 # Consistent with default below.
return "[{:-#.{}g}]".format(data, g_sig_digits)
else:
try:
data[0]
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def test_cursor_data():
"data, text", [
([[10001, 10000]], "[10001.000]"),
([[.123, .987]], "[0.123]"),
([[np.nan, 1, 2]], "[]"),
])
def test_format_cursor_data(data, text):
from matplotlib.backend_bases import MouseEvent
Expand All @@ -349,7 +350,6 @@ def test_format_cursor_data(data, text):

xdisp, ydisp = ax.transData.transform([0, 0])
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
assert im.get_cursor_data(event) == data[0][0]
assert im.format_cursor_data(im.get_cursor_data(event)) == text


Expand Down