def format_coord(x, y, X, extent=None):
    """Set the format of the coordinates that are read when hovering
    over on a plot.
    """
    numrows, numcols = X.shape
    if extent is not None:
        col = int((x - extent[0]) / (extent[1] - extent[0]) * numcols + 0.5)
        row = int((y - extent[3]) / (extent[2] - extent[3]) * numrows + 0.5)
    else:
        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, z=E!' % (x, y)