.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/misc/tickedstroke_demo.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_misc_tickedstroke_demo.py: ======================= TickedStroke patheffect ======================= Matplotlib's :mod:`.patheffects` can be used to alter the way paths are drawn at a low enough level that they can affect almost anything. The :ref:`patheffects guide` details the use of patheffects. The `~matplotlib.patheffects.TickedStroke` patheffect illustrated here draws a path with a ticked style. The spacing, length, and angle of ticks can be controlled. See also the :doc:`/gallery/lines_bars_and_markers/lines_with_ticks_demo` example. See also the :doc:`/gallery/images_contours_and_fields/contours_in_optimization_demo` example. .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 26-28 Applying TickedStroke to paths ============================== .. GENERATED FROM PYTHON SOURCE LINES 28-44 .. code-block:: Python import matplotlib.patches as patches from matplotlib.path import Path import matplotlib.patheffects as patheffects fig, ax = plt.subplots(figsize=(6, 6)) path = Path.unit_circle() patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[ patheffects.withTickedStroke(angle=-90, spacing=10, length=1)]) ax.add_patch(patch) ax.axis('equal') ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) plt.show() .. image-sg:: /gallery/misc/images/sphx_glr_tickedstroke_demo_001.png :alt: tickedstroke demo :srcset: /gallery/misc/images/sphx_glr_tickedstroke_demo_001.png, /gallery/misc/images/sphx_glr_tickedstroke_demo_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-47 Applying TickedStroke to lines ============================== .. GENERATED FROM PYTHON SOURCE LINES 47-60 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 6)) ax.plot([0, 1], [0, 1], label="Line", path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)]) nx = 101 x = np.linspace(0.0, 1.0, nx) y = 0.3*np.sin(x*8) + 0.4 ax.plot(x, y, label="Curve", path_effects=[patheffects.withTickedStroke()]) ax.legend() plt.show() .. image-sg:: /gallery/misc/images/sphx_glr_tickedstroke_demo_002.png :alt: tickedstroke demo :srcset: /gallery/misc/images/sphx_glr_tickedstroke_demo_002.png, /gallery/misc/images/sphx_glr_tickedstroke_demo_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-68 Applying TickedStroke to contour plots ====================================== Contour plot with objective and constraints. Curves generated by contour to represent a typical constraint in an optimization problem should be plotted with angles between zero and 180 degrees. .. GENERATED FROM PYTHON SOURCE LINES 68-104 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 6)) nx = 101 ny = 105 # Set up survey vectors xvec = np.linspace(0.001, 4.0, nx) yvec = np.linspace(0.001, 4.0, ny) # Set up survey matrices. Design disk loading and gear ratio. x1, x2 = np.meshgrid(xvec, yvec) # Evaluate some stuff to plot obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2 g1 = -(3*x1 + x2 - 5.5) g2 = -(x1 + 2*x2 - 4.5) g3 = 0.8 + x1**-3 - x2 cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16], colors='black') ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True) cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown') cg1.set(path_effects=[patheffects.withTickedStroke(angle=135)]) cg2 = ax.contour(x1, x2, g2, [0], colors='orangered') cg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)]) cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue') cg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)]) ax.set_xlim(0, 4) ax.set_ylim(0, 4) plt.show() .. image-sg:: /gallery/misc/images/sphx_glr_tickedstroke_demo_003.png :alt: tickedstroke demo :srcset: /gallery/misc/images/sphx_glr_tickedstroke_demo_003.png, /gallery/misc/images/sphx_glr_tickedstroke_demo_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 105-109 Direction/side of the ticks =========================== To change which side of the line the ticks are drawn, change the sign of the angle. .. GENERATED FROM PYTHON SOURCE LINES 109-120 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 6)) line_x = line_y = [0, 1] ax.plot(line_x, line_y, label="Line", path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)]) ax.plot(line_x, line_y, label="Opposite side", path_effects=[patheffects.withTickedStroke(spacing=7, angle=-135)]) ax.legend() plt.show() .. image-sg:: /gallery/misc/images/sphx_glr_tickedstroke_demo_004.png :alt: tickedstroke demo :srcset: /gallery/misc/images/sphx_glr_tickedstroke_demo_004.png, /gallery/misc/images/sphx_glr_tickedstroke_demo_004_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.770 seconds) .. _sphx_glr_download_gallery_misc_tickedstroke_demo.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tickedstroke_demo.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tickedstroke_demo.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_