mgplot 0.2.30__py3-none-any.whl → 0.2.32__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mgplot/annotation_utils.py +196 -51
- mgplot/finalise_plot.py +45 -13
- mgplot/line_plot.py +8 -2
- {mgplot-0.2.30.dist-info → mgplot-0.2.32.dist-info}/METADATA +2 -2
- {mgplot-0.2.30.dist-info → mgplot-0.2.32.dist-info}/RECORD +7 -7
- {mgplot-0.2.30.dist-info → mgplot-0.2.32.dist-info}/WHEEL +1 -1
- {mgplot-0.2.30.dist-info → mgplot-0.2.32.dist-info}/licenses/LICENSE +0 -0
mgplot/annotation_utils.py
CHANGED
|
@@ -18,14 +18,25 @@ Algorithm (everything is computed in display/pixel coordinates):
|
|
|
18
18
|
label that finds such a slot keeps that local position.
|
|
19
19
|
* A near-end / at-end label that cannot be cleared within that nudge is snapped
|
|
20
20
|
across to the rightmost data point and stacked vertically with the other
|
|
21
|
-
end-of-axes labels.
|
|
21
|
+
end-of-axes labels. The stack keeps the labels in the same vertical order as
|
|
22
|
+
the line ends they belong to (isotonic placement), so annotations for lines
|
|
23
|
+
that finish close together never read out of order.
|
|
22
24
|
* An interior label that cannot be cleared keeps the candidate position with the
|
|
23
25
|
most clearance -- overlap is accepted rather than moving it off its line end.
|
|
24
26
|
* Line avoidance never applies to a label sitting at the last data point: its
|
|
25
27
|
text extends into the right margin where there is no line to clear.
|
|
28
|
+
|
|
29
|
+
Two caller-set flags change the above:
|
|
30
|
+
|
|
31
|
+
* ``force_right`` sends every label straight to the snapped stack at the
|
|
32
|
+
rightmost data point, skipping local placement altogether.
|
|
33
|
+
* ``leader_lines`` draws a thin leader from a label's anchor (its own line end)
|
|
34
|
+
to the label box, for any label that finished more than half its own height
|
|
35
|
+
away from that anchor.
|
|
26
36
|
"""
|
|
27
37
|
|
|
28
|
-
from
|
|
38
|
+
from dataclasses import dataclass
|
|
39
|
+
from math import hypot, inf
|
|
29
40
|
from typing import Any, Final
|
|
30
41
|
|
|
31
42
|
import numpy as np
|
|
@@ -46,14 +57,37 @@ _STEP_PX: Final[float] = 1.0 # search step when looking for a clear slot
|
|
|
46
57
|
_SNAP_LIMIT_PX: Final[float] = 1.0e4 # effectively-unbounded search at the right edge
|
|
47
58
|
_SPAN_SAMPLES: Final[int] = 5 # samples of a line across a label's x-span
|
|
48
59
|
_X_EPS: Final[float] = 1.0e-9 # fraction-of-span tolerance for "at the last point"
|
|
60
|
+
_LEADER_MIN_FRAC: Final[float] = 0.5 # draw a leader once displaced this many label-heights
|
|
61
|
+
_LEADER_LW: Final[float] = 0.7 # leader line width (points)
|
|
62
|
+
_LEADER_ALPHA: Final[float] = 0.5 # leader line alpha
|
|
63
|
+
_LEADER_ZORDER: Final[float] = 1.5 # under the data lines (matplotlib default zorder 2)
|
|
49
64
|
|
|
50
65
|
|
|
51
66
|
# --- registration (mirrors the period-axes stash in axis_utils)
|
|
67
|
+
@dataclass(frozen=True)
|
|
68
|
+
class AnnotationOptions:
|
|
69
|
+
"""How end-of-line labels should be laid out on an axes.
|
|
70
|
+
|
|
71
|
+
Attributes:
|
|
72
|
+
near_end: labels whose line end is within this fraction of the data
|
|
73
|
+
width from the right edge are snapped to the edge when they cannot
|
|
74
|
+
be placed locally.
|
|
75
|
+
force_right: place every label at the rightmost data point, without
|
|
76
|
+
first trying to keep it at its own line end.
|
|
77
|
+
leader_lines: draw a leader from a displaced label back to its anchor.
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
near_end: float
|
|
82
|
+
force_right: bool = False
|
|
83
|
+
leader_lines: bool = False
|
|
84
|
+
|
|
85
|
+
|
|
52
86
|
def register_annotations(
|
|
53
87
|
axes: Axes,
|
|
54
88
|
pairs: list[tuple[Text, Line2D | None]],
|
|
55
89
|
lines: list[Line2D],
|
|
56
|
-
|
|
90
|
+
options: AnnotationOptions,
|
|
57
91
|
) -> None:
|
|
58
92
|
"""Stash end-of-line annotation artists on an Axes for later de-collision.
|
|
59
93
|
|
|
@@ -61,9 +95,8 @@ def register_annotations(
|
|
|
61
95
|
axes: the Axes the labels were drawn on.
|
|
62
96
|
pairs: (label Text, the Line2D it annotates) for each annotated series.
|
|
63
97
|
lines: every data Line2D on the axes (obstacles for interior labels).
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
be placed locally.
|
|
98
|
+
options: the layout settings for this axes; the flags of repeated
|
|
99
|
+
registrations on one axes are OR-ed together.
|
|
67
100
|
|
|
68
101
|
"""
|
|
69
102
|
if not pairs:
|
|
@@ -72,7 +105,16 @@ def register_annotations(
|
|
|
72
105
|
stash["pairs"].extend(pairs)
|
|
73
106
|
known = {id(ln) for ln in stash["lines"]}
|
|
74
107
|
stash["lines"].extend(ln for ln in lines if id(ln) not in known)
|
|
75
|
-
stash
|
|
108
|
+
prior: AnnotationOptions | None = stash.get("options")
|
|
109
|
+
stash["options"] = (
|
|
110
|
+
options
|
|
111
|
+
if prior is None
|
|
112
|
+
else AnnotationOptions(
|
|
113
|
+
near_end=options.near_end,
|
|
114
|
+
force_right=options.force_right or prior.force_right,
|
|
115
|
+
leader_lines=options.leader_lines or prior.leader_lines,
|
|
116
|
+
)
|
|
117
|
+
)
|
|
76
118
|
setattr(axes, _AXES_ANNO_ATTR, stash)
|
|
77
119
|
|
|
78
120
|
|
|
@@ -146,44 +188,96 @@ def _place_interior(lab: dict[str, Any], placed: list[dict[str, Any]], line_disp
|
|
|
146
188
|
lab["yc"] = best_yc # cannot clear within +/- height: accept the best overlap
|
|
147
189
|
|
|
148
190
|
|
|
149
|
-
def
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
191
|
+
def _place_cluster_local(
|
|
192
|
+
lab: dict[str, Any],
|
|
193
|
+
placed: list[dict[str, Any]],
|
|
194
|
+
line_disps: list[LineDisp],
|
|
195
|
+
) -> bool:
|
|
196
|
+
"""Try to keep a near-end label at its own line end; True when it fits there."""
|
|
197
|
+
if lab["at_end"]:
|
|
198
|
+
return False # already at the right edge: it belongs in the stack
|
|
199
|
+
base = lab["yc"]
|
|
200
|
+
if not _hits_line(lab, base, line_disps) and not _hits_label(lab, base, placed):
|
|
201
|
+
return True
|
|
153
202
|
off = _STEP_PX
|
|
154
|
-
while off <=
|
|
203
|
+
while off <= lab["h"]:
|
|
155
204
|
for cand in (base + off, base - off):
|
|
156
|
-
if not _hits_label(lab, cand, placed):
|
|
205
|
+
if not _hits_line(lab, cand, line_disps) and not _hits_label(lab, cand, placed):
|
|
206
|
+
lab["yc"] = cand
|
|
207
|
+
return True
|
|
208
|
+
off += _STEP_PX
|
|
209
|
+
return False
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _isotonic(values: list[float]) -> list[float]:
|
|
213
|
+
"""Least-squares fit of a non-decreasing sequence to values (pool adjacent violators)."""
|
|
214
|
+
blocks: list[tuple[float, int]] = [] # (block mean, block size)
|
|
215
|
+
for val in values:
|
|
216
|
+
mean, count = val, 1
|
|
217
|
+
while blocks and blocks[-1][0] > mean:
|
|
218
|
+
prev_mean, prev_count = blocks.pop()
|
|
219
|
+
mean = (prev_mean * prev_count + mean * count) / (prev_count + count)
|
|
220
|
+
count += prev_count
|
|
221
|
+
blocks.append((mean, count))
|
|
222
|
+
out: list[float] = []
|
|
223
|
+
for mean, count in blocks:
|
|
224
|
+
out.extend([mean] * count)
|
|
225
|
+
return out
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _stack_shift(stack: list[dict[str, Any]], ys: list[float], placed: list[dict[str, Any]]) -> float:
|
|
229
|
+
"""Smallest whole-stack offset that clears the already-placed labels."""
|
|
230
|
+
|
|
231
|
+
def clear(off: float) -> bool:
|
|
232
|
+
return not any(_hits_label(lab, y + off, placed) for lab, y in zip(stack, ys, strict=True))
|
|
233
|
+
|
|
234
|
+
if clear(0.0):
|
|
235
|
+
return 0.0
|
|
236
|
+
off = _STEP_PX
|
|
237
|
+
while off <= _SNAP_LIMIT_PX:
|
|
238
|
+
for cand in (off, -off):
|
|
239
|
+
if clear(cand):
|
|
157
240
|
return cand
|
|
158
241
|
off += _STEP_PX
|
|
159
|
-
return
|
|
242
|
+
return 0.0
|
|
160
243
|
|
|
161
244
|
|
|
162
|
-
def
|
|
163
|
-
|
|
245
|
+
def _stack_snapped(
|
|
246
|
+
stack: list[dict[str, Any]],
|
|
164
247
|
placed: list[dict[str, Any]],
|
|
165
|
-
line_disps: list[LineDisp],
|
|
166
248
|
right_disp_x: float,
|
|
167
249
|
) -> None:
|
|
168
|
-
"""
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
lab["
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
250
|
+
"""Snap labels to the rightmost data point, stacked in line-end order.
|
|
251
|
+
|
|
252
|
+
The vertical order of the stack matches the order of the line ends it
|
|
253
|
+
annotates, so labels for lines that finish close together never swap.
|
|
254
|
+
Within that constraint the labels sit as near their own line end as the
|
|
255
|
+
minimum separation allows.
|
|
256
|
+
"""
|
|
257
|
+
if not stack:
|
|
258
|
+
return
|
|
259
|
+
for lab in stack:
|
|
260
|
+
delta = right_disp_x - lab["x0"]
|
|
261
|
+
lab["x0"] += delta
|
|
262
|
+
lab["x1"] += delta
|
|
263
|
+
lab["snapped"] = True
|
|
264
|
+
|
|
265
|
+
stack.sort(key=lambda lab: lab["anchor_yc"]) # bottom-most line end first
|
|
266
|
+
# Subtracting the cumulative minimum separation turns "stay this far apart,
|
|
267
|
+
# in this order" into plain "non-decreasing", which _isotonic() solves.
|
|
268
|
+
cums: list[float] = []
|
|
269
|
+
cum, prev_h = 0.0, None
|
|
270
|
+
for lab in stack:
|
|
271
|
+
if prev_h is not None:
|
|
272
|
+
cum += (prev_h + lab["h"]) / 2.0 + _GAP_PX
|
|
273
|
+
cums.append(cum)
|
|
274
|
+
prev_h = lab["h"]
|
|
275
|
+
fitted = _isotonic([lab["anchor_yc"] - c for lab, c in zip(stack, cums, strict=True)])
|
|
276
|
+
ys = [f + c for f, c in zip(fitted, cums, strict=True)]
|
|
277
|
+
|
|
278
|
+
shift = _stack_shift(stack, ys, placed)
|
|
279
|
+
for lab, y in zip(stack, ys, strict=True):
|
|
280
|
+
lab["yc"] = y + shift
|
|
187
281
|
|
|
188
282
|
|
|
189
283
|
def _root_figure(axes: Axes) -> Figure:
|
|
@@ -262,6 +356,8 @@ def _build_labels(
|
|
|
262
356
|
"x1": bb.x1,
|
|
263
357
|
"yc": (bb.y0 + bb.y1) / 2.0,
|
|
264
358
|
"h": bb.height,
|
|
359
|
+
"anchor_x0": bb.x0, # box left edge before any placement
|
|
360
|
+
"anchor_yc": (bb.y0 + bb.y1) / 2.0, # the line end this label belongs to
|
|
265
361
|
"own": line_index.get(id(own), -1),
|
|
266
362
|
"at_end": abs(right_x - x_data) <= _X_EPS * span,
|
|
267
363
|
"near": (right_x - x_data) <= near_end * span,
|
|
@@ -271,6 +367,64 @@ def _build_labels(
|
|
|
271
367
|
return labels
|
|
272
368
|
|
|
273
369
|
|
|
370
|
+
def _place_all(
|
|
371
|
+
labels: list[dict[str, Any]],
|
|
372
|
+
line_disps: list[LineDisp | None],
|
|
373
|
+
right_disp_x: float,
|
|
374
|
+
*,
|
|
375
|
+
force_right: bool,
|
|
376
|
+
) -> None:
|
|
377
|
+
"""Give every label a final display y: interior first, then the right-edge stack."""
|
|
378
|
+
|
|
379
|
+
def others(lab: dict[str, Any]) -> list[LineDisp]:
|
|
380
|
+
"""Return line geometry excluding the label's own line (it ends at the anchor)."""
|
|
381
|
+
return [ld for i, ld in enumerate(line_disps) if ld is not None and i != lab["own"]]
|
|
382
|
+
|
|
383
|
+
placed: list[dict[str, Any]] = []
|
|
384
|
+
interior = [] if force_right else [lab for lab in labels if not lab["near"]]
|
|
385
|
+
cluster = [lab for lab in labels if force_right or lab["near"]]
|
|
386
|
+
for lab in sorted(interior, key=lambda lab: lab["yc"]):
|
|
387
|
+
_place_interior(lab, placed, others(lab))
|
|
388
|
+
placed.append(lab)
|
|
389
|
+
|
|
390
|
+
stack: list[dict[str, Any]] = []
|
|
391
|
+
for lab in sorted(cluster, key=lambda lab: (-lab["x_data"], lab["yc"])):
|
|
392
|
+
if not force_right and _place_cluster_local(lab, placed, others(lab)):
|
|
393
|
+
placed.append(lab)
|
|
394
|
+
else:
|
|
395
|
+
stack.append(lab) # laid out together below, so their order is kept
|
|
396
|
+
_stack_snapped(stack, placed, right_disp_x)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def _draw_leaders(axes: Axes, labels: list[dict[str, Any]], trans: Transform) -> None:
|
|
400
|
+
"""Join each displaced label back to its own line end with a thin leader.
|
|
401
|
+
|
|
402
|
+
A label that barely moved gets no leader: the stub would be shorter than the
|
|
403
|
+
text is tall and would read as dirt rather than as a connection.
|
|
404
|
+
"""
|
|
405
|
+
inv = trans.inverted()
|
|
406
|
+
for lab in labels:
|
|
407
|
+
if hypot(lab["x0"] - lab["anchor_x0"], lab["yc"] - lab["anchor_yc"]) < lab["h"] * _LEADER_MIN_FRAC:
|
|
408
|
+
continue
|
|
409
|
+
x_anchor = float(trans.transform((lab["x_data"], 0.0))[0])
|
|
410
|
+
x_start, y_start = inv.transform((x_anchor, lab["anchor_yc"]))
|
|
411
|
+
x_end, y_end = inv.transform((lab["x0"] - _GAP_PX, lab["yc"]))
|
|
412
|
+
# add_artist() (not add_line()) so the leader cannot disturb the data
|
|
413
|
+
# limits the labels were just measured against.
|
|
414
|
+
axes.add_artist(
|
|
415
|
+
Line2D(
|
|
416
|
+
[x_start, x_end],
|
|
417
|
+
[y_start, y_end],
|
|
418
|
+
lw=_LEADER_LW,
|
|
419
|
+
color=lab["t"].get_color(),
|
|
420
|
+
alpha=_LEADER_ALPHA,
|
|
421
|
+
zorder=_LEADER_ZORDER,
|
|
422
|
+
clip_on=False, # snapped stacks can run past ylim
|
|
423
|
+
label="_nolegend_",
|
|
424
|
+
),
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
|
|
274
428
|
# --- public entry point
|
|
275
429
|
def resolve_annotation_collisions(axes: Axes) -> None:
|
|
276
430
|
"""Reposition stashed end-of-line labels so they do not collide.
|
|
@@ -295,24 +449,12 @@ def resolve_annotation_collisions(axes: Axes) -> None:
|
|
|
295
449
|
return
|
|
296
450
|
line_disps, line_index, right_x, span, right_disp_x = geometry
|
|
297
451
|
|
|
298
|
-
|
|
452
|
+
options: AnnotationOptions = stash["options"]
|
|
453
|
+
labels = _build_labels(stash["pairs"], renderer, line_index, (right_x, span, options.near_end))
|
|
299
454
|
if not labels:
|
|
300
455
|
return
|
|
301
456
|
|
|
302
|
-
|
|
303
|
-
"""Return line geometry excluding the label's own line (it ends at the anchor)."""
|
|
304
|
-
return [ld for i, ld in enumerate(line_disps) if ld is not None and i != lab["own"]]
|
|
305
|
-
|
|
306
|
-
# --- place interior labels first, then the right-edge cluster
|
|
307
|
-
placed: list[dict[str, Any]] = []
|
|
308
|
-
interior = sorted((lab for lab in labels if not lab["near"]), key=lambda lab: lab["yc"])
|
|
309
|
-
cluster = sorted((lab for lab in labels if lab["near"]), key=lambda lab: (-lab["x_data"], lab["yc"]))
|
|
310
|
-
for lab in interior:
|
|
311
|
-
_place_interior(lab, placed, others(lab))
|
|
312
|
-
placed.append(lab)
|
|
313
|
-
for lab in cluster:
|
|
314
|
-
_place_cluster(lab, placed, others(lab), right_disp_x)
|
|
315
|
-
placed.append(lab)
|
|
457
|
+
_place_all(labels, line_disps, right_disp_x, force_right=options.force_right)
|
|
316
458
|
|
|
317
459
|
# --- write the new positions back in data coordinates
|
|
318
460
|
inv = trans.inverted()
|
|
@@ -322,3 +464,6 @@ def resolve_annotation_collisions(axes: Axes) -> None:
|
|
|
322
464
|
lab["t"].set_y(y_data)
|
|
323
465
|
if lab["snapped"]:
|
|
324
466
|
lab["t"].set_x(right_x)
|
|
467
|
+
|
|
468
|
+
if options.leader_lines:
|
|
469
|
+
_draw_leaders(axes, labels, trans)
|
mgplot/finalise_plot.py
CHANGED
|
@@ -35,7 +35,7 @@ DEFAULT_FILE_TITLE_NAME: Final[str] = "plot"
|
|
|
35
35
|
# --- annotated axvline text
|
|
36
36
|
VLINE_TEXT_FONTSIZE: Final[str] = "xx-small"
|
|
37
37
|
VLINE_TEXT_ROTATION: Final[int] = 90
|
|
38
|
-
VLINE_TEXT_OFFSET: Final[float] =
|
|
38
|
+
VLINE_TEXT_OFFSET: Final[float] = 2.0 # points, to the right of the line
|
|
39
39
|
VLINE_TEXT_PAD: Final[float] = 0.01 # axes fraction, in from the top/bottom
|
|
40
40
|
VLINE_AUTO_BAND: Final[float] = 0.02 # fraction of the x-span sampled either side
|
|
41
41
|
|
|
@@ -284,7 +284,37 @@ def _convert_period_value(axes: Axes, setting: str, value: object) -> object:
|
|
|
284
284
|
# --- annotated vertical lines
|
|
285
285
|
# "text", "loc" and "text_kwargs" in an axvline dict describe its label rather
|
|
286
286
|
# than the line, and are popped before the dict is splatted into ax.axvline().
|
|
287
|
-
|
|
287
|
+
# loc is a space-separated string mixing a vertical word ("auto"/"top"/"bottom")
|
|
288
|
+
# with a side word ("left"/"right"); either may be omitted. The side picks which
|
|
289
|
+
# flank of the line the rotated label sits on, VLINE_TEXT_OFFSET points away.
|
|
290
|
+
_VLINE_EDGES: Final[tuple[str, ...]] = ("top", "bottom")
|
|
291
|
+
_VLINE_SIDES: Final[tuple[str, ...]] = ("left", "right")
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _parse_vline_loc(loc: object) -> tuple[str, str]:
|
|
295
|
+
"""Split a loc string into (edge, side), order-free and each optional.
|
|
296
|
+
|
|
297
|
+
edge is "auto", "top" or "bottom"; side is "left" or "right". An omitted
|
|
298
|
+
edge falls back to "auto" (choose by the data) and an omitted side to
|
|
299
|
+
"right" (today's placement). Raises on an unrecognised or doubled word --
|
|
300
|
+
each is a typo, not a choice.
|
|
301
|
+
"""
|
|
302
|
+
if not isinstance(loc, str):
|
|
303
|
+
raise TypeError(f"axvline 'loc' must be a string, got {type(loc)}")
|
|
304
|
+
edge: str | None = None
|
|
305
|
+
side: str | None = None
|
|
306
|
+
for tok in loc.split():
|
|
307
|
+
if tok == "auto" or tok in _VLINE_EDGES:
|
|
308
|
+
if edge is not None:
|
|
309
|
+
raise ValueError(f"axvline 'loc' names two vertical positions, got {loc!r}")
|
|
310
|
+
edge = tok
|
|
311
|
+
elif tok in _VLINE_SIDES:
|
|
312
|
+
if side is not None:
|
|
313
|
+
raise ValueError(f"axvline 'loc' names two sides, got {loc!r}")
|
|
314
|
+
side = tok
|
|
315
|
+
else:
|
|
316
|
+
raise ValueError(f"axvline 'loc' word {tok!r} not recognised in {loc!r}")
|
|
317
|
+
return edge or "auto", side or "right"
|
|
288
318
|
|
|
289
319
|
|
|
290
320
|
def _pop_vline_text(item: dict[str, Any]) -> tuple[str, str, dict[str, Any]] | None:
|
|
@@ -301,8 +331,7 @@ def _pop_vline_text(item: dict[str, Any]) -> tuple[str, str, dict[str, Any]] | N
|
|
|
301
331
|
if loc != "auto" or text_kwargs:
|
|
302
332
|
raise ValueError("axvline 'loc'/'text_kwargs' given without any 'text' to place")
|
|
303
333
|
return None
|
|
304
|
-
|
|
305
|
-
raise ValueError(f"axvline 'loc' must be one of {_VLINE_LOCATIONS}, got {loc!r}")
|
|
334
|
+
_parse_vline_loc(loc) # validate now; raises on a bad loc string
|
|
306
335
|
if not isinstance(text_kwargs, dict):
|
|
307
336
|
raise TypeError(f"axvline 'text_kwargs' must be a dict, got {type(text_kwargs)}")
|
|
308
337
|
return str(text), loc, text_kwargs
|
|
@@ -362,23 +391,26 @@ def _auto_vline_loc(axes: Axes, x: float) -> str:
|
|
|
362
391
|
|
|
363
392
|
|
|
364
393
|
def _annotate_vline(axes: Axes, item: dict[str, Any], line: Line2D, spec: tuple[str, str, dict]) -> None:
|
|
365
|
-
"""Place a rotated text label just to
|
|
394
|
+
"""Place a rotated text label just to one side of a vertical line.
|
|
366
395
|
|
|
367
|
-
The
|
|
368
|
-
|
|
369
|
-
of any later change to the
|
|
396
|
+
The side (left/right of the line) comes from loc; the label is anchored
|
|
397
|
+
with x in data coordinates and y in axes coordinates, so it stays pinned
|
|
398
|
+
to the top/bottom of the plot regardless of any later change to the
|
|
399
|
+
y-limits.
|
|
370
400
|
"""
|
|
371
401
|
text, loc, text_kwargs = spec
|
|
372
402
|
x = item.get("x", 0) # matches the matplotlib default for axvline
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
403
|
+
edge, side = _parse_vline_loc(loc)
|
|
404
|
+
if edge == "auto":
|
|
405
|
+
edge = _auto_vline_loc(axes, float(x))
|
|
406
|
+
y, valign = (1.0 - VLINE_TEXT_PAD, "top") if edge == "top" else (VLINE_TEXT_PAD, "bottom")
|
|
407
|
+
x_off, halign = (VLINE_TEXT_OFFSET, "left") if side == "right" else (-VLINE_TEXT_OFFSET, "right")
|
|
376
408
|
|
|
377
409
|
options: dict[str, Any] = {
|
|
378
410
|
"rotation": VLINE_TEXT_ROTATION,
|
|
379
411
|
"fontsize": VLINE_TEXT_FONTSIZE,
|
|
380
412
|
"color": line.get_color(),
|
|
381
|
-
"ha":
|
|
413
|
+
"ha": halign,
|
|
382
414
|
"va": valign,
|
|
383
415
|
}
|
|
384
416
|
options.update(text_kwargs)
|
|
@@ -386,7 +418,7 @@ def _annotate_vline(axes: Axes, item: dict[str, Any], line: Line2D, spec: tuple[
|
|
|
386
418
|
text,
|
|
387
419
|
xy=(x, y),
|
|
388
420
|
xycoords=blended_transform_factory(axes.transData, axes.transAxes),
|
|
389
|
-
xytext=(
|
|
421
|
+
xytext=(x_off, 0),
|
|
390
422
|
textcoords="offset points",
|
|
391
423
|
**options,
|
|
392
424
|
)
|
mgplot/line_plot.py
CHANGED
|
@@ -9,7 +9,7 @@ from matplotlib.text import Text
|
|
|
9
9
|
from pandas import DataFrame, Period, PeriodIndex, Series
|
|
10
10
|
from pandas.api.types import is_numeric_dtype
|
|
11
11
|
|
|
12
|
-
from mgplot.annotation_utils import register_annotations
|
|
12
|
+
from mgplot.annotation_utils import AnnotationOptions, register_annotations
|
|
13
13
|
from mgplot.axis_utils import map_periodindex, set_labels
|
|
14
14
|
from mgplot.keyword_checking import BaseKwargs, report_kwargs, validate_kwargs
|
|
15
15
|
from mgplot.settings import DataT, get_setting
|
|
@@ -51,6 +51,8 @@ class LineKwargs(BaseKwargs):
|
|
|
51
51
|
rotation: NotRequired[Sequence[int | float] | int | float]
|
|
52
52
|
annotate_color: NotRequired[str | Sequence[str] | bool | Sequence[bool] | None]
|
|
53
53
|
near_end: NotRequired[float]
|
|
54
|
+
force_right: NotRequired[bool]
|
|
55
|
+
leader_lines: NotRequired[bool]
|
|
54
56
|
plot_from: NotRequired[int | Period | None]
|
|
55
57
|
label_series: NotRequired[bool | Sequence[bool] | None]
|
|
56
58
|
max_ticks: NotRequired[int]
|
|
@@ -244,7 +246,11 @@ def line_plot(data: DataT, **kwargs: Unpack[LineKwargs]) -> Axes:
|
|
|
244
246
|
axes,
|
|
245
247
|
annotations,
|
|
246
248
|
drawn_lines,
|
|
247
|
-
|
|
249
|
+
AnnotationOptions(
|
|
250
|
+
near_end=kwargs_d.get("near_end", DEFAULT_NEAR_END),
|
|
251
|
+
force_right=bool(kwargs_d.get("force_right", False)),
|
|
252
|
+
leader_lines=bool(kwargs_d.get("leader_lines", False)),
|
|
253
|
+
),
|
|
248
254
|
)
|
|
249
255
|
|
|
250
256
|
# --- set the labels
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: mgplot
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.32
|
|
4
4
|
Summary: mgplot is a time-series/PeriodIndex frontend for matplotlib
|
|
5
5
|
Project-URL: Repository, https://github.com/bpalmer4/mgplot
|
|
6
6
|
Project-URL: Homepage, https://github.com/bpalmer4/mgplot
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
mgplot/__init__.py,sha256=J3CQX5UXrGXrfs-YWpbzHA_sz1HJ5438xNM0V36x1vo,3164
|
|
2
|
-
mgplot/annotation_utils.py,sha256=
|
|
2
|
+
mgplot/annotation_utils.py,sha256=nlR1J_0b9HD7d3sL1oyNmhiIyI2Xp6cgKW5fwWDRSaI,18567
|
|
3
3
|
mgplot/axis_utils.py,sha256=-wnjUTL2MdwEntLEVihDABOxLSbux0aSyATCnIsVumg,16041
|
|
4
4
|
mgplot/bar_plot.py,sha256=FobAsadYSEf-U0RZFgZutTzse4Rgr0TDFyWUMM68JVs,12513
|
|
5
5
|
mgplot/colors.py,sha256=f5q4z7kORUu66uAxNeK9KAZ64gv_7h_x2qH53BvzwPw,4848
|
|
6
6
|
mgplot/fill_between_plot.py,sha256=XJvtkBeOkohHyayQnu84jURapB5RsMAR-3SaskHkHfM,3361
|
|
7
|
-
mgplot/finalise_plot.py,sha256=
|
|
7
|
+
mgplot/finalise_plot.py,sha256=ttrSy2noxF4EyFlYZJUjy1LICFFE0IuF_26HreeMD1k,24456
|
|
8
8
|
mgplot/finalisers.py,sha256=Wozq4XOmnMdhEB9LJqHao9TrleOfhNV54kRa_bgN-e8,10547
|
|
9
9
|
mgplot/growth_plot.py,sha256=QALNYyINd_u7BL3q-My16eRuhq0LzGfawu-_MsVOXl0,8708
|
|
10
10
|
mgplot/keyword_checking.py,sha256=8CFexwEK_Uyv3_8hAnJ2ewk0tEjNjkwzKfX2uFw2UpM,11446
|
|
11
|
-
mgplot/line_plot.py,sha256=
|
|
11
|
+
mgplot/line_plot.py,sha256=Glajqp5lDIKKt5V-AwVhn84BvdfPSzJme1s3BnGqJD8,9175
|
|
12
12
|
mgplot/lint-all.sh,sha256=O7ns09v0cQNQ6POWtXtYn6VdtYURHTjy06vntR8tr_U,465
|
|
13
13
|
mgplot/multi_plot.py,sha256=8tiUXe1UJGyPvHp3ox2CQ5b-KjdSV9y5avI_70uJFMk,10699
|
|
14
14
|
mgplot/postcovid_plot.py,sha256=TR3BY_1CbIR2IRvlqqbqhhacEyUMDZIEL-mcLs9FV3U,8716
|
|
@@ -19,7 +19,7 @@ mgplot/seastrend_plot.py,sha256=hi9t0cY0bHdIOU5abLMX7qvAp_hlVLlJFyEKNBzg6Ts,2000
|
|
|
19
19
|
mgplot/settings.py,sha256=RhxgX4d-CgRpvs-ALG_j8tg3wwHSyfvFNdL0aMjofA0,5584
|
|
20
20
|
mgplot/summary_plot.py,sha256=Kg7VD8KFIls4KlD4jeaaXnDw51BZ3p_wMAUpjG49zGQ,11102
|
|
21
21
|
mgplot/utilities.py,sha256=PfzThb_nXYIMhnprQzNcaBqUZV8ysHJte0TU7TIis00,8450
|
|
22
|
-
mgplot-0.2.
|
|
23
|
-
mgplot-0.2.
|
|
24
|
-
mgplot-0.2.
|
|
25
|
-
mgplot-0.2.
|
|
22
|
+
mgplot-0.2.32.dist-info/METADATA,sha256=qvq5qkI_HneUX7yrF4yvElE8piLhtTKvtfcwZVQ_apw,7923
|
|
23
|
+
mgplot-0.2.32.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
24
|
+
mgplot-0.2.32.dist-info/licenses/LICENSE,sha256=6ZyruOwF8J7mY25-vZHdOibtY8cSNLz0vWeH59TLhtw,1082
|
|
25
|
+
mgplot-0.2.32.dist-info/RECORD,,
|
|
File without changes
|