{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# TickedStroke patheffect\n\nMatplotlib's :mod:`.patheffects` can be used to alter the way paths\nare drawn at a low enough level that they can affect almost anything.\n\nThe `patheffects guide`\ndetails the use of patheffects.\n\nThe `~matplotlib.patheffects.TickedStroke` patheffect illustrated here\ndraws a path with a ticked style. The spacing, length, and angle of\nticks can be controlled.\n\nSee also the :doc:`/gallery/lines_bars_and_markers/lines_with_ticks_demo` example.\n\nSee also the :doc:`/gallery/images_contours_and_fields/contours_in_optimization_demo`\nexample.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying TickedStroke to paths\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.patches as patches\nfrom matplotlib.path import Path\nimport matplotlib.patheffects as patheffects\n\nfig, ax = plt.subplots(figsize=(6, 6))\npath = Path.unit_circle()\npatch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[\n patheffects.withTickedStroke(angle=-90, spacing=10, length=1)])\n\nax.add_patch(patch)\nax.axis('equal')\nax.set_xlim(-2, 2)\nax.set_ylim(-2, 2)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying TickedStroke to lines\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(6, 6))\nax.plot([0, 1], [0, 1], label=\"Line\",\n path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])\n\nnx = 101\nx = np.linspace(0.0, 1.0, nx)\ny = 0.3*np.sin(x*8) + 0.4\nax.plot(x, y, label=\"Curve\", path_effects=[patheffects.withTickedStroke()])\n\nax.legend()\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying TickedStroke to contour plots\n\nContour plot with objective and constraints.\nCurves generated by contour to represent a typical constraint in an\noptimization problem should be plotted with angles between zero and\n180 degrees.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(6, 6))\n\nnx = 101\nny = 105\n\n# Set up survey vectors\nxvec = np.linspace(0.001, 4.0, nx)\nyvec = np.linspace(0.001, 4.0, ny)\n\n# Set up survey matrices. Design disk loading and gear ratio.\nx1, x2 = np.meshgrid(xvec, yvec)\n\n# Evaluate some stuff to plot\nobj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2\ng1 = -(3*x1 + x2 - 5.5)\ng2 = -(x1 + 2*x2 - 4.5)\ng3 = 0.8 + x1**-3 - x2\n\ncntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],\n colors='black')\nax.clabel(cntr, fmt=\"%2.1f\", use_clabeltext=True)\n\ncg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')\ncg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])\n\ncg2 = ax.contour(x1, x2, g2, [0], colors='orangered')\ncg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])\n\ncg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')\ncg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])\n\nax.set_xlim(0, 4)\nax.set_ylim(0, 4)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Direction/side of the ticks\n\nTo change which side of the line the ticks are drawn, change the sign of the angle.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(6, 6))\nline_x = line_y = [0, 1]\nax.plot(line_x, line_y, label=\"Line\",\n path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])\n\nax.plot(line_x, line_y, label=\"Opposite side\",\n path_effects=[patheffects.withTickedStroke(spacing=7, angle=-135)])\n\nax.legend()\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 }