1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
"""
==================================
Modifying the coordinate formatter
==================================
Modify the coordinate formatter to report the image "z" value of the nearest
pixel given x and y. This functionality is built in by default; this example
just showcases how to customize the `~.axes.Axes.format_coord` function.
"""
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
X = 10*np.random.rand(5, 3)
fig, ax = plt.subplots()
ax.imshow(X)
def format_coord(x, y):
col = round(x)
row = round(y)
nrows, ncols = X.shape
if 0 <= col < ncols and 0 <= row < nrows:
z = X[row, col]
return f'x={x:1.4f}, y={y:1.4f}, z={z:1.4f}'
else:
return f'x={x:1.4f}, y={y:1.4f}'
ax.format_coord = format_coord
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.format_coord`
# - `matplotlib.axes.Axes.imshow`
|