{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Rasterization for vector graphics\n\nRasterization converts vector graphics into a raster image (pixels). It can\nspeed up rendering and produce smaller files for large data sets, but comes\nat the cost of a fixed resolution.\n\nWhether rasterization should be used can be specified per artist.  This can be\nuseful to reduce the file size of large artists, while maintaining the\nadvantages of vector graphics for other artists such as the Axes\nand text.  For instance a complicated `~.Axes.pcolormesh` or\n`~.Axes.contourf` can be made significantly simpler by rasterizing.\nSetting rasterization only affects vector backends such as PDF, SVG, or PS.\n\nRasterization is disabled by default. There are two ways to enable it, which\ncan also be combined:\n\n- Set `~.Artist.set_rasterized` on individual artists, or use the keyword\n  argument *rasterized* when creating the artist.\n- Set `.Axes.set_rasterization_zorder` to rasterize all artists with a zorder\n  less than the given value.\n\nThe storage size and the resolution of the rasterized artist is determined by\nits physical size and the value of the ``dpi`` parameter passed to\n`~.Figure.savefig`.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>The image of this example shown in the HTML documentation is not a vector\n    graphic. Therefore, it cannot illustrate the rasterization effect. Please\n    run this example locally and check the generated graphics files.</p></div>\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\n\nd = np.arange(100).reshape(10, 10)  # the values to be color-mapped\nx, y = np.meshgrid(np.arange(11), np.arange(11))\n\ntheta = 0.25*np.pi\nxx = x*np.cos(theta) - y*np.sin(theta)  # rotate x by -theta\nyy = x*np.sin(theta) + y*np.cos(theta)  # rotate y by -theta\n\nfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, layout=\"constrained\")\n\n# pcolormesh without rasterization\nax1.set_aspect(1)\nax1.pcolormesh(xx, yy, d)\nax1.set_title(\"No Rasterization\")\n\n# pcolormesh with rasterization; enabled by keyword argument\nax2.set_aspect(1)\nax2.set_title(\"Rasterization\")\nax2.pcolormesh(xx, yy, d, rasterized=True)\n\n# pcolormesh with an overlaid text without rasterization\nax3.set_aspect(1)\nax3.pcolormesh(xx, yy, d)\nax3.text(0.5, 0.5, \"Text\", alpha=0.2,\n         va=\"center\", ha=\"center\", size=50, transform=ax3.transAxes)\nax3.set_title(\"No Rasterization\")\n\n# pcolormesh with an overlaid text without rasterization; enabled by zorder.\n# Setting the rasterization zorder threshold to 0 and a negative zorder on the\n# pcolormesh rasterizes it. All artists have a non-negative zorder by default,\n# so they (e.g. the text here) are not affected.\nax4.set_aspect(1)\nm = ax4.pcolormesh(xx, yy, d, zorder=-10)\nax4.text(0.5, 0.5, \"Text\", alpha=0.2,\n         va=\"center\", ha=\"center\", size=50, transform=ax4.transAxes)\nax4.set_rasterization_zorder(0)\nax4.set_title(\"Rasterization z$<-10$\")\n\n# Save files in pdf and eps format\nplt.savefig(\"test_rasterization.pdf\", dpi=150)\nplt.savefig(\"test_rasterization.eps\", dpi=150)\n\nif not plt.rcParams[\"text.usetex\"]:\n    plt.savefig(\"test_rasterization.svg\", dpi=150)\n    # svg backend currently ignores the dpi"
      ]
    },
    {
      "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.artist.Artist.set_rasterized`\n   - `matplotlib.axes.Axes.set_rasterization_zorder`\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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}