AeroViz 0.1.3__py3-none-any.whl → 0.1.4__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.
Potentially problematic release.
This version of AeroViz might be problematic. Click here for more details.
- AeroViz/__init__.py +7 -5
- AeroViz/{config → data}/DEFAULT_DATA.csv +1 -1
- AeroViz/dataProcess/Chemistry/__init__.py +40 -40
- AeroViz/dataProcess/Chemistry/_calculate.py +15 -15
- AeroViz/dataProcess/Chemistry/_isoropia.py +72 -68
- AeroViz/dataProcess/Chemistry/_mass_volume.py +158 -161
- AeroViz/dataProcess/Chemistry/_ocec.py +109 -109
- AeroViz/dataProcess/Chemistry/_partition.py +19 -18
- AeroViz/dataProcess/Chemistry/_teom.py +9 -11
- AeroViz/dataProcess/Chemistry/isrpia.cnf +21 -0
- AeroViz/dataProcess/Optical/Angstrom_exponent.py +20 -0
- AeroViz/dataProcess/Optical/_IMPROVE.py +40 -41
- AeroViz/dataProcess/Optical/__init__.py +29 -44
- AeroViz/dataProcess/Optical/_absorption.py +21 -47
- AeroViz/dataProcess/Optical/_extinction.py +31 -25
- AeroViz/dataProcess/Optical/_mie.py +5 -7
- AeroViz/dataProcess/Optical/_mie_sd.py +89 -90
- AeroViz/dataProcess/Optical/_scattering.py +19 -20
- AeroViz/dataProcess/SizeDistr/__init__.py +39 -39
- AeroViz/dataProcess/SizeDistr/__merge.py +159 -158
- AeroViz/dataProcess/SizeDistr/_merge.py +155 -154
- AeroViz/dataProcess/SizeDistr/_merge_v1.py +162 -161
- AeroViz/dataProcess/SizeDistr/_merge_v2.py +153 -152
- AeroViz/dataProcess/SizeDistr/_merge_v3.py +327 -327
- AeroViz/dataProcess/SizeDistr/_merge_v4.py +273 -275
- AeroViz/dataProcess/SizeDistr/_size_distr.py +51 -51
- AeroViz/dataProcess/VOC/__init__.py +9 -9
- AeroViz/dataProcess/VOC/_potential_par.py +53 -55
- AeroViz/dataProcess/__init__.py +28 -6
- AeroViz/dataProcess/core/__init__.py +59 -65
- AeroViz/plot/__init__.py +7 -2
- AeroViz/plot/bar.py +126 -0
- AeroViz/plot/box.py +69 -0
- AeroViz/plot/distribution/distribution.py +421 -427
- AeroViz/plot/meteorology/meteorology.py +240 -292
- AeroViz/plot/optical/__init__.py +0 -1
- AeroViz/plot/optical/optical.py +230 -230
- AeroViz/plot/pie.py +198 -0
- AeroViz/plot/regression.py +196 -0
- AeroViz/plot/scatter.py +165 -0
- AeroViz/plot/templates/__init__.py +2 -4
- AeroViz/plot/templates/ammonium_rich.py +34 -0
- AeroViz/plot/templates/contour.py +25 -25
- AeroViz/plot/templates/corr_matrix.py +86 -93
- AeroViz/plot/templates/diurnal_pattern.py +28 -26
- AeroViz/plot/templates/koschmieder.py +59 -123
- AeroViz/plot/templates/metal_heatmap.py +135 -37
- AeroViz/plot/timeseries/__init__.py +1 -0
- AeroViz/plot/timeseries/template.py +47 -0
- AeroViz/plot/timeseries/timeseries.py +324 -264
- AeroViz/plot/utils/__init__.py +2 -1
- AeroViz/plot/utils/_color.py +57 -57
- AeroViz/plot/utils/_unit.py +48 -48
- AeroViz/plot/utils/plt_utils.py +92 -0
- AeroViz/plot/utils/sklearn_utils.py +49 -0
- AeroViz/plot/utils/units.json +5 -0
- AeroViz/plot/violin.py +80 -0
- AeroViz/process/__init__.py +17 -17
- AeroViz/process/core/DataProc.py +9 -9
- AeroViz/process/core/SizeDist.py +81 -81
- AeroViz/process/method/PyMieScatt_update.py +488 -488
- AeroViz/process/method/mie_theory.py +231 -229
- AeroViz/process/method/prop.py +40 -40
- AeroViz/process/script/AbstractDistCalc.py +103 -103
- AeroViz/process/script/Chemical.py +168 -167
- AeroViz/process/script/IMPACT.py +40 -40
- AeroViz/process/script/IMPROVE.py +152 -152
- AeroViz/process/script/Others.py +45 -45
- AeroViz/process/script/PSD.py +26 -26
- AeroViz/process/script/PSD_dry.py +69 -70
- AeroViz/process/script/retrieve_RI.py +50 -51
- AeroViz/rawDataReader/__init__.py +53 -58
- AeroViz/rawDataReader/config/supported_instruments.py +155 -0
- AeroViz/rawDataReader/core/__init__.py +233 -356
- AeroViz/rawDataReader/script/AE33.py +17 -18
- AeroViz/rawDataReader/script/AE43.py +18 -21
- AeroViz/rawDataReader/script/APS_3321.py +30 -30
- AeroViz/rawDataReader/script/Aurora.py +23 -24
- AeroViz/rawDataReader/script/BC1054.py +36 -40
- AeroViz/rawDataReader/script/EPA_vertical.py +37 -9
- AeroViz/rawDataReader/script/GRIMM.py +16 -23
- AeroViz/rawDataReader/script/IGAC.py +90 -0
- AeroViz/rawDataReader/script/MA350.py +32 -39
- AeroViz/rawDataReader/script/Minion.py +103 -0
- AeroViz/rawDataReader/script/NEPH.py +69 -74
- AeroViz/rawDataReader/script/SMPS_TH.py +25 -25
- AeroViz/rawDataReader/script/SMPS_aim11.py +32 -32
- AeroViz/rawDataReader/script/SMPS_genr.py +31 -31
- AeroViz/rawDataReader/script/Sunset_OCEC.py +60 -0
- AeroViz/rawDataReader/script/TEOM.py +30 -28
- AeroViz/rawDataReader/script/Table.py +13 -14
- AeroViz/rawDataReader/script/VOC.py +26 -0
- AeroViz/rawDataReader/script/__init__.py +18 -20
- AeroViz/tools/database.py +64 -66
- AeroViz/tools/dataclassifier.py +106 -106
- AeroViz/tools/dataprinter.py +51 -51
- AeroViz/tools/datareader.py +38 -38
- {AeroViz-0.1.3.dist-info → AeroViz-0.1.4.dist-info}/METADATA +5 -4
- AeroViz-0.1.4.dist-info/RECORD +112 -0
- AeroViz/plot/improve/__init__.py +0 -1
- AeroViz/plot/improve/improve.py +0 -240
- AeroViz/plot/optical/aethalometer.py +0 -77
- AeroViz/plot/templates/event_evolution.py +0 -65
- AeroViz/plot/templates/regression.py +0 -256
- AeroViz/plot/templates/scatter.py +0 -130
- AeroViz/plot/templates/templates.py +0 -398
- AeroViz/plot/utils/_decorator.py +0 -74
- AeroViz/rawDataReader/script/IGAC_TH.py +0 -104
- AeroViz/rawDataReader/script/IGAC_ZM.py +0 -90
- AeroViz/rawDataReader/script/OCEC_LCRES.py +0 -34
- AeroViz/rawDataReader/script/OCEC_RES.py +0 -28
- AeroViz/rawDataReader/script/VOC_TH.py +0 -30
- AeroViz/rawDataReader/script/VOC_ZM.py +0 -37
- AeroViz/rawDataReader/utils/__init__.py +0 -0
- AeroViz/rawDataReader/utils/config.py +0 -169
- AeroViz-0.1.3.dist-info/RECORD +0 -111
- /AeroViz/{config → data}/DEFAULT_PNSD_DATA.csv +0 -0
- /AeroViz/{config → rawDataReader/config}/__init__.py +0 -0
- {AeroViz-0.1.3.dist-info → AeroViz-0.1.4.dist-info}/LICENSE +0 -0
- {AeroViz-0.1.3.dist-info → AeroViz-0.1.4.dist-info}/WHEEL +0 -0
- {AeroViz-0.1.3.dist-info → AeroViz-0.1.4.dist-info}/top_level.txt +0 -0
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
from datetime import datetime
|
|
2
1
|
from typing import Literal
|
|
3
2
|
|
|
4
3
|
import matplotlib.pyplot as plt
|
|
4
|
+
import numpy as np
|
|
5
5
|
from matplotlib.cm import ScalarMappable
|
|
6
6
|
from matplotlib.pyplot import Figure, Axes
|
|
7
7
|
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
|
|
8
|
-
from pandas import DataFrame, date_range
|
|
8
|
+
from pandas import DataFrame, date_range
|
|
9
9
|
|
|
10
10
|
from AeroViz.plot.utils import *
|
|
11
11
|
|
|
12
|
-
__all__ = ['timeseries', '
|
|
12
|
+
__all__ = ['timeseries', 'timeseries_stacked']
|
|
13
|
+
|
|
13
14
|
|
|
14
15
|
default_bar_kws = dict(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
width=0.0417,
|
|
17
|
+
edgecolor=None,
|
|
18
|
+
linewidth=0,
|
|
19
|
+
cmap='jet',
|
|
19
20
|
)
|
|
20
21
|
|
|
21
22
|
default_scatter_kws = dict(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
marker='o',
|
|
24
|
+
s=5,
|
|
25
|
+
edgecolor=None,
|
|
26
|
+
linewidths=0.3,
|
|
27
|
+
alpha=0.9,
|
|
28
|
+
cmap='jet',
|
|
28
29
|
)
|
|
29
30
|
|
|
30
31
|
default_insert_kws = dict(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
width="1.5%",
|
|
33
|
+
height="100%",
|
|
34
|
+
loc='lower left',
|
|
35
|
+
bbox_to_anchor=(1.01, 0, 1.2, 1),
|
|
36
|
+
borderpad=0
|
|
36
37
|
)
|
|
37
38
|
|
|
38
39
|
default_plot_kws = dict()
|
|
@@ -41,262 +42,321 @@ default_cbar_kws = dict()
|
|
|
41
42
|
|
|
42
43
|
|
|
43
44
|
def _scatter(ax, df, _y, _c, scatter_kws, cbar_kws, inset_kws):
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
if _c is None or _c not in df.columns:
|
|
46
|
+
scatter_kws.pop('cmap')
|
|
47
|
+
ax.scatter(df.index, df[_y], **scatter_kws)
|
|
48
|
+
else:
|
|
49
|
+
ax.scatter(df.index, df[_y], c=df[_c], **scatter_kws)
|
|
50
|
+
cax = inset_axes(ax, **inset_kws)
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
# Filter the children to find ScalarMappable objects
|
|
53
|
+
mappable_objects = [child for child in ax.get_children() if isinstance(child, ScalarMappable)]
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
# Use the first mappable object for the colorbar
|
|
56
|
+
if mappable_objects:
|
|
57
|
+
plt.colorbar(mappable=mappable_objects[0], cax=cax, **cbar_kws)
|
|
58
|
+
else:
|
|
59
|
+
print("No mappable objects found.")
|
|
59
60
|
|
|
60
61
|
|
|
61
62
|
def _bar(ax, df, _y, _c, bar_kws, cbar_kws, inset_kws):
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
scalar_map, colors = Color.color_maker(df[_c].values, cmap=bar_kws.pop('cmap'))
|
|
64
|
+
ax.bar(df.index, df[_y], color=scalar_map.to_rgba(colors), **bar_kws)
|
|
65
|
+
cax = inset_axes(ax, **inset_kws)
|
|
66
|
+
plt.colorbar(mappable=scalar_map, cax=cax, **cbar_kws)
|
|
66
67
|
|
|
67
68
|
|
|
68
69
|
def _plot(ax, df, _y, _color, plot_kws):
|
|
69
|
-
|
|
70
|
+
ax.plot(df.index, df[_y], color=_color, **plot_kws)
|
|
71
|
+
|
|
70
72
|
|
|
73
|
+
def process_timeseries_data(df, rolling=None, interpolate_limit=None):
|
|
74
|
+
# apply rolling window if specified
|
|
75
|
+
df = df.rolling(window=rolling, min_periods=1).mean(numeric_only=True) if rolling is not None else df
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
[label for axes in axes_list for label in axes.get_legend_handles_labels()[1]]
|
|
76
|
-
)
|
|
77
|
+
# apply interpolation if specified
|
|
78
|
+
df = df.interpolate(method='time', limit=interpolate_limit) if interpolate_limit is not None else df
|
|
79
|
+
return df
|
|
77
80
|
|
|
78
81
|
|
|
79
|
-
@set_figure(
|
|
82
|
+
@set_figure(autolayout=False)
|
|
80
83
|
def timeseries(df: DataFrame,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
84
|
+
y: list[str] | str,
|
|
85
|
+
y2: list[str] | str = None,
|
|
86
|
+
yi: list[str] | str = None,
|
|
87
|
+
color: list[str] | str | None = None,
|
|
88
|
+
label: list[str] | str | None = None,
|
|
89
|
+
rolling: int | str | None = 3,
|
|
90
|
+
interpolate_limit: int | None = 6,
|
|
91
|
+
major_freq: str = '1MS',
|
|
92
|
+
minor_freq: str = '10d',
|
|
93
|
+
style: list[Literal['scatter', 'bar', 'line']] | str | None = None,
|
|
94
|
+
ax: Axes | None = None,
|
|
95
|
+
set_xaxis_visible: bool | None = None,
|
|
96
|
+
legend_loc: Literal['best', 'upper right', 'upper left', 'lower left', 'lower right'] = 'best',
|
|
97
|
+
legend_ncol: int = 1,
|
|
98
|
+
**kwargs
|
|
99
|
+
) -> tuple[Figure, Axes]:
|
|
100
|
+
"""
|
|
101
|
+
Plot the timeseries data with the option of scatterplot, barplot, and lineplot.
|
|
102
|
+
|
|
103
|
+
Parameters
|
|
104
|
+
-----------
|
|
105
|
+
df : DataFrame
|
|
106
|
+
The data to plot.
|
|
107
|
+
y : list[str] | str
|
|
108
|
+
The primary y-axis data columns.
|
|
109
|
+
y2 : list[str] | str, optional
|
|
110
|
+
The secondary y-axis data columns. Defaults to None.
|
|
111
|
+
yi : list[str] | str, optional
|
|
112
|
+
The components for percentage calculation. Defaults to None.
|
|
113
|
+
color : str, optional
|
|
114
|
+
The column for color mapping or the color. Defaults to None.
|
|
115
|
+
label : str, optional
|
|
116
|
+
The label for the legend. Defaults to None.
|
|
117
|
+
rolling : str | int | None, optional
|
|
118
|
+
Rolling window size for smoothing. Defaults to None.
|
|
119
|
+
interpolate_limit : int, optional
|
|
120
|
+
Interpolation limit for missing values. Defaults to None.
|
|
121
|
+
major_freq : str, optional
|
|
122
|
+
Frequency for x-axis ticks. Defaults to '1MS'.
|
|
123
|
+
minor_freq : str, optional
|
|
124
|
+
Frequency for x-axis minor ticks. Defaults to '10d'.
|
|
125
|
+
style : Literal['scatter', 'bar', 'line'] | None, optional
|
|
126
|
+
Style of the plot. Defaults to 'scatter'.
|
|
127
|
+
ax : Axes | None, optional
|
|
128
|
+
Matplotlib Axes object to plot on. Defaults to None.
|
|
129
|
+
set_xaxis_visible : bool | None, optional
|
|
130
|
+
Whether to set x-axis visibility. Defaults to None.
|
|
131
|
+
legend_loc : Literal['best', 'upper right', 'upper left', 'lower left', 'lower right'], optional
|
|
132
|
+
Location of the legend. Defaults to 'best'.
|
|
133
|
+
legend_ncol : int, optional
|
|
134
|
+
Number of columns in the legend. Defaults to 1.
|
|
135
|
+
**kwargs : Additional keyword arguments for customization.
|
|
136
|
+
fig_kws : dict, optional
|
|
137
|
+
Additional keyword arguments for the figure. Defaults to {}.
|
|
138
|
+
scatter_kws : dict, optional
|
|
139
|
+
Additional keyword arguments for the scatter plot. Defaults to {}.
|
|
140
|
+
bar_kws : dict, optional
|
|
141
|
+
Additional keyword arguments for the bar plot. Defaults to {}.
|
|
142
|
+
ax_plot_kws : dict, optional
|
|
143
|
+
Additional keyword arguments for the primary y-axis plot. Defaults to {}.
|
|
144
|
+
ax2_plot_kws : dict, optional
|
|
145
|
+
Additional keyword arguments for the secondary y-axis plot. Defaults to {}.
|
|
146
|
+
cbar_kws : dict, optional
|
|
147
|
+
Additional keyword arguments for the colorbar. Defaults to {}.
|
|
148
|
+
inset_kws : dict, optional
|
|
149
|
+
Additional keyword arguments for the inset axes. Defaults to {}.
|
|
150
|
+
|
|
151
|
+
Returns
|
|
152
|
+
-------
|
|
153
|
+
ax : AxesSubplot
|
|
154
|
+
Matplotlib AxesSubplot.
|
|
155
|
+
|
|
156
|
+
Example
|
|
157
|
+
-------
|
|
158
|
+
>>> timeseries(df, y='WS', color='WD', scatter_kws=dict(cmap='hsv'), cbar_kws=dict(ticks=[0, 90, 180, 270, 360]), ylim=[0, None])
|
|
159
|
+
"""
|
|
160
|
+
# Set the time
|
|
161
|
+
try:
|
|
162
|
+
st_tm, fn_tm = df.index[0], df.index[-1]
|
|
163
|
+
except IndexError:
|
|
164
|
+
raise IndexError("The DataFrame is empty. Please provide a valid DataFrame.")
|
|
165
|
+
|
|
166
|
+
# calculate the percentage of each component
|
|
167
|
+
if yi is not None:
|
|
168
|
+
df_pct = df[yi].div(df[yi].sum(axis=1), axis=0) * 100
|
|
169
|
+
mean = [f"{_label} : {df[comp].mean():.2f}" for _label, comp in zip(label, yi)]
|
|
170
|
+
pct = [f"{_label} : {df_pct[comp].mean():.2f}%" for _label, comp in zip(label, yi)]
|
|
171
|
+
df_pct = process_timeseries_data(df_pct, rolling, interpolate_limit)
|
|
172
|
+
|
|
173
|
+
# process data
|
|
174
|
+
df = process_timeseries_data(df, rolling, interpolate_limit)
|
|
175
|
+
|
|
176
|
+
# Initialize figure and axis if not provided
|
|
177
|
+
fig, ax = plt.subplots(**{**{'figsize': (6, 2)}, **kwargs.get('fig_kws', {})}) if ax is None else (
|
|
178
|
+
ax.get_figure(), ax)
|
|
179
|
+
|
|
180
|
+
# Ensure y, y2, c, and style are lists
|
|
181
|
+
y = [y] if isinstance(y, str) else y
|
|
182
|
+
y2 = [y2] if isinstance(y2, str) else y2 if y2 is not None else []
|
|
183
|
+
color = [color] if isinstance(color, str) else color if color is not None else [None] * (len(y) + len(y2))
|
|
184
|
+
label = [label] if isinstance(label, str) else label if label is not None else [None] * (len(y) + len(y2))
|
|
185
|
+
style = [style] if isinstance(style, str) else style if style is not None else ['plot'] * (len(y) + len(y2))
|
|
186
|
+
|
|
187
|
+
for name, lst in [("c", color), ("style", style), ("label", label)]:
|
|
188
|
+
if len(lst) != len(y) + len(y2):
|
|
189
|
+
raise ValueError(f"The length of {name} must match the combined length of y and y2")
|
|
190
|
+
|
|
191
|
+
# Create a secondary y-axis if y2 is not empty
|
|
192
|
+
ax2 = ax.twinx() if y2 else None
|
|
193
|
+
|
|
194
|
+
# # Set color cycle
|
|
195
|
+
ax.set_prop_cycle(Color.color_cycle)
|
|
196
|
+
if y2:
|
|
197
|
+
ax2.set_prop_cycle(Color.color_cycle[len(y):])
|
|
198
|
+
|
|
199
|
+
if y2 and ('scatter' or 'bar') in style:
|
|
200
|
+
fig.subplots_adjust(right=0.8)
|
|
201
|
+
|
|
202
|
+
for i, _c in enumerate(color):
|
|
203
|
+
if _c is not None and _c in df.columns:
|
|
204
|
+
style[i] = 'scatter'
|
|
205
|
+
|
|
206
|
+
for i, (_y, _c, _label, _style) in enumerate(zip(y, color, label, style)):
|
|
207
|
+
scatter_kws = {**default_scatter_kws, **{'label': Unit(_y)}, **kwargs.get('scatter_kws', {})}
|
|
208
|
+
bar_kws = {**default_bar_kws, **{'label': Unit(_y)}, **kwargs.get('bar_kws', {})}
|
|
209
|
+
plot_kws = {**default_plot_kws, **{'label': Unit(_y)}, **kwargs.get('plot_kws', {})}
|
|
210
|
+
|
|
211
|
+
if _style in ['scatter', 'bar']:
|
|
212
|
+
cbar_kws = {**default_cbar_kws, **{'label': Unit(_c), 'ticks': None}, **kwargs.get('cbar_kws', {})}
|
|
213
|
+
inset_kws = {**default_insert_kws, **{'bbox_transform': ax.transAxes}, **kwargs.get('inset_kws', {})}
|
|
214
|
+
|
|
215
|
+
if _style == 'scatter':
|
|
216
|
+
_scatter(ax, df, _y, _c, scatter_kws, cbar_kws, inset_kws)
|
|
217
|
+
|
|
218
|
+
elif _style == 'bar':
|
|
219
|
+
_bar(ax, df, _y, _c, bar_kws, cbar_kws, inset_kws)
|
|
220
|
+
|
|
221
|
+
else:
|
|
222
|
+
_plot(ax, df, _y, _c, plot_kws)
|
|
223
|
+
|
|
224
|
+
if y2:
|
|
225
|
+
for i, (_y, _c, _style) in enumerate(zip(y2, color[len(y):], style[len(y):])):
|
|
226
|
+
scatter_kws = {**default_scatter_kws, **{'label': Unit(_y)}, **kwargs.get('scatter_kws2', {})}
|
|
227
|
+
bar_kws = {**default_bar_kws, **{'label': Unit(_y)}, **kwargs.get('bar_kws2', {})}
|
|
228
|
+
plot_kws = {**default_plot_kws, **{'label': Unit(_y)}, **kwargs.get('plot_kws2', {})}
|
|
229
|
+
|
|
230
|
+
if _style in ['scatter', 'bar']:
|
|
231
|
+
cbar_kws = {**default_cbar_kws, **{'label': Unit(_c), 'ticks': None}, **kwargs.get('cbar_kws2', {})}
|
|
232
|
+
inset_kws = {**default_insert_kws, **{'bbox_transform': ax.transAxes}, **kwargs.get('inset_kws2', {})}
|
|
233
|
+
|
|
234
|
+
if _style == 'scatter':
|
|
235
|
+
_scatter(ax2, df, _y, _c, scatter_kws, cbar_kws, inset_kws)
|
|
236
|
+
|
|
237
|
+
elif _style == 'bar':
|
|
238
|
+
_bar(ax2, df, _y, _c, bar_kws, cbar_kws, inset_kws)
|
|
239
|
+
|
|
240
|
+
else: # line plot
|
|
241
|
+
_plot(ax2, df, _y, _c, plot_kws)
|
|
242
|
+
|
|
243
|
+
# Combine legends from ax and ax2
|
|
244
|
+
ax.legend(*combine_legends([ax, ax2]), loc=legend_loc, ncol=legend_ncol)
|
|
245
|
+
|
|
246
|
+
else:
|
|
247
|
+
ax.legend(loc=legend_loc, ncol=legend_ncol)
|
|
248
|
+
|
|
249
|
+
if set_xaxis_visible is not None:
|
|
250
|
+
ax.axes.xaxis.set_visible(set_xaxis_visible)
|
|
251
|
+
|
|
252
|
+
ax.set(xlabel=kwargs.get('xlabel', ''),
|
|
253
|
+
ylabel=kwargs.get('ylabel', Unit(y) if isinstance(y, str) else Unit(y[0])),
|
|
254
|
+
xlim=kwargs.get('xlim', (st_tm, fn_tm)),
|
|
255
|
+
ylim=kwargs.get('ylim', (None, None)),
|
|
256
|
+
title=kwargs.get('title', '')
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
xticks = kwargs.get('xticks', date_range(start=st_tm, end=fn_tm, freq=major_freq))
|
|
260
|
+
minor_xticks = kwargs.get('minor_xticks', date_range(start=st_tm, end=fn_tm, freq=minor_freq))
|
|
261
|
+
|
|
262
|
+
ax.set_xticks(ticks=xticks, labels=xticks.strftime("%F"))
|
|
263
|
+
ax.set_xticks(minor_xticks, minor=True)
|
|
264
|
+
|
|
265
|
+
if y2:
|
|
266
|
+
ax2.set(ylim=kwargs.get('ylim2', (None, None)),
|
|
267
|
+
ylabel=kwargs.get('ylabel2', Unit(y2) if isinstance(y2, str) else Unit(y2[0]))
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
plt.show()
|
|
271
|
+
|
|
272
|
+
return fig, ax
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@set_figure(autolayout=False)
|
|
276
|
+
def timeseries_stacked(df,
|
|
277
|
+
y: list[str] | str,
|
|
278
|
+
yi: list[str] | str,
|
|
279
|
+
label: list[str] | str,
|
|
280
|
+
rolling: int | str | None = 6,
|
|
281
|
+
interpolate_limit: int | None = 6,
|
|
282
|
+
major_freq: str = '1MS',
|
|
283
|
+
minor_freq: str = '10d',
|
|
284
|
+
ax: Axes | None = None,
|
|
285
|
+
legend_ncol: int = 1,
|
|
286
|
+
**kwargs
|
|
287
|
+
) -> tuple[Figure, Axes]:
|
|
288
|
+
try:
|
|
289
|
+
st_tm, fn_tm = df.index[0], df.index[-1]
|
|
290
|
+
except IndexError:
|
|
291
|
+
raise IndexError("The DataFrame is empty. Please provide a valid DataFrame.")
|
|
292
|
+
|
|
293
|
+
# calculate the percentage of each component
|
|
294
|
+
df_pct = df[yi].div(df[yi].sum(axis=1), axis=0) * 100
|
|
295
|
+
mean = [f"{_label} : {df[comp].mean():.2f}" for _label, comp in zip(label, yi)]
|
|
296
|
+
pct = [f"{_label} : {df_pct[comp].mean():.2f}%" for _label, comp in zip(label, yi)]
|
|
297
|
+
|
|
298
|
+
# process data
|
|
299
|
+
df = process_timeseries_data(df, rolling, interpolate_limit)
|
|
300
|
+
df_pct = process_timeseries_data(df_pct, rolling, interpolate_limit)
|
|
301
|
+
|
|
302
|
+
fig, (ax1, ax2) = plt.subplots(2, 1, **{**{'figsize': (12, 3)}, **kwargs.get('fig_kws', {})})
|
|
303
|
+
|
|
304
|
+
width = 0.0417
|
|
305
|
+
color = Color.colors1
|
|
306
|
+
|
|
307
|
+
for name, lst in [("color", color), ("label", label)]:
|
|
308
|
+
if len(lst) != len(yi):
|
|
309
|
+
raise ValueError(f"The length of {name} must match the combined length of y and y2")
|
|
310
|
+
|
|
311
|
+
bottom = None # 初始化堆疊位置
|
|
312
|
+
for i, (_column, _color, _label) in enumerate(zip(yi, color, mean)):
|
|
313
|
+
if i == 0:
|
|
314
|
+
bottom = df[_column] * 0 # 第一個堆疊底部為零
|
|
315
|
+
ax1.bar(df.index, df[_column], color=_color, width=width, bottom=bottom, label=_label)
|
|
316
|
+
bottom += df[_column] # 更新堆疊底部位置
|
|
317
|
+
|
|
318
|
+
ax1.legend(loc='upper left', ncol=legend_ncol, prop={'weight': 'bold'}, bbox_to_anchor=(1, 0, 1.2, 1))
|
|
319
|
+
|
|
320
|
+
ax1.axes.xaxis.set_visible(False)
|
|
321
|
+
|
|
322
|
+
ax1.set(xlabel=kwargs.get('xlabel', ''),
|
|
323
|
+
ylabel=kwargs.get('ylabel', Unit(y) if isinstance(y, str) else Unit(y[0])),
|
|
324
|
+
xlim=kwargs.get('xlim', (st_tm, fn_tm)),
|
|
325
|
+
ylim=kwargs.get('ylim', (None, None)),
|
|
326
|
+
title=kwargs.get('title', ''),
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
xticks = kwargs.get('xticks', date_range(start=st_tm, end=fn_tm, freq=major_freq))
|
|
330
|
+
yticks = kwargs.get('yticks', np.linspace(*ax1.get_ylim(), num=6))
|
|
331
|
+
minor_xticks = kwargs.get('minor_xticks', date_range(start=st_tm, end=fn_tm, freq=minor_freq))
|
|
332
|
+
|
|
333
|
+
ax1.set_xticks(ticks=xticks, labels=xticks.strftime("%F"))
|
|
334
|
+
ax1.set_yticks(ticks=yticks, labels=[f'{tick:.0f}' for tick in yticks])
|
|
335
|
+
ax1.set_xticks(minor_xticks, minor=True)
|
|
336
|
+
|
|
337
|
+
# ax2
|
|
338
|
+
bottom = None # 初始化堆疊位置
|
|
339
|
+
for i, (_column, _color, _label) in enumerate(zip(yi, color, pct)):
|
|
340
|
+
if i == 0:
|
|
341
|
+
bottom = df_pct[_column] * 0 # 第一個堆疊底部為零
|
|
342
|
+
ax2.bar(df_pct.index, df_pct[_column], color=_color, width=width, bottom=bottom, label=_label)
|
|
343
|
+
bottom += df_pct[_column] # 更新堆疊底部位置
|
|
344
|
+
|
|
345
|
+
ax2.legend(loc='upper left', ncol=legend_ncol, prop={'weight': 'bold'}, bbox_to_anchor=(1, 0, 1.2, 1))
|
|
346
|
+
|
|
347
|
+
ax2.set(xlabel=kwargs.get('xlabel', ''),
|
|
348
|
+
ylabel=kwargs.get('ylabel', 'Percentage (%)'),
|
|
349
|
+
xlim=kwargs.get('xlim', (st_tm, fn_tm)),
|
|
350
|
+
ylim=(0, 100),
|
|
351
|
+
title=kwargs.get('title', '')
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
xticks = kwargs.get('xticks', date_range(start=st_tm, end=fn_tm, freq=major_freq))
|
|
355
|
+
yticks = kwargs.get('yticks', np.linspace(*ax2.get_ylim(), num=6))
|
|
356
|
+
minor_xticks = kwargs.get('minor_xticks', date_range(start=st_tm, end=fn_tm, freq=minor_freq))
|
|
357
|
+
|
|
358
|
+
ax2.set_xticks(ticks=xticks, labels=xticks.strftime("%F"))
|
|
359
|
+
ax2.set_yticks(ticks=yticks, labels=[f'{tick:.0f}' for tick in yticks])
|
|
360
|
+
ax2.set_xticks(minor_xticks, minor=True)
|
|
361
|
+
|
|
362
|
+
return fig, ax1
|