{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# pcolormesh\n\n`.axes.Axes.pcolormesh` allows you to generate 2D image-style plots.\nNote that it is faster than the similar `~.axes.Axes.pcolor`.\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 BoundaryNorm\nfrom matplotlib.ticker import MaxNLocator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic pcolormesh\n\nWe usually specify a pcolormesh by defining the edge of quadrilaterals and\nthe value of the quadrilateral. Note that here *x* and *y* each have one\nextra element than Z in the respective dimension.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.random.seed(19680801)\nZ = np.random.rand(6, 10)\nx = np.arange(-0.5, 10, 1) # len = 11\ny = np.arange(4.5, 11, 1) # len = 7\n\nfig, ax = plt.subplots()\nax.pcolormesh(x, y, Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Non-rectilinear pcolormesh\n\nNote that we can also specify matrices for *X* and *Y* and have\nnon-rectilinear quadrilaterals.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-0.5, 10, 1) # len = 11\ny = np.arange(4.5, 11, 1) # len = 7\nX, Y = np.meshgrid(x, y)\nX = X + 0.2 * Y # tilt the coordinates.\nY = Y + 0.3 * X\n\nfig, ax = plt.subplots()\nax.pcolormesh(X, Y, Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Centered Coordinates\n\nOften a user wants to pass *X* and *Y* with the same sizes as *Z* to\n`.axes.Axes.pcolormesh`. This is also allowed if ``shading='auto'`` is\npassed (default set by :rc:`pcolor.shading`). Pre Matplotlib 3.3,\n``shading='flat'`` would drop the last column and row of *Z*, but now gives\nan error. If this is really what you want, then simply drop the last row and\ncolumn of Z manually:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(10) # len = 10\ny = np.arange(6) # len = 6\nX, Y = np.meshgrid(x, y)\n\nfig, axs = plt.subplots(2, 1, sharex=True, sharey=True)\naxs[0].pcolormesh(X, Y, Z, vmin=np.min(Z), vmax=np.max(Z), shading='auto')\naxs[0].set_title(\"shading='auto' = 'nearest'\")\naxs[1].pcolormesh(X, Y, Z[:-1, :-1], vmin=np.min(Z), vmax=np.max(Z),\n shading='flat')\naxs[1].set_title(\"shading='flat'\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making levels using Norms\n\nShows how to combine Normalization and Colormap instances to draw\n\"levels\" in `.axes.Axes.pcolor`, `.axes.Axes.pcolormesh`\nand `.axes.Axes.imshow` type plots in a similar\nway to the levels keyword argument to contour/contourf.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# make these smaller to increase the resolution\ndx, dy = 0.05, 0.05\n\n# generate 2 2d grids for the x & y bounds\ny, x = np.mgrid[slice(1, 5 + dy, dy),\n slice(1, 5 + dx, dx)]\n\nz = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)\n\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]\nlevels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())\n\n\n# pick the desired colormap, sensible levels, and define a normalization\n# instance which takes data values and translates those into levels.\ncmap = plt.colormaps['PiYG']\nnorm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)\n\nfig, (ax0, ax1) = plt.subplots(nrows=2)\n\nim = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm)\nfig.colorbar(im, ax=ax0)\nax0.set_title('pcolormesh with levels')\n\n\n# contours are *point* based plots, so convert our bound into point\n# centers\ncf = ax1.contourf(x[:-1, :-1] + dx/2.,\n y[:-1, :-1] + dy/2., z, levels=levels,\n cmap=cmap)\nfig.colorbar(cf, ax=ax1)\nax1.set_title('contourf with levels')\n\n# adjust spacing between subplots so `ax1` title and `ax0` tick labels\n# don't overlap\nfig.tight_layout()\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.pcolormesh` / `matplotlib.pyplot.pcolormesh`\n - `matplotlib.axes.Axes.contourf` / `matplotlib.pyplot.contourf`\n - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`\n - `matplotlib.colors.BoundaryNorm`\n - `matplotlib.ticker.MaxNLocator`\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 }