|
| 1 | +""" |
| 2 | +1D Line Styles |
| 3 | +============== |
| 4 | +
|
| 5 | +Demonstrates the line-style, opacity, and per-point marker parameters |
| 6 | +available on :meth:`~anyplotlib.figure_plots.Axes.plot` and |
| 7 | +:meth:`~anyplotlib.figure_plots.Plot1D.add_line`. |
| 8 | +
|
| 9 | +Four separate figures are shown: |
| 10 | +
|
| 11 | +1. **Linestyles** – all four dash patterns on one panel with a legend. |
| 12 | +2. **Alpha (transparency)** – two overlapping sine waves, each at 40 % opacity. |
| 13 | +3. **Marker symbols** – all seven supported symbols, each on its own offset |
| 14 | + curve. |
| 15 | +4. **Combined** – dashed + semi-transparent + circle-marker overlay on a solid |
| 16 | + primary line; demonstrates post-construction setters. |
| 17 | +""" |
| 18 | +import numpy as np |
| 19 | +import anyplotlib as vw |
| 20 | + |
| 21 | +t256 = np.linspace(0.0, 2.0 * np.pi, 256) # dense — good for dashes / alpha |
| 22 | +t24 = np.linspace(0.0, 2.0 * np.pi, 24) # sparse — makes markers visible |
| 23 | + |
| 24 | +# ── 1. Linestyles ───────────────────────────────────────────────────────────── |
| 25 | +fig1, ax1 = vw.subplots(1, 1, figsize=(580, 300)) |
| 26 | + |
| 27 | +plot1 = ax1.plot(np.sin(t256), color="#4fc3f7", linewidth=2, |
| 28 | + linestyle="solid", label="solid") |
| 29 | +plot1.add_line(np.sin(t256) + 0.6, color="#ff7043", linewidth=2, |
| 30 | + linestyle="dashed", label="dashed (\"--\")") |
| 31 | +plot1.add_line(np.sin(t256) + 1.2, color="#aed581", linewidth=2, |
| 32 | + linestyle="dotted", label="dotted (\":\")") |
| 33 | +plot1.add_line(np.sin(t256) + 1.8, color="#ce93d8", linewidth=2, |
| 34 | + linestyle="dashdot", label="dashdot (\"-.\")") |
| 35 | + |
| 36 | +fig1 |
| 37 | + |
| 38 | +# %% |
| 39 | +# The ``ls`` shorthand |
| 40 | +# -------------------- |
| 41 | +# Each linestyle has a single-character (or two-character) shorthand that |
| 42 | +# matches the matplotlib convention: |
| 43 | +# |
| 44 | +# * ``"-"`` → ``"solid"`` |
| 45 | +# * ``"--"`` → ``"dashed"`` |
| 46 | +# * ``":"`` → ``"dotted"`` |
| 47 | +# * ``"-."`` → ``"dashdot"`` |
| 48 | +# |
| 49 | +# The shorthands work on both :meth:`~anyplotlib.figure_plots.Axes.plot` |
| 50 | +# and :meth:`~anyplotlib.figure_plots.Plot1D.add_line`: |
| 51 | + |
| 52 | +fig2a, ax2a = vw.subplots(1, 1, figsize=(440, 220)) |
| 53 | +p = ax2a.plot(np.sin(t256), ls="-", color="#4fc3f7", label='ls="-"') |
| 54 | +p.add_line(np.sin(t256) + 0.8, ls="--", color="#ff7043", label='ls="--"') |
| 55 | +p.add_line(np.sin(t256) + 1.6, ls=":", color="#aed581", label='ls=":"') |
| 56 | +fig2a |
| 57 | + |
| 58 | +# %% |
| 59 | +# Alpha (opacity) |
| 60 | +# --------------- |
| 61 | +# ``alpha`` controls line opacity on a 0–1 scale. Values below 1 let |
| 62 | +# overlapping curves show through each other — useful for comparing signals |
| 63 | +# that share the same amplitude range. |
| 64 | + |
| 65 | +fig2, ax2 = vw.subplots(1, 1, figsize=(580, 300)) |
| 66 | + |
| 67 | +plot2 = ax2.plot(np.sin(t256), color="#4fc3f7", alpha=0.4, linewidth=3, |
| 68 | + label="sin α=0.4") |
| 69 | +plot2.add_line(np.cos(t256), color="#ff7043", alpha=0.4, linewidth=3, |
| 70 | + label="cos α=0.4") |
| 71 | + |
| 72 | +fig2 |
| 73 | + |
| 74 | +# %% |
| 75 | +# Marker symbols |
| 76 | +# -------------- |
| 77 | +# Set ``marker`` to place a symbol at every data point. Use a **sparse** |
| 78 | +# x-axis (few points) so the individual markers are legible. |
| 79 | +# ``markersize`` is the radius (circles / diamonds) or half-side-length |
| 80 | +# (squares, triangles) in canvas pixels. |
| 81 | +# |
| 82 | +# Supported symbols: |
| 83 | +# |
| 84 | +# * ``"o"`` — circle |
| 85 | +# * ``"s"`` — square |
| 86 | +# * ``"^"`` — triangle-up |
| 87 | +# * ``"v"`` — triangle-down |
| 88 | +# * ``"D"`` — diamond |
| 89 | +# * ``"+"`` — plus (stroke-only) |
| 90 | +# * ``"x"`` — cross (stroke-only) |
| 91 | +# * ``"none"`` — no marker (default) |
| 92 | + |
| 93 | +SYMBOLS = [ |
| 94 | + ("o", "#4fc3f7"), |
| 95 | + ("s", "#ff7043"), |
| 96 | + ("^", "#aed581"), |
| 97 | + ("v", "#ce93d8"), |
| 98 | + ("D", "#ffcc02"), |
| 99 | + ("+", "#80cbc4"), |
| 100 | + ("x", "#ef9a9a"), |
| 101 | +] |
| 102 | + |
| 103 | +fig3, ax3 = vw.subplots(1, 1, figsize=(580, 380)) |
| 104 | + |
| 105 | +plot3 = ax3.plot( |
| 106 | + np.sin(t24) + (0 - 3) * 0.9, |
| 107 | + color=SYMBOLS[0][1], linewidth=1.5, |
| 108 | + marker=SYMBOLS[0][0], markersize=5, |
| 109 | + label=f'marker="{SYMBOLS[0][0]}"', |
| 110 | +) |
| 111 | +for i, (sym, col) in enumerate(SYMBOLS[1:], 1): |
| 112 | + plot3.add_line( |
| 113 | + np.sin(t24) + (i - 3) * 0.9, |
| 114 | + color=col, linewidth=1.5, |
| 115 | + marker=sym, markersize=5, |
| 116 | + label=f'marker="{sym}"', |
| 117 | + ) |
| 118 | + |
| 119 | +fig3 |
| 120 | + |
| 121 | +# %% |
| 122 | +# Combined — linestyle + alpha + marker |
| 123 | +# -------------------------------------- |
| 124 | +# All three style parameters can be combined freely on the same line or on |
| 125 | +# separate overlay lines. |
| 126 | + |
| 127 | +fig4, ax4 = vw.subplots(1, 1, figsize=(580, 300)) |
| 128 | + |
| 129 | +# Dense solid primary line |
| 130 | +plot4 = ax4.plot(np.sin(t256), color="#4fc3f7", linewidth=2, |
| 131 | + label="sin (solid)") |
| 132 | + |
| 133 | +# Sparse dashed overlay with circle markers and reduced opacity |
| 134 | +plot4.add_line(np.cos(t24), color="#ff7043", linewidth=2, |
| 135 | + linestyle="dashed", alpha=0.75, |
| 136 | + marker="o", markersize=5, |
| 137 | + label="cos (dashed, α=0.75, marker='o')") |
| 138 | + |
| 139 | +fig4 |
| 140 | + |
| 141 | +# %% |
| 142 | +# Post-construction setters |
| 143 | +# ------------------------- |
| 144 | +# Every primary-line style property has a matching setter method. These |
| 145 | +# mutate ``_state`` and push the change to the canvas immediately — no |
| 146 | +# need to recreate the panel. |
| 147 | + |
| 148 | +fig5, ax5 = vw.subplots(1, 1, figsize=(440, 220)) |
| 149 | +plot5 = ax5.plot(np.sin(t256), color="#4fc3f7", linewidth=1.5) |
| 150 | + |
| 151 | +# Change style via setters |
| 152 | +plot5.set_color("#ff7043") |
| 153 | +plot5.set_linewidth(2.5) |
| 154 | +plot5.set_linestyle("dashdot") # equivalent: plot5.set_linestyle("-.") |
| 155 | +plot5.set_alpha(0.8) |
| 156 | +plot5.set_marker("o", markersize=5) |
| 157 | + |
| 158 | +fig5 |
| 159 | + |
0 commit comments