{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# QuadMesh Demo\n\n`~.axes.Axes.pcolormesh` uses a `~matplotlib.collections.QuadMesh`,\na faster generalization of `~.axes.Axes.pcolor`, but with some restrictions.\n\nThis demo illustrates a bug in quadmesh with masked data.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom matplotlib import pyplot as plt\n\nn = 12\nx = np.linspace(-1.5, 1.5, n)\ny = np.linspace(-1.5, 1.5, n * 2)\nX, Y = np.meshgrid(x, y)\nQx = np.cos(Y) - np.cos(X)\nQz = np.sin(Y) + np.sin(X)\nZ = np.sqrt(X**2 + Y**2) / 5\nZ = (Z - Z.min()) / (Z.max() - Z.min())\n\n# The color array can include masked values.\nZm = np.ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)\n\nfig, axs = plt.subplots(nrows=1, ncols=3)\naxs[0].pcolormesh(Qx, Qz, Z, shading='gouraud')\naxs[0].set_title('Without masked values')\n\n# You can control the color of the masked region.\ncmap = plt.colormaps[plt.rcParams['image.cmap']].with_extremes(bad='y')\naxs[1].pcolormesh(Qx, Qz, Zm, shading='gouraud', cmap=cmap)\naxs[1].set_title('With masked values')\n\n# Or use the default, which is transparent.\naxs[2].pcolormesh(Qx, Qz, Zm, shading='gouraud')\naxs[2].set_title('With masked values')\n\nfig.tight_layout()\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\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 }