Repository: yt-project/yt

Issue:
BUG: modifications through matplotlib engine cannot be properly displayed
<!--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**

The Modifications through Matplotlib engine cannot be properly displayed. 

Taking the following code for example, the expected modifications can only be shown by the containing matplotlib figure object like `fig.savefig("sloshing.png")`. 

**Code for reproduction**

adapted from [docs](https://yt-project.org/docs/dev/cookbook/simple_plots.html#accessing-and-modifying-plots-directly) (also broken there)

```python
import numpy as np

import yt

# Load the dataset.
ds = yt.load("GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150")

# Create a slice object
slc = yt.SlicePlot(ds, "x", ("gas", "density"), width=(800.0, "kpc"))

# Get a reference to the matplotlib axes object for the plot
ax = slc.plots[("gas", "density")].axes

# Let's adjust the x axis tick labels
for label in ax.xaxis.get_ticklabels():
    label.set_color("red")
    label.set_fontsize(16)

# Get a reference to the matplotlib figure object for the plot
fig = slc.plots[("gas", "density")].figure

# And create a mini-panel of a gaussian histogram inside the plot
rect = (0.2, 0.2, 0.2, 0.2)
new_ax = fig.add_axes(rect)

n, bins, patches = new_ax.hist(
    np.random.randn(1000) + 20, 50, facecolor="black", edgecolor="black"
)

# Make sure its visible
new_ax.tick_params(colors="white")

# And label it
la = new_ax.set_xlabel("Dinosaurs per furlong")
la.set_color("white")

slc.save()
```

**Actual outcome**

![iTerm2 1b3woO sloshing_nomag2_hdf5_plt_cnt_0150_Slice_x_density](https://github.com/yt-project/yt/assets/22034707/9bece844-afaa-4327-9660-05a4d009c137)

**Expected outcome**

The changes of the x-axis tick labels
![iTerm2 h1XCIV sloshing](https://github.com/yt-project/yt/assets/22034707/8178509e-88ef-4eeb-9d28-ce3438ffd900)

**Version Information**
  * Operating System: MacOS 14.1.1 and Red Hat Enterprise Linux Server release 7.8 (Maipo)
  * Python Version: 3.9
  * yt version: 4.2.1 and 4.3.0

<!--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:
hi @xshaokun , thanks for noticing !

I think what's happening is that the image (and most of the plot) is lazy-generated at *some point* ™️ between calls to MPL API and `.save()`, which invalidates everything done by the script.
It's trivially fixed by forcing rendering at a controlled point ahead of MPL API calls with `slc.render()`, though I wonder why this wasn't necessary when this example was written.

Resolving patch:
diff --git a/doc/source/cookbook/simple_slice_matplotlib_example.py b/doc/source/cookbook/simple_slice_matplotlib_example.py
--- a/doc/source/cookbook/simple_slice_matplotlib_example.py
+++ b/doc/source/cookbook/simple_slice_matplotlib_example.py
@@ -8,6 +8,10 @@
 # Create a slice object
 slc = yt.SlicePlot(ds, "x", ("gas", "density"), width=(800.0, "kpc"))
 
+# Rendering should be performed explicitly *before* any modification is
+# performed directly with matplotlib.
+slc.render()
+
 # Get a reference to the matplotlib axes object for the plot
 ax = slc.plots[("gas", "density")].axes
 


Test patch: