Note

Go to the endto download the full example code.

Modifying the coordinate formatter#

Modify the coordinate formatter to report the image "z" value of the nearestpixel given x and y. This functionality is built in by default; this examplejust showcases how to customize theformat_coord function.

importmatplotlib.pyplotaspltimportnumpyasnp# Fixing random state for reproducibilitynp.random.seed(19680801)X=10*np.random.rand(5,3)fig,ax=plt.subplots()ax.imshow(X)defformat_coord(x,y):col=round(x)row=round(y)nrows,ncols=X.shapeif0<=col<ncolsand0<=row<nrows:z=X[row,col]returnf'x={x:1.4f}, y={y:1.4f}, z={z:1.4f}'else:returnf'x={x:1.4f}, y={y:1.4f}'ax.format_coord=format_coordplt.show()
image zcoord

References

The use of the following functions, methods, classes and modules is shownin this example:

Gallery generated by Sphinx-Gallery