{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Manual Contour\n\nExample of displaying your own contour lines and polygons using ContourSet.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport matplotlib.cm as cm\nfrom matplotlib.contour import ContourSet\nfrom matplotlib.path import Path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Contour lines for each level are a list/tuple of polygons.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lines0 = [[[0, 0], [0, 4]]]\nlines1 = [[[2, 0], [1, 2], [1, 3]]]\nlines2 = [[[3, 0], [3, 2]], [[3, 3], [3, 4]]] # Note two lines." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Filled contours between two levels are also a list/tuple of polygons.\nPoints can be ordered clockwise or anticlockwise.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filled01 = [[[0, 0], [0, 4], [1, 3], [1, 2], [2, 0]]]\nfilled12 = [[[2, 0], [3, 0], [3, 2], [1, 3], [1, 2]], # Note two polygons.\n [[1, 4], [3, 4], [3, 3]]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n\n# Filled contours using filled=True.\ncs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone)\ncbar = fig.colorbar(cs)\n\n# Contour lines (non-filled).\nlines = ContourSet(\n ax, [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, linewidths=3)\ncbar.add_lines(lines)\n\nax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 4.5),\n title='User-specified contours')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple filled contour lines can be specified in a single list of polygon\nvertices along with a list of vertex kinds (code types) as described in the\nPath class. This is particularly useful for polygons with holes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\nfilled01 = [[[0, 0], [3, 0], [3, 3], [0, 3], [1, 1], [1, 2], [2, 2], [2, 1]]]\nM = Path.MOVETO\nL = Path.LINETO\nkinds01 = [[M, L, L, L, M, L, L, L]]\ncs = ContourSet(ax, [0, 1], [filled01], [kinds01], filled=True)\ncbar = fig.colorbar(cs)\n\nax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 3.5),\n title='User specified filled contours with holes')\n\nplt.show()" ] } ], "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 }