Repository: yt-project/yt

Issue:
`mpl_kwargs={'format' : ext}` is ignored when saving a slice plot
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**
<!--A short 1-2 sentences that succinctly describes the bug-->
`SlicePlot` object ignores file specication by `mpl_kwargs={'format' : ext}` in `save` even though some other options  are effective.

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

```python
import yt
import matplotlib.pyplot as plt

supported_file_formats = plt.gcf().canvas.get_supported_filetypes()
print('Matplotlib supports:')
for k, v in supported_file_formats.items():
    print(k, v)

print('Remove pgf')
del supported_file_formats['pgf']
ds = yt.load('MOOSE_sample_data/out.e')
slc = yt.SlicePlot(ds, 'z', 'diffused', origin='native')
for ext in supported_file_formats.keys():
    print('Saving as ' + ext)
    slc.save('images/mplkwargs_for_{:s}'.format(ext), mpl_kwargs={'format' : ext})
```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

The output files are all in png.
```
mplkwargs_for_eps_Slice_z_diffused.png
mplkwargs_for_jpeg_Slice_z_diffused.png
mplkwargs_for_jpg_Slice_z_diffused.png
mplkwargs_for_pdf_Slice_z_diffused.png
mplkwargs_for_png_Slice_z_diffused.png
mplkwargs_for_ps_Slice_z_diffused.png
mplkwargs_for_raw_Slice_z_diffused.png
mplkwargs_for_rgba_Slice_z_diffused.png
mplkwargs_for_svg_Slice_z_diffused.png
mplkwargs_for_svgz_Slice_z_diffused.png
mplkwargs_for_tif_Slice_z_diffused.png
mplkwargs_for_tiff_Slice_z_diffused.png
```

**Expected outcome**

<!--A description of the expected outcome from the code snippet-->
<!--If this used to work in an earlier version of yt, please note the
version it used to work on-->
Each image should be saved using the given format to `mpl_kwargs={'format' : ext}`

**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System:
  * Python Version:
  * yt version:
  * Other Libraries (if applicable): 

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->




Discussion:
Potentially related to issue #2309
Hi @shoheiogawa! @matthewturk submitted yt-project/yt#2318 to address yt-project/yt#2309. Once that's merged, would you mind checking to see if this issue still persists? 
@munkm once the code is merged, I will check if the issue has been solved.
So this isn't solved even with https://github.com/yt-project/yt/pull/3410
The supported and preferred API in yt is however to use the `suffix` argument in `SlicePlot.save` rather than deferring to matplotlib, and we *do* have flexible support for different file formats offered by matplotlib now. Maybe we could just special case this specific argument and intercept it, but I'm not sure this is worth the additional maintenance cost for us.

Resolving patch:
diff --git a/yt/visualization/plot_container.py b/yt/visualization/plot_container.py
--- a/yt/visualization/plot_container.py
+++ b/yt/visualization/plot_container.py
@@ -3,8 +3,10 @@
 import os
 import sys
 import textwrap
+import warnings
 from collections import defaultdict
 from functools import wraps
+from typing import Any, Dict, Optional
 
 import numpy as np
 from matplotlib.cm import get_cmap
@@ -553,7 +555,12 @@ def set_figure_size(self, size):
         return self
 
     @validate_plot
-    def save(self, name=None, suffix=".png", mpl_kwargs=None):
+    def save(
+        self,
+        name: Optional[str] = None,
+        suffix: str = ".png",
+        mpl_kwargs: Optional[Dict[str, Any]] = None,
+    ):
         """saves the plot to disk.
 
         Parameters
@@ -575,6 +582,14 @@ def save(self, name=None, suffix=".png", mpl_kwargs=None):
         names = []
         if mpl_kwargs is None:
             mpl_kwargs = {}
+        elif "format" in mpl_kwargs:
+            new_suffix = mpl_kwargs.pop("format")
+            if new_suffix != suffix:
+                warnings.warn(
+                    f"Overriding suffix {suffix!r} with mpl_kwargs['format'] = {new_suffix!r}. "
+                    "Use the `suffix` argument directly to suppress this warning."
+                )
+            suffix = new_suffix
 
         if name is None:
             name = str(self.ds)


Test patch: