[ Web Proxy ]
URL:
Viewing: https://matplotlib.org/users/../examples/api/image_zcoord.html [Back]  [Original]

api example code: image_zcoord.py — Matplotlib 2.0.2 documentation
You are reading an old version of the documentation (v2.0.2). For the latest version see https://matplotlib.org/stable/
forkme_right_orange_ff7600.png [forkme_right_orange_ff7600.png]
matplotlib [matplotlib]
depsy_badge.svg [depsy_badge.svg]
Travis-CI: matplotlib.svg [matplotlib.svg]

This Page

api example code: image_zcoord.py

(Source code, png, pdf)

"""
==================================
Modifying the coordinate formatter
==================================

Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt

X = 10*np.random.rand(5, 3)

fig, ax = plt.subplots()
ax.imshow(X, interpolation='nearest')

numrows, numcols = X.shape


def format_coord(x, y):
    col = int(x + 0.5)
    row = int(y + 0.5)
    if col >= 0 and col < numcols and row >= 0 and row < numrows:
        z = X[row, col]
        return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z)
    else:
        return 'x=%1.4f, y=%1.4f' % (x, y)

ax.format_coord = format_coord
plt.show()

Keywords: python, matplotlib, pylab, example, codex (see Search examples)


Web Proxy Viewer  |  New URL  |  Original Page