Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: X/Y/Z plot script not saving Sub grids
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

I have used X/Y/Z plot script, after ticking both "Include Sub Images" and '"Include Sub Grids", the sub grids are created (I have a sub grid image visible in the UI.

However these Sub Grids and not auto-saved in the regular `./output/txt2img-grids/` folder. Only the grid compounded from all Sub Grids is saved there.

See screenshot (yes, I know I made a 5x7x19 cube), where we can see that all are visible, but neither `./output/txt2img-grids/` no `./output/txt2img-images/` have the sub-grids saved. Only the first compunded grid is saved.

![image](https://user-images.githubusercontent.com/123163912/215270196-62be778a-8aef-406a-ab42-bec16da15505.png)

I have 'Always save all generated image grids" and 'Do not save grids consisting of one picture' ticked in the option.

Most likely related to PR #7146 from @EllangoK that changes X/Y plot to X/Y/Z.


### Steps to reproduce the problem

1. Go to txt2img tab,
2. Select X/Y/Z plot script
3. Select a type and at least 2 values for X, Y, Z
4. Tick "Include Sub Grids"


### What should have happened?

each Sub Grid (one for each Z) should be auto-saved in `./output/txt2img-grids/`

### Commit where the problem happens

9beb794e0b0dc1a0f9e89d8e38bd789a8c608397

### What platforms do you use to access the UI ?

Windows

### What browsers do you use to access the UI ?

Mozilla Firefox

### Command Line Arguments

```Shell
--xformers
```


### List of extensions

No

### Console logs

```Shell
No useful information in console log, but see here:
https://bin.infini.fr/?ed8e59234f9f7f1f#FKdiVPqjf3rLHeMis3npWvUgFRgDzxBQ9W38jiKPLUzt
(expires in 6 day)
```


### Additional information

_No response_


Discussion:

Resolving patch:
diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py
--- a/scripts/xyz_grid.py
+++ b/scripts/xyz_grid.py
@@ -286,23 +286,24 @@ def index(ix, iy, iz):
         print("Unexpected error: draw_xyz_grid failed to return even a single processed image")
         return Processed(p, [])
 
-    grids = [None] * len(zs)
+    sub_grids = [None] * len(zs)
     for i in range(len(zs)):
         start_index = i * len(xs) * len(ys)
         end_index = start_index + len(xs) * len(ys)
         grid = images.image_grid(image_cache[start_index:end_index], rows=len(ys))
         if draw_legend:
             grid = images.draw_grid_annotations(grid, cell_size[0], cell_size[1], hor_texts, ver_texts)
-        
-        grids[i] = grid        
+        sub_grids[i] = grid
         if include_sub_grids and len(zs) > 1:
             processed_result.images.insert(i+1, grid)
 
-    original_grid_size = grids[0].size
-    grids = images.image_grid(grids, rows=1)
-    processed_result.images[0] = images.draw_grid_annotations(grids, original_grid_size[0], original_grid_size[1], title_texts, [[images.GridAnnotation()]])
+    sub_grid_size = sub_grids[0].size
+    z_grid = images.image_grid(sub_grids, rows=1)
+    if draw_legend:
+        z_grid = images.draw_grid_annotations(z_grid, sub_grid_size[0], sub_grid_size[1], title_texts, [[images.GridAnnotation()]])
+    processed_result.images[0] = z_grid
 
-    return processed_result
+    return processed_result, sub_grids
 
 
 class SharedSettingsStackHelper(object):
@@ -576,7 +577,7 @@ def cell(x, y, z):
             return res
 
         with SharedSettingsStackHelper():
-            processed = draw_xyz_grid(
+            processed, sub_grids = draw_xyz_grid(
                 p,
                 xs=xs,
                 ys=ys,
@@ -592,6 +593,10 @@ def cell(x, y, z):
                 second_axes_processed=second_axes_processed
             )
 
+        if opts.grid_save and len(sub_grids) > 1:
+            for sub_grid in sub_grids:
+                images.save_image(sub_grid, p.outpath_grids, "xyz_grid", info=grid_infotext[0], extension=opts.grid_format, prompt=p.prompt, seed=processed.seed, grid=True, p=p)
+
         if opts.grid_save:
             images.save_image(processed.images[0], p.outpath_grids, "xyz_grid", info=grid_infotext[0], extension=opts.grid_format, prompt=p.prompt, seed=processed.seed, grid=True, p=p)
 


Test patch: