🐛 Fix stacked plots functionality by ensuring multiple output var…#4068
🐛 Fix stacked plots functionality by ensuring multiple output var…#4068timothy-nunn merged 4 commits intomainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4068 +/- ##
==========================================
- Coverage 49.44% 49.44% -0.01%
==========================================
Files 149 149
Lines 29796 29798 +2
==========================================
Hits 14734 14734
- Misses 15062 15064 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
timothy-nunn
left a comment
There was a problem hiding this comment.
Can you give me an example of the bug this fixes please (an MFile + commands to run)... its not immediately obvious what the problem was and how this fixes it.
process/io/plot_scans.py
Outdated
| sharex=True, | ||
| ) | ||
| fig.subplots_adjust(hspace=0.0) | ||
| pass |
There was a problem hiding this comment.
Please rewrite this if statement to avoid just having a pass
timothy-nunn
left a comment
There was a problem hiding this comment.
The example you provided me still produces 3 files rather than the desired 1.
c980191 to
4032908
Compare
There was a problem hiding this comment.
Pull request overview
Refactors 1D scan plotting in process/core/io/plot_scans.py to improve stacked-plot handling by creating subplot grids only when needed and aiming to avoid repeated subplot creation.
Changes:
- Moves stacked-plot subplot creation into the stacked plotting branch instead of the outer output loop.
- Adds/adjusts validation for stacked plots requiring multiple output variables.
- Changes when
savefig()is called (now gated bynot stack_plots).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # check stack plots will work | ||
| if len(output_names) <= 1: | ||
| raise ValueError( | ||
| "For stack plots need more than 1 output variable" | ||
| ) | ||
| # Create subplots only once for the first output | ||
| if index == 0: | ||
| fig, axs = plt.subplots( | ||
| len(output_names), | ||
| 1, | ||
| figsize=(8.0, (3.5 + (1 * len(output_names)))), | ||
| sharex=True, | ||
| ) | ||
| fig.subplots_adjust(hspace=0.0) |
There was a problem hiding this comment.
plt.subplots(...) for stacked plots is created inside the inner for input_file in input_files loop, guarded only by index == 0. With multiple input files this will recreate the figure for each input and discard previously plotted series. Also, if the first requested output is skipped (missing in m_file.data), index won’t be 0 for the first plotted output and axs will be undefined. Consider creating the stacked fig, axs once before iterating inputs (or using a fig is None / axs is None guard) and basing creation on “first plotted output”, not index == 0.
| axs[index].plot( | ||
| scan_var_array[input_file], | ||
| output_arrays[input_file][output_name], | ||
| "--o", | ||
| color="blue" if output_names2 != [] else None, | ||
| label=labl, | ||
| ) |
There was a problem hiding this comment.
In the stacked-plots branch the plot is added to axs[index], but other formatting in this function uses axs[output_names.index(output_name)]. If output_names ever contains duplicates, these indices can diverge and formatting may apply to a different axis than the plotted data. Using a single indexing approach (preferably the loop index) throughout avoids this inconsistency.
4032908 to
cfa44cf
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
8d6de19 to
43974d6
Compare
This pull request refactors the logic for handling stacked plots in the
mainfunction ofprocess/io/plot_scans.py. The main improvement is to ensure that subplots for stacked plots are created only once, and only when needed, thereby fixing potential issues with subplot creation and improving code clarity.Plotting logic improvements:
fig, axs = plt.subplots(...)) from the outer loop to occur only once, at the first relevant iteration, preventing redundant subplot creation and related errors.passstatement, since subplot creation is now handled inside the plotting logic.ValueErrorif stacked plots are requested with only one output variable, ensuring proper usage and error handling.…iables are handled correctlyDescription
Checklist
I confirm that I have completed the following checks: