{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Linestyles\n\nSimple linestyles can be defined using the strings \"solid\", \"dotted\", \"dashed\"\nor \"dashdot\". More refined control can be achieved by providing a dash tuple\n``(offset, (on_off_seq))``. For example, ``(0, (3, 10, 1, 15))`` means\n(3pt line, 10pt space, 1pt line, 15pt space) with no offset, while\n``(5, (10, 3))``, means (10pt line, 3pt space), but skip the first 5pt line.\nSee also `.Line2D.set_linestyle`. The specific on/off sequences of the\n\"dotted\", \"dashed\" and \"dashdot\" styles are configurable:\n\n* :rc:`lines.dotted_pattern`\n* :rc:`lines.dashed_pattern`\n* :rc:`lines.dashdot_pattern`\n\n*Note*: The dash style can also be configured via `.Line2D.set_dashes`\nas shown in :doc:`/gallery/lines_bars_and_markers/line_demo_dash_control`\nand passing a list of dash sequences using the keyword *dashes* to the\ncycler in `property_cycle `.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nlinestyle_str = [\n ('solid', 'solid'), # Same as (0, ()) or '-'\n ('dotted', 'dotted'), # Same as ':'\n ('dashed', 'dashed'), # Same as '--'\n ('dashdot', 'dashdot')] # Same as '-.'\n\nlinestyle_tuple = [\n ('loosely dotted', (0, (1, 10))),\n ('dotted', (0, (1, 5))),\n ('densely dotted', (0, (1, 1))),\n\n ('long dash with offset', (5, (10, 3))),\n ('loosely dashed', (0, (5, 10))),\n ('dashed', (0, (5, 5))),\n ('densely dashed', (0, (5, 1))),\n\n ('loosely dashdotted', (0, (3, 10, 1, 10))),\n ('dashdotted', (0, (3, 5, 1, 5))),\n ('densely dashdotted', (0, (3, 1, 1, 1))),\n\n ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),\n ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),\n ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]\n\n\ndef plot_linestyles(ax, linestyles, title):\n X, Y = np.linspace(0, 100, 10), np.zeros(10)\n yticklabels = []\n\n for i, (name, linestyle) in enumerate(linestyles):\n ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')\n yticklabels.append(name)\n\n ax.set_title(title)\n ax.set(ylim=(-0.5, len(linestyles)-0.5),\n yticks=np.arange(len(linestyles)),\n yticklabels=yticklabels)\n ax.tick_params(left=False, bottom=False, labelbottom=False)\n ax.spines[:].set_visible(False)\n\n # For each line style, add a text annotation with a small offset from\n # the reference point (0 in Axes coords, y tick value in Data coords).\n for i, (name, linestyle) in enumerate(linestyles):\n ax.annotate(repr(linestyle),\n xy=(0.0, i), xycoords=ax.get_yaxis_transform(),\n xytext=(-6, -12), textcoords='offset points',\n color=\"blue\", fontsize=8, ha=\"right\", family=\"monospace\")\n\n\nfig, (ax0, ax1) = plt.subplots(2, 1, figsize=(7, 8), height_ratios=[1, 3],\n layout='constrained')\n\nplot_linestyles(ax0, linestyle_str[::-1], title='Named linestyles')\nplot_linestyles(ax1, linestyle_tuple[::-1], title='Parametrized linestyles')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n styling: linestyle\n purpose: reference\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 }