{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# pcolor images\n\n`~.Axes.pcolor` generates 2D image-style plots, as illustrated below.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.colors import LogNorm\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A simple pcolor demo\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Z = np.random.rand(6, 10)\n\nfig, (ax0, ax1) = plt.subplots(2, 1)\n\nc = ax0.pcolor(Z)\nax0.set_title('default: no edges')\n\nc = ax1.pcolor(Z, edgecolors='k', linewidths=4)\nax1.set_title('thick edges')\n\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparing pcolor with similar functions\n\nDemonstrates similarities between `~.axes.Axes.pcolor`,\n`~.axes.Axes.pcolormesh`, `~.axes.Axes.imshow` and\n`~.axes.Axes.pcolorfast` for drawing quadrilateral grids.\nNote that we call ``imshow`` with ``aspect=\"auto\"`` so that it doesn't force\nthe data pixels to be square (the default is ``aspect=\"equal\"``).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# make these smaller to increase the resolution\ndx, dy = 0.15, 0.05\n\n# generate 2 2d grids for the x & y bounds\ny, x = np.mgrid[-3:3+dy:dy, -3:3+dx:dx]\nz = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)\n# x and y are bounds, so z should be the value *inside* those bounds.\n# Therefore, remove the last value from the z array.\nz = z[:-1, :-1]\nz_min, z_max = -abs(z).max(), abs(z).max()\n\nfig, axs = plt.subplots(2, 2)\n\nax = axs[0, 0]\nc = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)\nax.set_title('pcolor')\nfig.colorbar(c, ax=ax)\n\nax = axs[0, 1]\nc = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)\nax.set_title('pcolormesh')\nfig.colorbar(c, ax=ax)\n\nax = axs[1, 0]\nc = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,\n extent=[x.min(), x.max(), y.min(), y.max()],\n interpolation='nearest', origin='lower', aspect='auto')\nax.set_title('image (nearest, aspect=\"auto\")')\nfig.colorbar(c, ax=ax)\n\nax = axs[1, 1]\nc = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)\nax.set_title('pcolorfast')\nfig.colorbar(c, ax=ax)\n\nfig.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pcolor with a log scale\n\nThe following shows pcolor plots with a log scale.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "N = 100\nX, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))\n\n# A low hump with a spike coming out.\n# Needs to have z/colour axis on a log scale, so we see both hump and spike.\n# A linear scale only shows the spike.\nZ1 = np.exp(-X**2 - Y**2)\nZ2 = np.exp(-(X * 10)**2 - (Y * 10)**2)\nZ = Z1 + 50 * Z2\n\nfig, (ax0, ax1) = plt.subplots(2, 1)\n\nc = ax0.pcolor(X, Y, Z, shading='auto',\n norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='PuBu_r')\nfig.colorbar(c, ax=ax0)\n\nc = ax1.pcolor(X, Y, Z, cmap='PuBu_r', shading='auto')\nfig.colorbar(c, ax=ax1)\n\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.pcolor` / `matplotlib.pyplot.pcolor`\n - `matplotlib.axes.Axes.pcolormesh` / `matplotlib.pyplot.pcolormesh`\n - `matplotlib.axes.Axes.pcolorfast`\n - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`\n - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n - `matplotlib.colors.LogNorm`\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.5" } }, "nbformat": 4, "nbformat_minor": 0 }