Skip to content

Commit b014e94

Browse files
committed
Enhance documentation for line properties: update parameter descriptions and add usage examples for linestyle, alpha, and marker options
1 parent b5c4a47 commit b014e94

File tree

4 files changed

+337
-57
lines changed

4 files changed

+337
-57
lines changed

Examples/plot_line_styles.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+

Examples/plot_spectra1d.py

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
:meth:`~anyplotlib.figure_plots.Axes.plot`.
77
88
The spectrum contains a broad background and three Gaussian peaks.
9-
Circle markers highlight the peak positions, and a range widget
10-
selects a region of interest. Pan and zoom with the mouse; press **R**
11-
to reset the view.
9+
Circle markers highlight the peak positions using
10+
:meth:`~anyplotlib.figure_plots.Plot1D.add_points`, and a range widget
11+
selects a region of interest. A model fit is overlaid with a dashed line,
12+
and the background component is shown as a semi-transparent dotted curve with
13+
diamond markers.
14+
15+
Pan and zoom with the mouse; press **R** to reset the view.
1216
"""
1317
import numpy as np
1418
import anyplotlib as vw
@@ -21,20 +25,23 @@
2125
def gaussian(x, mu, sigma, amp):
2226
return amp * np.exp(-0.5 * ((x - mu) / sigma) ** 2)
2327

28+
background = 0.4 * np.exp(-0.08 * (energy - 280))
29+
2430
# Background + three peaks (C 1s region)
2531
spectrum = (
26-
0.4 * np.exp(-0.08 * (energy - 280)) # exponential background
32+
background
2733
+ gaussian(energy, 284.8, 0.4, 1.0) # C–C / C–H
2834
+ gaussian(energy, 286.2, 0.4, 0.35) # C–O
2935
+ gaussian(energy, 288.0, 0.4, 0.18) # C=O
3036
+ rng.normal(scale=0.015, size=len(energy))
3137
)
3238

3339
# ── Plot ──────────────────────────────────────────────────────────────────────
34-
fig, ax = vw.subplots(1, 1, figsize=(620, 320))
35-
v = ax.plot(spectrum, axes=[energy], units="eV", y_units="Intensity (a.u.)")
40+
fig, ax = vw.subplots(1, 1, figsize=(620, 340))
41+
v = ax.plot(spectrum, axes=[energy], units="eV", y_units="Intensity (a.u.)",
42+
color="#4fc3f7", linewidth=1.5)
3643

37-
# ── Peak markers ──────────────────────────────────────────────────────────────
44+
# ── Peak markers (add_points collection) ──────────────────────────────────────
3845
peak_energies = np.array([284.8, 286.2, 288.0])
3946
peak_offsets = np.column_stack([
4047
peak_energies,
@@ -50,17 +57,56 @@ def gaussian(x, mu, sigma, amp):
5057
fig
5158

5259
# %%
53-
# Overlay a second spectrum
54-
# -------------------------
60+
# Overlay a model fit — linestyle and alpha
61+
# -----------------------------------------
5562
# Use :meth:`~anyplotlib.figure_plots.Plot1D.add_line` to overlay additional
56-
# curves — useful for comparing reference spectra or fits.
63+
# curves. Here the noiseless model fit is drawn as a **dashed** line so it
64+
# is visually distinct from the noisy measured spectrum. The ``alpha``
65+
# parameter makes the fit semi-transparent so the data underneath remains
66+
# readable.
67+
#
68+
# The y-axis range is expanded automatically to accommodate any overlay line
69+
# whose values fall outside the current bounds.
5770

5871
fit = (
59-
0.4 * np.exp(-0.08 * (energy - 280))
72+
background
6073
+ gaussian(energy, 284.8, 0.4, 1.0)
6174
+ gaussian(energy, 286.2, 0.4, 0.35)
6275
+ gaussian(energy, 288.0, 0.4, 0.18)
6376
)
64-
v.add_line(fit, x_axis=energy, color="#ffcc00", linewidth=1.5, label="fit")
77+
v.add_line(fit, x_axis=energy,
78+
color="#ffcc00", linewidth=2.0,
79+
linestyle="dashed", alpha=0.85,
80+
label="fit")
81+
82+
fig
83+
84+
# %%
85+
# Background component — dotted line with markers
86+
# ------------------------------------------------
87+
# Draw the exponential background component as a **dotted** curve. Passing
88+
# ``marker="D"`` places a diamond at every data point (useful when the line
89+
# is sparse or when you want to emphasise individual sample positions).
90+
# ``markersize`` controls the half-size of the symbol in pixels.
91+
92+
# Sub-sample to keep the marker plot readable
93+
step = 32
94+
v.add_line(background[::step], x_axis=energy[::step],
95+
color="#ce93d8", linewidth=1.2,
96+
linestyle="dotted", alpha=0.9,
97+
marker="D", markersize=3,
98+
label="background")
99+
100+
fig
101+
102+
# %%
103+
# Post-construction setters
104+
# -------------------------
105+
# All primary-line style properties can be changed after the panel is created
106+
# without rebuilding it. This is useful in interactive notebooks where you
107+
# want to tweak the appearance of the main trace.
108+
109+
v.set_alpha(0.9) # slightly reduce primary-line opacity
110+
v.set_linewidth(2.0) # thicker stroke for the main spectrum
65111

66112
fig

anyplotlib/figure_plots.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,27 +1544,39 @@ class Plot1D:
15441544
Set at construction time via :meth:`Axes.plot` or updated afterwards
15451545
with the corresponding setter:
15461546
1547-
+-----------------+---------------+-----------------------------------------------+
1548-
| Parameter | Default | Description |
1549-
+=================+===============+===============================================+
1550-
| ``color`` | ``"#4fc3f7"`` | CSS colour string for the primary line. |
1551-
+-----------------+---------------+-----------------------------------------------+
1552-
| ``linewidth`` | ``1.5`` | Stroke width in pixels. |
1553-
+-----------------+---------------+-----------------------------------------------+
1554-
| ``linestyle`` | ``"solid"`` | ``"solid"``, ``"dashed"``, ``"dotted"``, |
1555-
| | | ``"dashdot"`` (or shorthands ``-``, ``--``, |
1556-
| | | ``:``, ``-.``). |
1557-
+-----------------+---------------+-----------------------------------------------+
1558-
| ``alpha`` | ``1.0`` | Line opacity (0–1). |
1559-
+-----------------+---------------+-----------------------------------------------+
1560-
| ``marker`` | ``"none"`` | Per-point symbol: ``"o"``, ``"s"``, |
1561-
| | | ``"^"``, ``"v"``, ``"D"``, ``"+"``, ``"x"``, |
1562-
| | | or ``"none"``. |
1563-
+-----------------+---------------+-----------------------------------------------+
1564-
| ``markersize`` | ``4.0`` | Marker radius / half-side in pixels. |
1565-
+-----------------+---------------+-----------------------------------------------+
1566-
| ``label`` | ``""`` | Legend label (empty = no legend entry). |
1567-
+-----------------+---------------+-----------------------------------------------+
1547+
.. list-table::
1548+
:header-rows: 1
1549+
:widths: 18 18 64
1550+
1551+
* - Parameter
1552+
- Default
1553+
- Description
1554+
* - ``color``
1555+
- ``"#4fc3f7"``
1556+
- CSS colour string for the primary line.
1557+
* - ``linewidth``
1558+
- ``1.5``
1559+
- Stroke width in pixels.
1560+
* - ``linestyle`` (``ls``)
1561+
- ``"solid"``
1562+
- Dash pattern: ``"solid"``, ``"dashed"``, ``"dotted"``,
1563+
``"dashdot"``. Shorthands ``"-"``, ``"--"``, ``":"``,
1564+
``"-."`` also accepted.
1565+
* - ``alpha``
1566+
- ``1.0``
1567+
- Line opacity (0 = transparent, 1 = fully opaque).
1568+
* - ``marker``
1569+
- ``"none"``
1570+
- Per-point symbol: ``"o"`` (circle), ``"s"`` (square),
1571+
``"^"``/``"v"`` (triangles), ``"D"`` (diamond),
1572+
``"+"``/``"x"`` (stroke-only), or ``"none"``.
1573+
* - ``markersize``
1574+
- ``4.0``
1575+
- Marker radius / half-side in pixels.
1576+
* - ``label``
1577+
- ``""``
1578+
- Legend label (empty string = no legend entry).
1579+
15681580
15691581
Public API summary
15701582
------------------

0 commit comments

Comments
 (0)