Skip to content

Commit 5370a52

Browse files
authored
Update global time series plotting (#301)
1 parent 80f7e68 commit 5370a52

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

docs/source/dev_guide/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Developer Guide
1010
release
1111
testing_e3sm_unified
1212
new_diags_set
13+
new_glb_plot
1314
new_task
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
***************************************
2+
Adding a new plot to Global Time Series
3+
***************************************
4+
5+
This guide gives a general list of things to consider when adding a new
6+
Global Time Series plot. The exact code changes required will differ amongst plots.
7+
8+
- Add the code for your new plot under "Plotting functions" in `coupled_global.py <https://github.com/E3SM-Project/zppy/blob/main/zppy/templates/coupled_global.py>`_. The name of your function should start with ``plot_``.
9+
- In ``PLOT_DICT``, add ``"function name without plot_" : function name``.
10+
- If this plot should be added to the defaults, complete this step: for ``plot_names`` under ``[global_time_series]`` in `default.ini <https://github.com/E3SM-Project/zppy/blob/main/zppy/templates/default.ini>`_, add the function name without plot to the string.
11+
- Run the `integration tests <https://e3sm-project.github.io/zppy/_build/html/main/dev_guide/testing.html#integration-tests>`_ and examine the differences from the expected files. If they match what you expect, update the expected files following "Commands to run to replace outdated expected files" on the machine-specific directions.

zppy/templates/coupled_global.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, List, Tuple
66

77
import matplotlib as mpl
8+
import matplotlib.backends.backend_pdf
89
import matplotlib.pyplot as plt
910
import numpy as np
1011
from netCDF4 import Dataset
@@ -529,22 +530,34 @@ def run(parameters):
529530

530531
xlim = [float(year1), float(year2)]
531532

532-
fig = plt.figure(figsize=[13.5, 16.5])
533533
num_plots = len(plot_list)
534-
nrows = math.ceil(num_plots / 2)
534+
nrows = 4
535535
ncols = 2
536-
537-
for i in range(num_plots):
538-
ax = plt.subplot(nrows, ncols, i + 1)
539-
try:
540-
PLOT_DICT[plot_list[i]](ax, xlim, exps)
541-
except KeyError:
542-
raise KeyError(f"Invalid plot name: {plot_list[i]}")
543-
544-
fig.tight_layout()
545-
fig.savefig(figstr + ".pdf")
546-
fig.savefig(figstr + ".png", dpi=150)
547-
plt.clf()
536+
plots_per_page = nrows * ncols
537+
num_pages = math.ceil(num_plots / plots_per_page)
538+
539+
i = 0
540+
# https://stackoverflow.com/questions/58738992/save-multiple-figures-with-subplots-into-a-pdf-with-multiple-pages
541+
pdf = matplotlib.backends.backend_pdf.PdfPages(f"{figstr}.pdf")
542+
for page in range(num_pages):
543+
fig = plt.figure(1, figsize=[13.5, 16.5])
544+
for j in range(plots_per_page):
545+
if i < num_plots:
546+
ax = plt.subplot(nrows, ncols, j + 1)
547+
try:
548+
PLOT_DICT[plot_list[i]](ax, xlim, exps)
549+
except KeyError:
550+
raise KeyError(f"Invalid plot name: {plot_list[i]}")
551+
i += 1
552+
553+
fig.tight_layout()
554+
pdf.savefig(1)
555+
if num_pages > 1:
556+
fig.savefig(figstr + f"_{page}.png", dpi=150)
557+
else:
558+
fig.savefig(figstr + ".png", dpi=150)
559+
plt.clf()
560+
pdf.close()
548561

549562

550563
if __name__ == "__main__":

zppy/templates/global_time_series.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ results_dir={{ prefix }}_results
100100
results_dir_absolute_path={{ scriptDir }}/${results_dir}
101101
mkdir -p ${results_dir_absolute_path}
102102
cp ${figstr}.pdf ${results_dir_absolute_path}/${figstr}.pdf
103-
cp ${figstr}.png ${results_dir_absolute_path}/${figstr}.png
103+
cp *.png ${results_dir_absolute_path}
104104

105105
################################################################################
106106
# Copy output to web server

0 commit comments

Comments
 (0)