Skip to content

Commit 9b4985e

Browse files
authored
Merge pull request #24289 from anntzer/iz
Cleanup image_zcoord example.
2 parents 77f3e1f + 575421d commit 9b4985e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

examples/images_contours_and_fields/image_zcoord.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
Modifying the coordinate formatter
44
==================================
55
6-
Modify the coordinate formatter to report the image "z"
7-
value of the nearest pixel given x and y.
8-
This functionality is built in by default, but it
9-
is still useful to show how to customize the
10-
`~.axes.Axes.format_coord` function.
6+
Modify the coordinate formatter to report the image "z" value of the nearest
7+
pixel given x and y. This functionality is built in by default; this example
8+
just showcases how to customize the `~.axes.Axes.format_coord` function.
119
"""
10+
1211
import numpy as np
1312
import matplotlib.pyplot as plt
1413

@@ -21,17 +20,17 @@
2120
fig, ax = plt.subplots()
2221
ax.imshow(X)
2322

24-
numrows, numcols = X.shape
25-
2623

2724
def format_coord(x, y):
28-
col = int(x + 0.5)
29-
row = int(y + 0.5)
30-
if 0 <= col < numcols and 0 <= row < numrows:
25+
col = round(x)
26+
row = round(y)
27+
nrows, ncols = X.shape
28+
if 0 <= col < ncols and 0 <= row < nrows:
3129
z = X[row, col]
32-
return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z)
30+
return f'x={x:1.4f}, y={y:1.4f}, z={z:1.4f}'
3331
else:
34-
return 'x=%1.4f, y=%1.4f' % (x, y)
32+
return f'x={x:1.4f}, y={y:1.4f}'
33+
3534

3635
ax.format_coord = format_coord
3736
plt.show()

0 commit comments

Comments
 (0)