Repository: nilearn/nilearn

Issue:
`plot_matrix` colour bar overlaps with the matrix when plotting on assigned subplot axes
<!--Provide a brief description of the bug.-->
The `plot_matrix` will overlap the colour bar on the main figure when plotting on subplots in a horizontal layout. This cannot be fixed by adjusting spacing with `subplots_adjust`.

<!--Please fill in the following information, to the best of your ability.-->
Nilearn version: 0.9.1

### Expected behavior

Colour bar stay in each subplot.

### Actual behavior

Colour bar at weird places.

### Steps and code to reproduce bug

```python
from nilearn import datasets
from nilearn.maskers import NiftiMapsMasker
from nilearn.connectome import ConnectivityMeasure
from nilearn.plotting import plot_matrix

import numpy as np

import matplotlib.pyplot as plt


atlas = datasets.fetch_atlas_msdl()
atlas_filename = atlas['maps']
labels = atlas['labels']

data = datasets.fetch_development_fmri(n_subjects=1)
fmri_filenames = data.func[0]

masker = NiftiMapsMasker(maps_img=atlas_filename, standardize=True, detrend=True)
correlation_measure = ConnectivityMeasure(kind='correlation')

time_series = masker.fit_transform(fmri_filenames)
correlation_matrix = correlation_measure.fit_transform([time_series])[0]
np.fill_diagonal(correlation_matrix, 0)

# plot on subplot, with colorbar
fig = plt.figure()
axes = fig.subplots(1, 2)
for corr, ax in zip([correlation_matrix, correlation_matrix], axes):
    plot_matrix(corr, labels=labels, colorbar=True, axes=ax, vmax=0.7, vmin=-0.7)
```
Output:
![image](https://user-images.githubusercontent.com/13743617/194398382-2905a23e-126d-4ce8-af39-0761291684ec.png)



Discussion:
Indeed, thx for raising.
Can you confirm if the MPL version generating these plots is 3.6.0, @htwangtw, or another version ?
I observed the same problem when using `plotting.plot_surf()` with subplots (MPL 3.6.0).
In this particular case, I used a `GridSpec` but it doesn't seem to change anything.

![sub-04_sub-05_features_correlation](https://user-images.githubusercontent.com/13835654/194515278-62fa2ea0-3c5c-4fbe-a072-d8d0f39ac19e.jpg)

> Can you confirm if the MPL version generating these plots is 3.6.0, @htwangtw, or another version ?

I was testing under 3.4.2
here is a minimal example for anyone wanting to reproduce the plotting issue without computing the brain connectivity stuff

```python
from matplotlib import pyplot as plt
import numpy as np

from nilearn import plotting

matrix = np.zeros((10, 10))
fig, axes = plt.subplots(1, 2)
for ax in axes:
    plotting.plot_matrix(matrix, axes=ax)

plt.show()
```

![nilearn-plot](https://user-images.githubusercontent.com/9196501/194590574-6e22161e-227a-4bd8-abb2-5c1ee7d5e6be.png)

related:
- https://github.com/nilearn/nilearn/issues/545

- also about giving more access to nilearn display objects (for all but the simplest layouts I find it easier to create all the axes, including the colorbars, myself with matplotlib and just use nilearn to plot one brain in one matplotlib Axes at a time):
  - https://github.com/nilearn/nilearn/pull/3073
  - https://github.com/nilearn/nilearn/pull/2859

I started looking into it, fixing the weird behaviour, but I'm still investigating on the colorbar labels overflowing on the next subplot.

![image](https://user-images.githubusercontent.com/12402673/198005776-b1c0063f-86f4-42ec-aab0-70e6e890cc19.png)


@alexisthual I don't think I touched code related to surface plot. Could you provide a minimal example so I can see how it relates?

Resolving patch:
diff --git a/nilearn/plotting/matrix_plotting.py b/nilearn/plotting/matrix_plotting.py
--- a/nilearn/plotting/matrix_plotting.py
+++ b/nilearn/plotting/matrix_plotting.py
@@ -6,7 +6,7 @@
 import pandas as pd
 import matplotlib.pyplot as plt
 import matplotlib.patches as mpatches
-from matplotlib.colorbar import make_axes
+from mpl_toolkits.axes_grid1 import make_axes_locatable
 from .._utils import fill_doc
 with warnings.catch_warnings():
     warnings.simplefilter("ignore", FutureWarning)
@@ -286,13 +286,11 @@ def plot_matrix(mat, title=None, labels=None, figure=None, axes=None,
                              rect=((0, 0, .95, 1) if colorbar
                                    else (0, 0, 1, 1)))
     if colorbar:
-        cax, kw = make_axes(axes, location='right', fraction=0.05, shrink=0.8,
-                            pad=.0)
-        fig.colorbar(mappable=display, cax=cax)
-        # make some room
-        fig.subplots_adjust(right=0.78)
-        # change current axis back to matrix
-        plt.sca(axes)
+        divider = make_axes_locatable(axes)
+        cax = divider.append_axes("right", size="5%", pad=.0)
+
+        plt.colorbar(display, cax=cax)
+        fig.tight_layout()
 
     if title is not None:
         # Adjust the size


Test patch: