Skip to content

Commit 0811305

Browse files
Fix premature plt.close() resulting in no subplots saved (#989)
* Fix premature `plt.close()` resulting in no subplots saved * Fix docstring parameter spacing
1 parent 00839fa commit 0811305

22 files changed

+182
-109
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[#]
2+
sets = ["zonal_mean_2d"]
3+
case_id = "MERRA2"
4+
variables = ["T"]
5+
ref_name = "MERRA2"
6+
reference_name = "MERRA2 Reanalysis"
7+
seasons = ["ANN", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "DJF", "MAM", "JJA", "SON"]
8+
contour_levels = [180,185,190,200,210,220,230,240,250,260,270,280,290,295,300]
9+
diff_levels = [-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7]
10+
11+
[#]
12+
sets = ["zonal_mean_2d"]
13+
case_id = "MERRA2"
14+
variables = ["U"]
15+
ref_name = "MERRA2"
16+
reference_name = "MERRA2 Reanalysis"
17+
seasons = ["ANN", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "DJF", "MAM", "JJA", "SON"]
18+
test_colormap = "PiYG_r"
19+
reference_colormap = "PiYG_r"
20+
contour_levels = [-40,-30,-25,-20,-15,-10,-5,-2.5,2.5,5,10,15,20,25,30,40]
21+
diff_levels = [-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from e3sm_diags.parameter.zonal_mean_2d_parameter import ZonalMean2dParameter
2+
from e3sm_diags.run import runner
3+
import sys
4+
5+
param = ZonalMean2dParameter()
6+
7+
param.reference_data_path = '/lcrc/soft/climate/e3sm_diags_data/obs_for_e3sm_diags/climatology'
8+
param.test_data_path = '/lcrc/group/e3sm/ac.zhang40/example_v3/v3.LR.historical_0051/post/atm/180x360_aave/clim/30yr'
9+
param.results_dir = '/lcrc/group/e3sm/public_html/diagnostic_output/ac.tvo/tests/pdf_size_1'
10+
param.case_id = 'ERA5'
11+
param.run_type = 'model_vs_obs'
12+
param.sets = ['zonal_mean_2d']
13+
param.variables = ['T']
14+
param.seasons = ['ANN']
15+
param.regions = ['global']
16+
param.plevs = [50.0, 100.0, 150.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0, 950.0, 1000.0]
17+
param.multiprocessing = True
18+
param.num_workers = 8
19+
param.main_title = 'T ANN global'
20+
param.backend = 'cartopy'
21+
param.output_format = ['png']
22+
param.canvas_size_w = 1212
23+
param.canvas_size_h = 1628
24+
param.figsize = [8.5, 11.0]
25+
param.dpi = 150
26+
param.arrows = True
27+
param.contour_levels = [180, 185, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 295, 300]
28+
param.test_name = 'v3.LR.historical_0051'
29+
param.short_test_name = 'v3.LR.historical_0201'
30+
param.test_colormap = 'cet_rainbow.rgb'
31+
param.ref_name = 'ERA5'
32+
param.reference_name = 'ERA5 Reanalysis'
33+
param.reference_colormap = 'cet_rainbow.rgb'
34+
param.diff_title = 'Model - Observations'
35+
param.diff_colormap = 'diverging_bwr.rgb'
36+
param.diff_levels = [-7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7]
37+
param.granulate = ['variables', 'seasons', 'regions']
38+
param.selectors = ['sets', 'seasons']
39+
param.save_netcdf = True
40+
param.output_format_subplot = ['pdf']
41+
42+
# cfg_filepath = 'auxiliary_tools/debug/987-pdf-no-plots/mvce.cfg'
43+
# sys.argv.extend(['-d', cfg_filepath])
44+
runner.sets_to_run = ['zonal_mean_2d']
45+
runner.run_diags([param])

e3sm_diags/plot/aerosol_aeronet_plot.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@
2626
BORDER_PADDING_SCATTER = (-0.08, -0.04, 0.15, 0.04)
2727

2828

29-
def _save_plot_aerosol_aeronet(fig, parameter):
30-
"""Save aerosol_aeronet plots using the _save_single_subplot helper function.
31-
32-
This function handles the special case where different border padding is needed
33-
for each panel by calling _save_single_subplot twice with panel-specific
34-
configurations (BORDER_PADDING_COLORMAP for panel 0, BORDER_PADDING_SCATTER for panel 1).
35-
"""
36-
# Save the main plot
37-
_save_main_plot(parameter)
38-
39-
# Save subplots with different border padding by calling general function
40-
# for each panel individually
41-
if parameter.output_format_subplot:
42-
# Save colormap panel (panel 0) with its specific border padding
43-
_save_single_subplot(fig, parameter, 0, PANEL_CFG[0], BORDER_PADDING_COLORMAP)
44-
45-
# Save scatter panel (panel 1) with its specific border padding
46-
_save_single_subplot(fig, parameter, 1, PANEL_CFG[1], BORDER_PADDING_SCATTER)
47-
48-
4929
def plot(
5030
parameter: CoreParameter,
5131
da_test: xr.DataArray,
@@ -130,3 +110,25 @@ def plot(
130110
plt.loglog(ref_site_arr, test_site_arr, "kx", markersize=3.0, mfc="none")
131111

132112
_save_plot_aerosol_aeronet(fig, parameter)
113+
114+
plt.close(fig)
115+
116+
117+
def _save_plot_aerosol_aeronet(fig, parameter):
118+
"""Save aerosol_aeronet plots using the _save_single_subplot helper function.
119+
120+
This function handles the special case where different border padding is needed
121+
for each panel by calling _save_single_subplot twice with panel-specific
122+
configurations (BORDER_PADDING_COLORMAP for panel 0, BORDER_PADDING_SCATTER for panel 1).
123+
"""
124+
# Save the main plot
125+
_save_main_plot(parameter)
126+
127+
# Save subplots with different border padding by calling general function
128+
# for each panel individually
129+
if parameter.output_format_subplot:
130+
# Save colormap panel (panel 0) with its specific border padding
131+
_save_single_subplot(fig, parameter, 0, PANEL_CFG[0], BORDER_PADDING_COLORMAP)
132+
133+
# Save scatter panel (panel 1) with its specific border padding
134+
_save_single_subplot(fig, parameter, 1, PANEL_CFG[1], BORDER_PADDING_SCATTER)

e3sm_diags/plot/annual_cycle_zonal_mean_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def plot(
8686

8787
_save_plot(fig, parameter)
8888

89-
plt.close()
89+
plt.close(fig)
9090

9191

9292
def _add_colormap(

e3sm_diags/plot/area_mean_time_series_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def plot(
107107
parameter.output_file = var
108108
_save_plot(fig, parameter, PANEL_CFG, BORDER_PADDING)
109109

110-
plt.close()
110+
plt.close(fig)
111111

112112

113113
def _get_mean_and_std_label(var: xr.DataArray, name: str) -> str:

e3sm_diags/plot/arm_diags_plot.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def _plot_diurnal_cycle(parameter: ARMDiagsParameter, vars_to_data: RefsTestMetr
131131
plt.title(output_file_name.replace("-", " "))
132132

133133
_save_plots(parameter, output_file_name, parameter.output_format)
134-
plt.close()
134+
135+
plt.close(fig)
135136

136137

137138
def _plot_diurnal_cycle_zt(parameter: ARMDiagsParameter, vars_to_data: RefsTestMetrics):
@@ -221,7 +222,7 @@ def _plot_diurnal_cycle_zt(parameter: ARMDiagsParameter, vars_to_data: RefsTestM
221222
output_file_name = parameter.output_file + "-" + data_name
222223
_save_plots(parameter, output_file_name, parameter.output_format)
223224

224-
plt.close()
225+
plt.close(fig)
225226

226227

227228
def _plot_convection_onset_statistics(
@@ -524,7 +525,7 @@ def _plot_convection_onset_statistics(
524525
bbox_inches="tight",
525526
)
526527

527-
plt.close()
528+
plt.close(fig)
528529

529530

530531
def _get_seasonal_mean(data: np.ndarray) -> np.ndarray:
@@ -618,7 +619,7 @@ def _plot_annual_cycle(
618619
plt.title(output_file_name.replace("-", " "))
619620
_save_plots(parameter, output_file_name, parameter.output_format)
620621

621-
plt.close()
622+
plt.close(fig)
622623

623624

624625
def _plot_aerosol_activation(
@@ -774,7 +775,7 @@ def _subplot_aerosol_ccn(
774775
bbox_inches="tight",
775776
)
776777

777-
plt.close()
778+
plt.close(fig)
778779

779780

780781
def _save_plots(

e3sm_diags/plot/cosp_histogram_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def plot(
6868

6969
_save_plot(fig, parameter, PANEL_CFG, BORDER_PADDING)
7070

71-
plt.close()
71+
plt.close(fig)
7272

7373

7474
def _add_colormap(

e3sm_diags/plot/diurnal_cycle_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def plot(
6464

6565
_save_plot(fig, parameter, PANEL_CFG)
6666

67-
plt.close()
67+
plt.close(fig)
6868

6969

7070
def _add_colormap(

e3sm_diags/plot/enso_diags_plot.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@
4848
ENSO_BORDER_PADDING_MAP = (-0.07, -0.025, 0.17, 0.022)
4949

5050

51-
def _save_plot_scatter(fig: plt.Figure, parameter: EnsoDiagsParameter):
52-
"""Save the scatter plot using the shared _save_single_subplot function."""
53-
_save_main_plot(parameter)
54-
55-
# Save the single subplot using shared helper (panel_config=None for full figure)
56-
if parameter.output_format_subplot:
57-
_save_single_subplot(fig, parameter, 0, None, None)
58-
59-
6051
def plot_scatter(
6152
parameter: EnsoDiagsParameter, x: MetricsDictScatter, y: MetricsDictScatter
6253
):
@@ -154,8 +145,7 @@ def plot_scatter(
154145
plt.legend()
155146

156147
_save_plot_scatter(fig, parameter)
157-
158-
plt.close()
148+
plt.close(fig)
159149

160150

161151
def plot_map(
@@ -209,7 +199,7 @@ def plot_map(
209199

210200
_save_plot(fig, parameter, DEFAULT_PANEL_CFG, ENSO_BORDER_PADDING_MAP)
211201

212-
plt.close()
202+
plt.close(fig)
213203

214204

215205
def _add_colormap(
@@ -413,3 +403,12 @@ def _get_contour_label_format_and_pad(c_levels: List[float]) -> Tuple[str, int]:
413403
pad = 30
414404

415405
return fmt, pad
406+
407+
408+
def _save_plot_scatter(fig: plt.Figure, parameter: EnsoDiagsParameter):
409+
"""Save the scatter plot using the shared _save_single_subplot function."""
410+
_save_main_plot(parameter)
411+
412+
# Save the single subplot using shared helper (panel_config=None for full figure)
413+
if parameter.output_format_subplot:
414+
_save_single_subplot(fig, parameter, 0, None, None)

e3sm_diags/plot/lat_lon_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def plot(
119119

120120
_save_plot(fig, parameter)
121121

122-
plt.close()
122+
plt.close(fig)
123123

124124

125125
def _add_colormap(

0 commit comments

Comments
 (0)