{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Modifying the coordinate formatter\n\nModify the coordinate formatter to report the image \"z\" value of the nearest\npixel given x and y. This functionality is built in by default; this example\njust showcases how to customize the `~.axes.Axes.format_coord` function.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n\nX = 10*np.random.rand(5, 3)\n\nfig, ax = plt.subplots()\nax.imshow(X)\n\n\ndef format_coord(x, y):\n col = round(x)\n row = round(y)\n nrows, ncols = X.shape\n if 0 <= col < ncols and 0 <= row < nrows:\n z = X[row, col]\n return f'x={x:1.4f}, y={y:1.4f}, z={z:1.4f}'\n else:\n return f'x={x:1.4f}, y={y:1.4f}'\n\n\nax.format_coord = format_coord\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.format_coord`\n - `matplotlib.axes.Axes.imshow`\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }