stplot 0.1.0__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.
- stplot/__init__.py +21 -0
- stplot/config/__init__.py +33 -0
- stplot/config/loader.py +106 -0
- stplot/config/models.py +328 -0
- stplot/config/presets/__init__.py +1 -0
- stplot/config/presets/default.yaml +208 -0
- stplot/config/presets/horizons_one_plan.yaml +59 -0
- stplot/io/__init__.py +6 -0
- stplot/io/spatial.py +72 -0
- stplot/io/tabular.py +154 -0
- stplot/plots/__init__.py +122 -0
- stplot/plots/barbell.py +400 -0
- stplot/plots/batch.py +135 -0
- stplot/plots/domains.py +1143 -0
- stplot/plots/executive.py +474 -0
- stplot/plots/export.py +74 -0
- stplot/plots/infographics.py +1118 -0
- stplot/plots/lollipop.py +285 -0
- stplot/plots/maps.py +738 -0
- stplot/plots/matrix.py +190 -0
- stplot/plots/one_plan.py +642 -0
- stplot/plots/paired.py +437 -0
- stplot/plots/scorecards.py +1940 -0
- stplot/plots/state.py +373 -0
- stplot/plots/state_trends.py +1413 -0
- stplot/plots/tables.py +107 -0
- stplot/plots/timeseries.py +607 -0
- stplot/plots/trends.py +305 -0
- stplot/reporting/__init__.py +24 -0
- stplot/reporting/captions.py +177 -0
- stplot/reporting/html_companion.py +339 -0
- stplot/reporting/templates.py +485 -0
- stplot/reporting/word.py +369 -0
- stplot/theme/__init__.py +25 -0
- stplot/theme/folium.py +62 -0
- stplot/theme/mpl.py +181 -0
- stplot/theme/plotly.py +130 -0
- stplot/transform/__init__.py +60 -0
- stplot/transform/categories.py +51 -0
- stplot/transform/columns.py +103 -0
- stplot/transform/filters.py +385 -0
- stplot/transform/merge.py +173 -0
- stplot/transform/pairing.py +154 -0
- stplot/transform/qa.py +297 -0
- stplot/transform/tables.py +573 -0
- stplot/transform/zones.py +354 -0
- stplot-0.1.0.dist-info/METADATA +235 -0
- stplot-0.1.0.dist-info/RECORD +49 -0
- stplot-0.1.0.dist-info/WHEEL +4 -0
stplot/plots/matrix.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Interactive Plotly NOF grade matrix (tile grid) template over rolling periods."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import pandas as pd
|
|
9
|
+
|
|
10
|
+
from stplot.config import (
|
|
11
|
+
Config,
|
|
12
|
+
ensure_config,
|
|
13
|
+
resolve_category_order,
|
|
14
|
+
resolve_column,
|
|
15
|
+
resolve_palette,
|
|
16
|
+
)
|
|
17
|
+
from stplot.theme.plotly import build_plotly_template
|
|
18
|
+
from stplot.transform.columns import validate_columns
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _import_plotly() -> tuple[Any, Any]:
|
|
22
|
+
try:
|
|
23
|
+
import plotly.graph_objects as go
|
|
24
|
+
from plotly.subplots import make_subplots
|
|
25
|
+
except ImportError as exc:
|
|
26
|
+
raise ImportError(
|
|
27
|
+
"Interactive Plotly charts require plotly. "
|
|
28
|
+
'Install with: pip install "stplot[interactive]"'
|
|
29
|
+
) from exc
|
|
30
|
+
return go, make_subplots
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def grade_matrix(
|
|
34
|
+
df: pd.DataFrame,
|
|
35
|
+
*,
|
|
36
|
+
period_col: str | None = None,
|
|
37
|
+
attribute_col: str | None = None,
|
|
38
|
+
band_col: str | None = None,
|
|
39
|
+
facet_col: str | None = None,
|
|
40
|
+
period_order: list[str] | None = None,
|
|
41
|
+
attribute_order: list[str] | None = None,
|
|
42
|
+
band_order: list[str] | None = None,
|
|
43
|
+
palette: dict[str, str] | None = None,
|
|
44
|
+
title: str | None = None,
|
|
45
|
+
xlabel: str | None = None,
|
|
46
|
+
marker_size: int = 22,
|
|
47
|
+
height: int | None = None,
|
|
48
|
+
width: int | None = None,
|
|
49
|
+
config: str | Path | Config | None = None,
|
|
50
|
+
) -> Any:
|
|
51
|
+
"""Plot a Plotly tile grid of NOF grades across attributes and rolling periods.
|
|
52
|
+
|
|
53
|
+
Supports both single-site grids (``facet_col=None``) and side-by-side multi-site
|
|
54
|
+
faceted comparisons (e.g., ``facet_col="sID"``).
|
|
55
|
+
|
|
56
|
+
Required columns (resolved via ``config.columns`` unless explicitly overridden):
|
|
57
|
+
- ``period_col`` (default key: ``"period"``, e.g., ``"yearz"``)
|
|
58
|
+
- ``attribute_col`` (default key: ``"attribute"``, e.g., ``"Attr"``)
|
|
59
|
+
- ``band_col`` (default key: ``"state_band"``, e.g., ``"NOF_Grade"``)
|
|
60
|
+
- ``facet_col`` (optional; e.g., ``"sID"`` for side-by-side site comparison)
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
``plotly.graph_objects.Figure`` instance.
|
|
64
|
+
"""
|
|
65
|
+
go, make_subplots = _import_plotly()
|
|
66
|
+
cfg = ensure_config(config)
|
|
67
|
+
|
|
68
|
+
resolved_period = resolve_column(period_col, cfg, "period")
|
|
69
|
+
resolved_attr = resolve_column(attribute_col, cfg, "attribute")
|
|
70
|
+
resolved_band = resolve_column(band_col, cfg, "state_band")
|
|
71
|
+
|
|
72
|
+
required = [resolved_period, resolved_attr, resolved_band]
|
|
73
|
+
if facet_col is not None:
|
|
74
|
+
required.append(facet_col)
|
|
75
|
+
validate_columns(df, required, context="grade_matrix")
|
|
76
|
+
|
|
77
|
+
resolved_palette = resolve_palette(palette, cfg, "state_bands")
|
|
78
|
+
resolved_band_order = resolve_category_order(band_order, cfg, "state_bands")
|
|
79
|
+
|
|
80
|
+
work = df.dropna(subset=[resolved_period, resolved_attr, resolved_band]).copy()
|
|
81
|
+
work[resolved_period] = work[resolved_period].astype(str)
|
|
82
|
+
work[resolved_attr] = work[resolved_attr].astype(str)
|
|
83
|
+
work[resolved_band] = work[resolved_band].astype(str)
|
|
84
|
+
|
|
85
|
+
periods = (
|
|
86
|
+
list(period_order)
|
|
87
|
+
if period_order is not None
|
|
88
|
+
else sorted(work[resolved_period].unique().tolist())
|
|
89
|
+
)
|
|
90
|
+
attributes = (
|
|
91
|
+
list(attribute_order)
|
|
92
|
+
if attribute_order is not None
|
|
93
|
+
else sorted(work[resolved_attr].unique().tolist())
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
present_bands = work[resolved_band].unique().tolist()
|
|
97
|
+
ordered_bands = [
|
|
98
|
+
b for b in resolved_band_order if b in present_bands or b in {"A", "B", "C", "D", "E"}
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
if facet_col is not None:
|
|
102
|
+
facets = list(dict.fromkeys(work[facet_col].astype(str).tolist()))
|
|
103
|
+
else:
|
|
104
|
+
facets = [None]
|
|
105
|
+
|
|
106
|
+
n_facets = len(facets)
|
|
107
|
+
fig = make_subplots(
|
|
108
|
+
rows=1,
|
|
109
|
+
cols=n_facets,
|
|
110
|
+
shared_yaxes=True,
|
|
111
|
+
subplot_titles=[str(f) for f in facets] if facet_col is not None else None,
|
|
112
|
+
horizontal_spacing=0.03,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
shown_in_legend: set[str] = set()
|
|
116
|
+
for col_idx, facet_val in enumerate(facets, start=1):
|
|
117
|
+
sub_facet = (
|
|
118
|
+
work[work[facet_col].astype(str) == facet_val] if facet_col is not None else work
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
for band in ordered_bands:
|
|
122
|
+
sub = sub_facet[sub_facet[resolved_band] == band]
|
|
123
|
+
show_legend = band not in shown_in_legend
|
|
124
|
+
if show_legend:
|
|
125
|
+
shown_in_legend.add(band)
|
|
126
|
+
|
|
127
|
+
x_vals = sub[resolved_period].tolist()
|
|
128
|
+
y_vals = sub[resolved_attr].tolist()
|
|
129
|
+
text_vals = [band] * len(sub)
|
|
130
|
+
|
|
131
|
+
fig.add_trace(
|
|
132
|
+
go.Scatter(
|
|
133
|
+
x=x_vals if x_vals else [None],
|
|
134
|
+
y=y_vals if y_vals else [None],
|
|
135
|
+
mode="markers+text",
|
|
136
|
+
name=band,
|
|
137
|
+
legendgroup=band,
|
|
138
|
+
showlegend=show_legend,
|
|
139
|
+
text=text_vals if text_vals else [None],
|
|
140
|
+
textposition="middle center",
|
|
141
|
+
textfont={"color": "#444444", "size": max(int(marker_size * 0.5), 9)},
|
|
142
|
+
marker={
|
|
143
|
+
"symbol": "square",
|
|
144
|
+
"size": marker_size,
|
|
145
|
+
"color": resolved_palette.get(band, "#bdbdbd"),
|
|
146
|
+
"line": {"width": 0},
|
|
147
|
+
},
|
|
148
|
+
hovertemplate=(
|
|
149
|
+
f"{(str(facet_val) + '<br>') if facet_val else ''}"
|
|
150
|
+
"<b>%{y}</b><br>"
|
|
151
|
+
"Period: %{x}<br>"
|
|
152
|
+
f"Grade: {band}<extra></extra>"
|
|
153
|
+
),
|
|
154
|
+
),
|
|
155
|
+
row=1,
|
|
156
|
+
col=col_idx,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
fig.update_xaxes(
|
|
160
|
+
categoryorder="array",
|
|
161
|
+
categoryarray=periods,
|
|
162
|
+
showgrid=False,
|
|
163
|
+
tickangle=90 if (n_facets > 1 or len(periods) > 10) else 0,
|
|
164
|
+
title_text=xlabel if (xlabel and col_idx == n_facets) else None,
|
|
165
|
+
row=1,
|
|
166
|
+
col=col_idx,
|
|
167
|
+
)
|
|
168
|
+
fig.update_yaxes(
|
|
169
|
+
categoryorder="array",
|
|
170
|
+
categoryarray=attributes,
|
|
171
|
+
showgrid=True,
|
|
172
|
+
gridcolor=cfg.theme.grid_color,
|
|
173
|
+
gridwidth=cfg.theme.grid_width,
|
|
174
|
+
row=1,
|
|
175
|
+
col=col_idx,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
auto_height = max(360, 28 * len(attributes) + 150)
|
|
179
|
+
auto_width = max(780, (36 * len(periods) + 200) * n_facets)
|
|
180
|
+
fig.update_layout(
|
|
181
|
+
template=build_plotly_template(cfg),
|
|
182
|
+
title=f"<b>{title}</b>" if title else None,
|
|
183
|
+
plot_bgcolor=cfg.theme.axes_facecolor,
|
|
184
|
+
paper_bgcolor=cfg.theme.background_color,
|
|
185
|
+
height=height or auto_height,
|
|
186
|
+
width=width or min(auto_width, 1600),
|
|
187
|
+
margin={"t": 70, "b": 90, "l": 190, "r": 70},
|
|
188
|
+
legend={"traceorder": "normal"},
|
|
189
|
+
)
|
|
190
|
+
return fig
|