Skip to content

🐛 Fix stacked plots functionality by ensuring multiple output var…#4068

Merged
timothy-nunn merged 4 commits intomainfrom
fix_stacked_plots_bug
Apr 10, 2026
Merged

🐛 Fix stacked plots functionality by ensuring multiple output var…#4068
timothy-nunn merged 4 commits intomainfrom
fix_stacked_plots_bug

Conversation

@chris-ashe
Copy link
Copy Markdown
Collaborator

@chris-ashe chris-ashe commented Jan 29, 2026

This pull request refactors the logic for handling stacked plots in the main function of process/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:

  • Moved the creation of subplots for stacked plots (fig, axs = plt.subplots(...)) from the outer loop to occur only once, at the first relevant iteration, preventing redundant subplot creation and related errors.
  • Removed the previous subplot creation block from the outer loop and replaced it with a pass statement, since subplot creation is now handled inside the plotting logic.
  • Added a check to raise a ValueError if stacked plots are requested with only one output variable, ensuring proper usage and error handling.…iables are handled correctly

Description

Checklist

I confirm that I have completed the following checks:

  • My changes follow the PROCESS style guide
  • I have justified any large differences in the regression tests caused by this pull request in the comments.
  • I have added new tests where appropriate for the changes I have made.
  • If I have had to change any existing unit or integration tests, I have justified this change in the pull request comments.
  • If I have made documentation changes, I have checked they render correctly.
  • I have added documentation for my change, if appropriate.

@chris-ashe chris-ashe self-assigned this Jan 29, 2026
@chris-ashe chris-ashe requested a review from a team as a code owner January 29, 2026 10:25
@chris-ashe chris-ashe added the Bug Something isnt working label Jan 29, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 0% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.44%. Comparing base (0a46aa3) to head (43974d6).

Files with missing lines Patch % Lines
process/core/io/plot/scans.py 0.00% 10 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@timothy-nunn timothy-nunn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

sharex=True,
)
fig.subplots_adjust(hspace=0.0)
pass
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite this if statement to avoid just having a pass

Copy link
Copy Markdown
Collaborator

@timothy-nunn timothy-nunn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example you provided me still produces 3 files rather than the desired 1.

Copilot AI review requested due to automatic review settings April 10, 2026 10:16
@chris-ashe chris-ashe force-pushed the fix_stacked_plots_bug branch from c980191 to 4032908 Compare April 10, 2026 10:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 by not stack_plots).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +685 to +698
# 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)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +700 to 705
axs[index].plot(
scan_var_array[input_file],
output_arrays[input_file][output_name],
"--o",
color="blue" if output_names2 != [] else None,
label=labl,
)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@chris-ashe chris-ashe force-pushed the fix_stacked_plots_bug branch from 4032908 to cfa44cf Compare April 10, 2026 10:30
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@chris-ashe chris-ashe force-pushed the fix_stacked_plots_bug branch from 8d6de19 to 43974d6 Compare April 10, 2026 11:03
@chris-ashe chris-ashe requested a review from timothy-nunn April 10, 2026 11:04
@timothy-nunn timothy-nunn merged commit 85ded89 into main Apr 10, 2026
10 checks passed
@timothy-nunn timothy-nunn deleted the fix_stacked_plots_bug branch April 10, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isnt working Plotting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants