Skip to content

Commit b18ef75

Browse files
committed
Scratch out plume plot demo
1 parent b8050ea commit b18ef75

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

docs/tutorials/pandas_accessor_tutorial.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import multiprocessing
3333
import traceback
3434

35+
import matplotlib.pyplot as plt
3536
import numpy as np
3637
import openscm_units
3738
import pandas as pd
@@ -170,7 +171,7 @@ def create_df(
170171
# %%
171172
small_ts = small_df.ct.to_timeseries(
172173
time_units="yr",
173-
interpolation=ct.InterpolationOption.PiecewiseConstantPreviousLeftClosed,
174+
interpolation=ct.InterpolationOption.Quadratic,
174175
)
175176
small_ts
176177

@@ -280,7 +281,7 @@ def create_df(
280281
# %%
281282
ax = (
282283
bigger_df.loc[pix.isin(variable="variable_1")]
283-
.groupby(["scenario", "variable", "units"], observed=True)
284+
.groupby(bigger_df.index.names.difference(["run"]), observed=True)
284285
.median()
285286
.loc[pix.ismatch(scenario="scenario_1*")]
286287
.ct.to_timeseries(
@@ -292,19 +293,20 @@ def create_df(
292293
ax.legend()
293294

294295
# %%
295-
# # Units don't round trip
296296
# pd.testing.assert_frame_equal(
297297
# small_df,
298-
# small_ts.ct.to_df()
298+
# # Units don't round trip by default
299+
# small_ts.ct.to_df(out_units="Mt / yr")
299300
# )
300-
small_ts.ct.to_df()
301+
small_ts.ct
301302

302303
# %%
303304
small_ts.ct.to_df(increase_resolution=3)
304305

305306
# %%
306307
sns_df = small_ts.loc[
307308
pix.isin(scenario=[f"scenario_{i}" for i in range(2)])
309+
# Rename to `to_tidy_df`
308310
].ct.to_sns_df(increase_resolution=100)
309311
sns_df
310312

@@ -319,10 +321,50 @@ def create_df(
319321
units="run",
320322
)
321323

324+
# %%
325+
plumes_over = ["run"]
326+
increase_resolution = 100
327+
quantiles_plumes = (
328+
(0.5, 0.8),
329+
((0.05, 0.95), 0.5),
330+
)
331+
332+
fig, ax = plt.subplots()
333+
for scenario, s_ts in small_ts.loc[pix.isin(variable="variable_0")].groupby("scenario", observed=True):
334+
for quantiles, alpha in quantiles_plumes:
335+
s_quants = s_ts.ct.to_df(increase_resolution=increase_resolution).groupby(small_ts.index.names.difference(plumes_over), observed=True).quantile(quantiles)
336+
if isinstance(quantiles, tuple):
337+
ax.fill_between(
338+
s_quants.columns.values.squeeze(),
339+
# As long as there are only two rows,
340+
# doesn't matter which way around you do this.
341+
s_quants.iloc[0, :].values.squeeze(),
342+
s_quants.iloc[1, :].values.squeeze(),
343+
alpha=alpha,
344+
# label=scenario,
345+
)
346+
else:
347+
ax.plot(
348+
s_quants.columns.values.squeeze(),
349+
s_quants.values.squeeze(),
350+
alpha=alpha,
351+
label=scenario,
352+
)
353+
354+
ax.legend()
355+
356+
# %%
357+
(
358+
small_ts
359+
.ct.to_df(increase_resolution=5)
360+
.groupby(small_ts.index.names.difference(["run"]), observed=True)
361+
.quantile([0.05, 0.5, 0.95])
362+
)
363+
322364
# %% [markdown]
323365
# - other operations, also with progress, parallel, parallel with progress
324366
# - plot with basic control over labels
325-
# - plot with grouping and plumes for ranges
367+
# - plot with grouping and plumes for ranges (basically reproduce scmdata API)
326368
# - convert with more fine-grained control over interpolation
327369
# (e.g. interpolation being passed as pd.Series)
328370
# - unit conversion

0 commit comments

Comments
 (0)