Skip to content

Commit

Permalink
Merge branch 'main' into 534_ao_extremes
Browse files Browse the repository at this point in the history
  • Loading branch information
lee1043 authored Nov 27, 2023
2 parents d4c62dc + 959a527 commit 340ed31
Show file tree
Hide file tree
Showing 10 changed files with 1,038 additions and 615 deletions.
1,178 changes: 739 additions & 439 deletions doc/jupyter/Demo/Demo_1b_mean_climate.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions doc/obs_info_dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@
},
"GPCP-2-3": {
"CMIP_CMOR_TABLE": "Amon",
"MD5sum": "3792901034585d3d495722f10a0dfecb",
"MD5sum": "036e14d0124f4d00921d58e3c1c3e9e1",
"RefTrackingDate": "Wed Aug 4 12:22:10 2021",
"filename": "pr_mon_GPCP-2-3_PCMDI_gn.200301-201812.AC.v20210804.nc",
"filename": "pr_monC_GPCP-2-3_PCMDI_gn_197901-201907.AC.v20230516.nc",
"period": "200301-201812",
"shape": "(12, 72, 144)",
"template": "pr/GPCP-2-3/v20210804/pr_mon_GPCP-2-3_PCMDI_gn.200301-201812.AC.v20210804.nc"
"template": "NOAA-NCEI/GPCP-2-3/monC/pr/gn/v20230516/pr_monC_GPCP-2-3_PCMDI_gn_197901-201907.AC.v20230516.nc"
},
"TRMM-3B43v-7": {
"CMIP_CMOR_TABLE": "Amon",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def parallel_coordinate_plot(
metric_names,
model_names,
models_to_highlight=list(),
models_to_highlight_by_line=True,
models_to_highlight_colors=None,
models_to_highlight_labels=None,
models_to_highlight_markers=["s", "o", "^", "*"],
models_to_highlight_markers_size=10,
fig=None,
ax=None,
figsize=(15, 5),
Expand All @@ -30,14 +33,19 @@ def parallel_coordinate_plot(
colormap="viridis",
num_color=20,
legend_off=False,
legend_ncol=6,
legend_bbox_to_anchor=(0.5, -0.14),
logo_rect=None,
logo_off=False,
model_names2=None,
group1_name="group1",
group2_name="group2",
comparing_models=None,
fill_between_lines=False,
fill_between_lines_colors=("green", "red"),
fill_between_lines_colors=("red", "green"),
arrow_between_lines=False,
arrow_between_lines_colors=("red", "green"),
arrow_alpha=1,
vertical_center=None,
vertical_center_line=False,
vertical_center_line_label=None,
Expand All @@ -50,9 +58,12 @@ def parallel_coordinate_plot(
- `data`: 2-d numpy array for metrics
- `metric_names`: list, names of metrics for individual vertical axes (axis=1)
- `model_names`: list, name of models for markers/lines (axis=0)
- `models_to_highlight`: list, default=None, List of models to highlight as lines
- `models_to_highlight`: list, default=None, List of models to highlight as lines or marker
- `models_to_highlight_by_line`: bool, default=True, highlight as lines. If False, as marker
- `models_to_highlight_colors`: list, default=None, List of colors for models to highlight as lines
- `models_to_highlight_labels`: list, default=None, List of string labels for models to highlight as lines
- `models_to_highlight_markers`: list, matplotlib markers for models to highlight if as marker
- `models_to_highlight_markers_size`: float, size of matplotlib markers for models to highlight if as marker
- `fig`: `matplotlib.figure` instance to which the parallel coordinate plot is plotted.
If not provided, use current axes or create a new one. Optional.
- `ax`: `matplotlib.axes.Axes` instance to which the parallel coordinate plot is plotted.
Expand All @@ -68,6 +79,8 @@ def parallel_coordinate_plot(
- `colormap`: string, default='viridis', matplotlib colormap
- `num_color`: integer, default=20, how many color to use.
- `legend_off`: bool, default=False, turn off legend
- `legend_ncol`: integer, default=6, number of columns for legend text
- `legend_bbox_to_anchor`: tuple, defulat=(0.5, -0.14), set legend box location
- `logo_rect`: sequence of float. The dimensions [left, bottom, width, height] of the new Axes.
All quantities are in fractions of figure width and height. Optional.
- `logo_off`: bool, default=False, turn off PMP logo
Expand All @@ -76,7 +89,10 @@ def parallel_coordinate_plot(
- `group2_name`: string, needed for violin plot legend if splited to two groups, for the 2nd group. Default is 'group2'.
- `comparing_models`: tuple or list containing two strings for models to compare with colors filled between the two lines.
- `fill_between_lines`: bool, default=False, fill color between lines for models in comparing_models
- `fill_between_lines_colors`: tuple or list containing two strings for colors filled between the two lines. Default=('green', 'red')
- `fill_between_lines_colors`: tuple or list containing two strings of colors for filled between the two lines. Default=('red', 'green')
- `arrow_between_lines`: bool, default=False, place arrows between two lines for models in comparing_models
- `arrow_between_lines_colors`: tuple or list containing two strings of colors for arrow between the two lines. Default=('red', 'green')
- `arrow_alpha`: float, default=1, transparency of arrow (faction between 0 to 1)
- `vertical_center`: string ("median", "mean")/float/integer, default=None, adjust range of vertical axis to set center of vertical axis as median, mean, or given number
- `vertical_center_line`: bool, default=False, show median as line
- `vertical_center_line_label`: str, default=None, label in legend for the horizontal vertical center line. If not given, it will be automatically assigned. It can be turned off by "off"
Expand Down Expand Up @@ -231,7 +247,18 @@ def parallel_coordinate_plot(
else:
label = model

ax.plot(range(N), zs[j, :], "-", c=color, label=label, lw=3)
if models_to_highlight_by_line:
ax.plot(range(N), zs[j, :], "-", c=color, label=label, lw=3)
else:
ax.plot(
range(N),
zs[j, :],
models_to_highlight_markers[mh_index],
c=color,
label=label,
markersize=models_to_highlight_markers_size,
)

mh_index += 1
else:
if identify_all_models:
Expand All @@ -251,8 +278,8 @@ def parallel_coordinate_plot(
vertical_center_line_label = None
ax.plot(range(N), zs_middle, "-", c="k", label=vertical_center_line_label, lw=1)

# Fill between lines
if fill_between_lines and (comparing_models is not None):
# Compare two models
if comparing_models is not None:
if isinstance(comparing_models, tuple) or (
isinstance(comparing_models, list) and len(comparing_models) == 2
):
Expand All @@ -261,24 +288,49 @@ def parallel_coordinate_plot(
m2 = model_names.index(comparing_models[1])
y1 = zs[m1, :]
y2 = zs[m2, :]
ax.fill_between(
x,
y1,
y2,
where=y2 >= y1,
facecolor=fill_between_lines_colors[0],
interpolate=True,
alpha=0.5,
)
ax.fill_between(
x,
y1,
y2,
where=y2 <= y1,
facecolor=fill_between_lines_colors[1],
interpolate=True,
alpha=0.5,
)

# Fill between lines
if fill_between_lines:
ax.fill_between(
x,
y1,
y2,
where=(y2 > y1),
facecolor=fill_between_lines_colors[0],
interpolate=False,
alpha=0.5,
)
ax.fill_between(
x,
y1,
y2,
where=(y2 < y1),
facecolor=fill_between_lines_colors[1],
interpolate=False,
alpha=0.5,
)

if arrow_between_lines:
# Add vertical arrows
for xi, yi1, yi2 in zip(x, y1, y2):
if yi2 > yi1:
arrow_color = arrow_between_lines_colors[0]
elif yi2 < yi1:
arrow_color = arrow_between_lines_colors[1]
else:
arrow_color = None
arrow_length = yi2 - yi1
ax.arrow(
xi,
yi1,
0,
arrow_length,
color=arrow_color,
length_includes_head=True,
alpha=arrow_alpha,
width=0.05,
head_width=0.15,
)

ax.set_xlim(-0.5, N - 0.5)
ax.set_xticks(range(N))
Expand All @@ -288,7 +340,10 @@ def parallel_coordinate_plot(
ax.set_title(title, fontsize=18)

if not legend_off:
ax.legend(loc="upper center", ncol=6, bbox_to_anchor=(0.5, -0.14))
# ax.legend(loc="upper center", ncol=6, bbox_to_anchor=(0.5, -0.14))
ax.legend(
loc="upper center", ncol=legend_ncol, bbox_to_anchor=legend_bbox_to_anchor
)

if not logo_off:
fig, ax = add_logo(fig, ax, logo_rect)
Expand Down
100 changes: 44 additions & 56 deletions pcmdi_metrics/graphics/portrait_plot/portrait_plot_example.ipynb

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions pcmdi_metrics/graphics/portrait_plot/portrait_plot_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def portrait_plot(
ax=None,
annotate=False,
annotate_data=None,
annotate_textcolors=("black", "white"),
annotate_textcolors_threshold=(-2, 2),
annotate_fontsize=15,
annotate_format="{x:.2f}",
figsize=(12, 10),
Expand Down Expand Up @@ -61,6 +63,8 @@ def portrait_plot(
- `annotate`: bool, default=False, add annotating text if true,
but work only for heatmap style map (i.e., no triangles)
- `annotate_data`: 2d numpy array, default=None. If None, the image's data is used. Optional.
- `annotate_textcolors`: Tuple. A pair of colors for annotation text. Default is ("black", "white")
- `annotate_textcolors_threshold`: Tuple or float. Value in data units according to which the colors from textcolors are applied. Default=(-2, 2)
- `annotate_fontsize`: number (int/float), default=15. Font size for annotation
- `annotate_format`: format for annotate value, default="{x:.2f}"
- `figsize`: tuple of two numbers (width, height), default=(12, 10), figure size in inches
Expand Down Expand Up @@ -169,13 +173,14 @@ def portrait_plot(
sys.exit("Error: annotate_data has different size than data")
else:
annotate_data = data
annotate_heatmap(
ax = annotate_heatmap(
im,
ax=ax,
data=data,
annotate_data=annotate_data,
valfmt=annotate_format,
threshold=(2, -2),
textcolors=annotate_textcolors,
threshold=annotate_textcolors_threshold,
fontsize=annotate_fontsize,
)

Expand Down Expand Up @@ -501,6 +506,8 @@ def annotate_heatmap(
text = ax.text(j + 0.5, i + 0.5, valfmt(annotate_data[i, j], None), **kw)
texts.append(text)

return ax


# ======================================================================
# Portrait plot 2 (two triangles)
Expand Down
8 changes: 6 additions & 2 deletions pcmdi_metrics/graphics/share/read_json_mean_clim.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ def extract_data(results_dict, var_list, region, stat, season, mip, debug=False)
Return a pandas dataframe for metric numbers at given region/stat/season.
Rows: models, Columns: variables (i.e., 2d array)
"""
model_list = sorted(list(results_dict[var_list[0]]["RESULTS"].keys()))
model_list = sorted(
list(results_dict[var_list[0]]["RESULTS"].keys()), key=str.casefold
)
# update model_list
if "rlut" in list(results_dict.keys()):
if "rlut" in list(results_dict["rlut"]["RESULTS"].keys()):
model_list = sorted(list(results_dict["rlut"]["RESULTS"].keys()))
model_list = sorted(
list(results_dict["rlut"]["RESULTS"].keys()), key=str.casefold
)

print("extract_data:: model_list: ", model_list)

Expand Down
Loading

0 comments on commit 340ed31

Please sign in to comment.