-
Notifications
You must be signed in to change notification settings - Fork 34
Fix premature plt.close() resulting in no subplots saved
#989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @chengzhuzhang, this PR is ready for review. Subplots are now generated in the PDF.
Please merge when ready.
Full directory:
https://web.lcrc.anl.gov/public/e3sm/diagnostic_output/ac.tvo/tests/pdf_size_1/zonal_mean_2d/ERA5/
| return time | ||
|
|
||
|
|
||
| def _get_time_freq_and_start_time(time: xr.DataArray) -> Tuple[int, int]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix mypy pre-commit error.
|
|
||
| _save_plot_aerosol_aeronet(fig, parameter) | ||
|
|
||
| plt.close(fig) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example of a change to call plt.close(fig) AFTER saving all possible plots.
| def _save_plot_scatter(fig: plt.Figure, parameter: EnsoDiagsParameter): | ||
| """Save the scatter plot using the shared _save_single_subplot function.""" | ||
| _save_main_plot(parameter) | ||
|
|
||
| # Save the single subplot using shared helper (panel_config=None for full figure) | ||
| if parameter.output_format_subplot: | ||
| _save_single_subplot(fig, parameter, 0, None, None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved these kinds of functions at the bottom because they are the last called private function, makes it easier to read file.
|
|
||
| logger.info(f"Plot saved in: {filepath}") | ||
|
|
||
| plt.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is what causes subsequent subplots to be missing in the PDF.
3af5f5e to
40b04eb
Compare
chengzhuzhang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-ran the tropical_subseasonal set, and now the pdfs look as expected.
Description
Related to #987.
In #984, I refactored plotting utilities to make them more maintainable by sharing logic across plotters.
During refactoring, I added a call to
plt.close()in_save_main_plot()(here). This prematurely closes the active plot figure, which results in subsequent calls to thepltlibrary to produce blank figures -- including subplots in PDF format.This PR now calls
plt.close(fig)after all plots have been saved. It also replacesplt.close()withplt.close(fig)to ensure that the correct figure is closed.Checklist
If applicable: