We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
nilearn.plotting overrides the matplotlib backend, causing troubles on remote SSH development
I am working on a remote settings, accessing a machine (drago/margaret for INRIA context) through a PyCharm Remote Interpreter.
I have been struggling to display nilearn figures in the scientific view of PyCharm, leading to a painful process of saving the image => rsync figures dir => visualize figures once there are on personal laptop.

I did a simple experiment drawing first a simple `plot(list(range(10)))` which is displayed and then a nilearn figure which does not show up (script at the end of the issue).

I found out the reason for this, which is that in `nilearn.plotting.__init__`, the matplotlib backend is set to `agg` if not in `[inline, agg]`, but the backend used initially (which is working for me) is `module://backend_interagg`.

Setting the backend to the initial value after importing `nilearn.datasets` fixes the problem.

I don't know exactly how those matplotlib backends work, if the list of accepted backends in `nilearn.datasets.__init__` could be extended or if we could have a more robust list of valid matplotlib backend. Also, I feel that nilearn shouldn't override matplotlib backend silently? 

<!--Please fill in the following information, to the best of your ability.-->
Nilearn version: 0.7.1

### Expected behavior

Nilearn does not override matplotlib backend silently.

### Actual behavior

Nilearn sets backend to "agg" if not in ["inline", "agg"], causing trouble with remote SSH development.

### Steps and code to reproduce bug

Run the following script through SSH remote interpreter

```python
import matplotlib
initial_backend = matplotlib.get_backend().lower()
print(initial_backend)

import matplotlib.pyplot as plt
import numpy as np

arr = np.zeros((100, 100))
plt.figure()
plt.plot(list(range(10)))
plt.show()
# >> The show is displayed in PyCharm

from nilearn import datasets, plotting
print(matplotlib.get_backend().lower())
# >> Backend has switched to "agg"

sample_brain_map = datasets.fetch_neurovault_motor_task().images[0]
plotting.plot_stat_map(
    sample_brain_map,
    threshold=3,
    title="Before setting back the backend",
)

plt.show()
# >> Does not show up

matplotlib.use(initial_backend)
plotting.plot_stat_map(
    sample_brain_map,
    threshold=3,
    title="After setting back the backend",
)

plt.show()
# >> Shows up

```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `nilearn/plotting/__init__.py`
Content:
```
1 """
2 Plotting code for nilearn
3 """
4 # Original Authors: Chris Filo Gorgolewski, Gael Varoquaux
5 import os
6 import sys
7 import importlib
8 
9 
10 ###############################################################################
11 # Make sure that we don't get DISPLAY problems when running without X on
12 # unices
13 def _set_mpl_backend():
14     # We are doing local imports here to avoid polluting our namespace
15     try:
16         import matplotlib
17     except ImportError:
18         if importlib.util.find_spec("pytest") is not None:
19             from .._utils.testing import skip_if_running_tests
20             # No need to fail when running tests
21             skip_if_running_tests('matplotlib not installed')
22         raise
23     else:
24         from ..version import (_import_module_with_version_check,
25                                OPTIONAL_MATPLOTLIB_MIN_VERSION)
26         # When matplotlib was successfully imported we need to check
27         # that the version is greater that the minimum required one
28         _import_module_with_version_check('matplotlib',
29                                           OPTIONAL_MATPLOTLIB_MIN_VERSION)
30         current_backend = matplotlib.get_backend().lower()
31 
32         if 'inline' in current_backend or 'nbagg' in current_backend:
33             return
34         # Set the backend to a non-interactive one for unices without X
35         # (see gh-2560)
36         if (sys.platform not in ('darwin', 'win32') and
37                 'DISPLAY' not in os.environ):
38             matplotlib.use('Agg')
39 
40 
41 _set_mpl_backend()
42 
43 ###############################################################################
44 from . import cm
45 from .img_plotting import (
46     plot_img, plot_anat, plot_epi, plot_roi, plot_stat_map,
47     plot_glass_brain, plot_connectome, plot_connectome_strength,
48     plot_markers, plot_prob_atlas, plot_carpet, plot_img_comparison, show)
49 from .find_cuts import find_xyz_cut_coords, find_cut_slices, \
50     find_parcellation_cut_coords, find_probabilistic_atlas_cut_coords
51 from .matrix_plotting import (plot_matrix, plot_contrast_matrix,
52                               plot_design_matrix, plot_event)
53 from .html_surface import view_surf, view_img_on_surf
54 from .html_stat_map import view_img
55 from .html_connectome import view_connectome, view_markers
56 from .surf_plotting import (plot_surf, plot_surf_stat_map, plot_surf_roi,
57                             plot_img_on_surf, plot_surf_contours)
58 
59 __all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',
60            'plot_roi', 'plot_stat_map', 'plot_glass_brain',
61            'plot_markers', 'plot_connectome', 'plot_prob_atlas',
62            'find_xyz_cut_coords', 'find_cut_slices',
63            'plot_img_comparison',
64            'show', 'plot_matrix',
65            'plot_design_matrix', 'plot_contrast_matrix', 'plot_event',
66            'view_surf', 'view_img_on_surf',
67            'view_img', 'view_connectome', 'view_markers',
68            'find_parcellation_cut_coords',
69            'find_probabilistic_atlas_cut_coords',
70            'plot_surf', 'plot_surf_stat_map', 'plot_surf_roi',
71            'plot_img_on_surf', 'plot_connectome_strength', 'plot_carpet',
72            'plot_surf_contours']
73
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/nilearn/plotting/__init__.py b/nilearn/plotting/__init__.py
--- a/nilearn/plotting/__init__.py
+++ b/nilearn/plotting/__init__.py
@@ -2,9 +2,8 @@
 Plotting code for nilearn
 """
 # Original Authors: Chris Filo Gorgolewski, Gael Varoquaux
-import os
-import sys
 import importlib
+import warnings
 
 
 ###############################################################################
@@ -29,13 +28,17 @@
                                           OPTIONAL_MATPLOTLIB_MIN_VERSION)
         current_backend = matplotlib.get_backend().lower()
 
-        if 'inline' in current_backend or 'nbagg' in current_backend:
-            return
-        # Set the backend to a non-interactive one for unices without X
-        # (see gh-2560)
-        if (sys.platform not in ('darwin', 'win32') and
-                'DISPLAY' not in os.environ):
-            matplotlib.use('Agg')
+        try:
+            # Making sure the current backend is usable by matplotlib
+            matplotlib.use(current_backend)
+        except Exception:
+            # If not, switching to default agg backend
+            matplotlib.use("Agg")
+        new_backend = matplotlib.get_backend().lower()
+
+        if new_backend != current_backend:
+            # Matplotlib backend has been changed, let's warn the user
+            warnings.warn(f"Backend changed to {new_backend}...")
 
 
 _set_mpl_backend()

{"golden_diff": "diff --git a/nilearn/plotting/__init__.py b/nilearn/plotting/__init__.py\n--- a/nilearn/plotting/__init__.py\n+++ b/nilearn/plotting/__init__.py\n@@ -2,9 +2,8 @@\n Plotting code for nilearn\n \"\"\"\n # Original Authors: Chris Filo Gorgolewski, Gael Varoquaux\n-import os\n-import sys\n import importlib\n+import warnings\n \n \n ###############################################################################\n@@ -29,13 +28,17 @@\n                                           OPTIONAL_MATPLOTLIB_MIN_VERSION)\n         current_backend = matplotlib.get_backend().lower()\n \n-        if 'inline' in current_backend or 'nbagg' in current_backend:\n-            return\n-        # Set the backend to a non-interactive one for unices without X\n-        # (see gh-2560)\n-        if (sys.platform not in ('darwin', 'win32') and\n-                'DISPLAY' not in os.environ):\n-            matplotlib.use('Agg')\n+        try:\n+            # Making sure the current backend is usable by matplotlib\n+            matplotlib.use(current_backend)\n+        except Exception:\n+            # If not, switching to default agg backend\n+            matplotlib.use(\"Agg\")\n+        new_backend = matplotlib.get_backend().lower()\n+\n+        if new_backend != current_backend:\n+            # Matplotlib backend has been changed, let's warn the user\n+            warnings.warn(f\"Backend changed to {new_backend}...\")\n \n \n _set_mpl_backend()\n", "issue": "nilearn.plotting overrides the matplotlib backend, causing troubles on remote SSH development\nI am working on a remote settings, accessing a machine (drago/margaret for INRIA context) through a PyCharm Remote Interpreter.\r\nI have been struggling to display nilearn figures in the scientific view of PyCharm, leading to a painful process of saving the image => rsync figures dir => visualize figures once there are on personal laptop.\r\n\r\nI did a simple experiment drawing first a simple `plot(list(range(10)))` which is displayed and then a nilearn figure which does not show up (script at the end of the issue).\r\n\r\nI found out the reason for this, which is that in `nilearn.plotting.__init__`, the matplotlib backend is set to `agg` if not in `[inline, agg]`, but the backend used initially (which is working for me) is `module://backend_interagg`.\r\n\r\nSetting the backend to the initial value after importing `nilearn.datasets` fixes the problem.\r\n\r\nI don't know exactly how those matplotlib backends work, if the list of accepted backends in `nilearn.datasets.__init__` could be extended or if we could have a more robust list of valid matplotlib backend. Also, I feel that nilearn shouldn't override matplotlib backend silently? \r\n\r\n<!--Please fill in the following information, to the best of your ability.-->\r\nNilearn version: 0.7.1\r\n\r\n### Expected behavior\r\n\r\nNilearn does not override matplotlib backend silently.\r\n\r\n### Actual behavior\r\n\r\nNilearn sets backend to \"agg\" if not in [\"inline\", \"agg\"], causing trouble with remote SSH development.\r\n\r\n### Steps and code to reproduce bug\r\n\r\nRun the following script through SSH remote interpreter\r\n\r\n```python\r\nimport matplotlib\r\ninitial_backend = matplotlib.get_backend().lower()\r\nprint(initial_backend)\r\n\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\narr = np.zeros((100, 100))\r\nplt.figure()\r\nplt.plot(list(range(10)))\r\nplt.show()\r\n# >> The show is displayed in PyCharm\r\n\r\nfrom nilearn import datasets, plotting\r\nprint(matplotlib.get_backend().lower())\r\n# >> Backend has switched to \"agg\"\r\n\r\nsample_brain_map = datasets.fetch_neurovault_motor_task().images[0]\r\nplotting.plot_stat_map(\r\n    sample_brain_map,\r\n    threshold=3,\r\n    title=\"Before setting back the backend\",\r\n)\r\n\r\nplt.show()\r\n# >> Does not show up\r\n\r\nmatplotlib.use(initial_backend)\r\nplotting.plot_stat_map(\r\n    sample_brain_map,\r\n    threshold=3,\r\n    title=\"After setting back the backend\",\r\n)\r\n\r\nplt.show()\r\n# >> Shows up\r\n\r\n```\r\n\n", "before_files": [{"content": "\"\"\"\nPlotting code for nilearn\n\"\"\"\n# Original Authors: Chris Filo Gorgolewski, Gael Varoquaux\nimport os\nimport sys\nimport importlib\n\n\n###############################################################################\n# Make sure that we don't get DISPLAY problems when running without X on\n# unices\ndef _set_mpl_backend():\n    # We are doing local imports here to avoid polluting our namespace\n    try:\n        import matplotlib\n    except ImportError:\n        if importlib.util.find_spec(\"pytest\") is not None:\n            from .._utils.testing import skip_if_running_tests\n            # No need to fail when running tests\n            skip_if_running_tests('matplotlib not installed')\n        raise\n    else:\n        from ..version import (_import_module_with_version_check,\n                               OPTIONAL_MATPLOTLIB_MIN_VERSION)\n        # When matplotlib was successfully imported we need to check\n        # that the version is greater that the minimum required one\n        _import_module_with_version_check('matplotlib',\n                                          OPTIONAL_MATPLOTLIB_MIN_VERSION)\n        current_backend = matplotlib.get_backend().lower()\n\n        if 'inline' in current_backend or 'nbagg' in current_backend:\n            return\n        # Set the backend to a non-interactive one for unices without X\n        # (see gh-2560)\n        if (sys.platform not in ('darwin', 'win32') and\n                'DISPLAY' not in os.environ):\n            matplotlib.use('Agg')\n\n\n_set_mpl_backend()\n\n###############################################################################\nfrom . import cm\nfrom .img_plotting import (\n    plot_img, plot_anat, plot_epi, plot_roi, plot_stat_map,\n    plot_glass_brain, plot_connectome, plot_connectome_strength,\n    plot_markers, plot_prob_atlas, plot_carpet, plot_img_comparison, show)\nfrom .find_cuts import find_xyz_cut_coords, find_cut_slices, \\\n    find_parcellation_cut_coords, find_probabilistic_atlas_cut_coords\nfrom .matrix_plotting import (plot_matrix, plot_contrast_matrix,\n                              plot_design_matrix, plot_event)\nfrom .html_surface import view_surf, view_img_on_surf\nfrom .html_stat_map import view_img\nfrom .html_connectome import view_connectome, view_markers\nfrom .surf_plotting import (plot_surf, plot_surf_stat_map, plot_surf_roi,\n                            plot_img_on_surf, plot_surf_contours)\n\n__all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',\n           'plot_roi', 'plot_stat_map', 'plot_glass_brain',\n           'plot_markers', 'plot_connectome', 'plot_prob_atlas',\n           'find_xyz_cut_coords', 'find_cut_slices',\n           'plot_img_comparison',\n           'show', 'plot_matrix',\n           'plot_design_matrix', 'plot_contrast_matrix', 'plot_event',\n           'view_surf', 'view_img_on_surf',\n           'view_img', 'view_connectome', 'view_markers',\n           'find_parcellation_cut_coords',\n           'find_probabilistic_atlas_cut_coords',\n           'plot_surf', 'plot_surf_stat_map', 'plot_surf_roi',\n           'plot_img_on_surf', 'plot_connectome_strength', 'plot_carpet',\n           'plot_surf_contours']\n", "path": "nilearn/plotting/__init__.py"}], "after_files": [{"content": "\"\"\"\nPlotting code for nilearn\n\"\"\"\n# Original Authors: Chris Filo Gorgolewski, Gael Varoquaux\nimport importlib\nimport warnings\n\n\n###############################################################################\n# Make sure that we don't get DISPLAY problems when running without X on\n# unices\ndef _set_mpl_backend():\n    # We are doing local imports here to avoid polluting our namespace\n    try:\n        import matplotlib\n    except ImportError:\n        if importlib.util.find_spec(\"pytest\") is not None:\n            from .._utils.testing import skip_if_running_tests\n            # No need to fail when running tests\n            skip_if_running_tests('matplotlib not installed')\n        raise\n    else:\n        from ..version import (_import_module_with_version_check,\n                               OPTIONAL_MATPLOTLIB_MIN_VERSION)\n        # When matplotlib was successfully imported we need to check\n        # that the version is greater that the minimum required one\n        _import_module_with_version_check('matplotlib',\n                                          OPTIONAL_MATPLOTLIB_MIN_VERSION)\n        current_backend = matplotlib.get_backend().lower()\n\n        try:\n            # Making sure the current backend is usable by matplotlib\n            matplotlib.use(current_backend)\n        except Exception:\n            # If not, switching to default agg backend\n            matplotlib.use(\"Agg\")\n        new_backend = matplotlib.get_backend().lower()\n\n        if new_backend != current_backend:\n            # Matplotlib backend has been changed, let's warn the user\n            warnings.warn(f\"Backend changed to {new_backend}...\")\n\n\n_set_mpl_backend()\n\n###############################################################################\nfrom . import cm\nfrom .img_plotting import (\n    plot_img, plot_anat, plot_epi, plot_roi, plot_stat_map,\n    plot_glass_brain, plot_connectome, plot_connectome_strength,\n    plot_markers, plot_prob_atlas, plot_carpet, plot_img_comparison, show)\nfrom .find_cuts import find_xyz_cut_coords, find_cut_slices, \\\n    find_parcellation_cut_coords, find_probabilistic_atlas_cut_coords\nfrom .matrix_plotting import (plot_matrix, plot_contrast_matrix,\n                              plot_design_matrix, plot_event)\nfrom .html_surface import view_surf, view_img_on_surf\nfrom .html_stat_map import view_img\nfrom .html_connectome import view_connectome, view_markers\nfrom .surf_plotting import (plot_surf, plot_surf_stat_map, plot_surf_roi,\n                            plot_img_on_surf, plot_surf_contours)\n\n__all__ = ['cm', 'plot_img', 'plot_anat', 'plot_epi',\n           'plot_roi', 'plot_stat_map', 'plot_glass_brain',\n           'plot_markers', 'plot_connectome', 'plot_prob_atlas',\n           'find_xyz_cut_coords', 'find_cut_slices',\n           'plot_img_comparison',\n           'show', 'plot_matrix',\n           'plot_design_matrix', 'plot_contrast_matrix', 'plot_event',\n           'view_surf', 'view_img_on_surf',\n           'view_img', 'view_connectome', 'view_markers',\n           'find_parcellation_cut_coords',\n           'find_probabilistic_atlas_cut_coords',\n           'plot_surf', 'plot_surf_stat_map', 'plot_surf_roi',\n           'plot_img_on_surf', 'plot_connectome_strength', 'plot_carpet',\n           'plot_surf_contours']\n", "path": "nilearn/plotting/__init__.py"}]}