Repository: nilearn/nilearn

Issue:
ticks missing from `view_img` colorbar for some images
```python
from nilearn import plotting, datasets

plotting.view_img(datasets.load_mni152_template(), bg_img=None).open_in_browser()
```

![Screenshot from 2021-06-14 10-17-45](https://user-images.githubusercontent.com/9196501/121907215-d8229480-ccf9-11eb-9ac2-0452bde89220.png)



Discussion:
I don't think the ticks are missing:

```python
plotting.view_img(datasets.load_mni152_template(), black_bg=True, bg_img=None)
```
![ticks](https://user-images.githubusercontent.com/2639645/121918841-b693ce00-cd36-11eb-9210-804de3ff0a02.png)
 
I'll have a closer look to see how we can fix this.
I see, so I guess the issue that when `black_bg` is False, most of the background is still actually black. so depending on the position of the colorbar the tick labels end up being black text on a black background
@jeromedockes This is also my conclusion. I'm still looking for a way to fix this. I think moving up the colorbar makes the most sense.
maybe @pbellec has some advice?
Not sure if it's related, but I noticed that in some instances nilearn volumes come with a mask, which leads to a white background in `plot_stat_map` for example, but is ignored by `view_img`. If the mask was applied, then `black_bg=False` would be the right guess, and the black writing would be visible. 

Resolving patch:
diff --git a/nilearn/plotting/html_stat_map.py b/nilearn/plotting/html_stat_map.py
--- a/nilearn/plotting/html_stat_map.py
+++ b/nilearn/plotting/html_stat_map.py
@@ -3,11 +3,13 @@
 """
 import os
 import json
+import copy
 import warnings
 from io import BytesIO
 from base64 import b64encode
 
 import numpy as np
+import matplotlib
 from matplotlib.image import imsave
 
 from nibabel.affines import apply_affine
@@ -23,6 +25,7 @@
 from .._utils.extmath import fast_abs_percentile
 from .._utils.niimg import _safe_get_data
 from ..datasets import load_mni152_template
+from ..masking import compute_brain_mask
 
 
 def _data_to_sprite(data):
@@ -307,8 +310,22 @@ def _json_view_size(params):
     return width_view, height_view
 
 
-def _json_view_data(bg_img, stat_map_img, mask_img, bg_min, bg_max, colors,
-                    cmap, colorbar):
+def _get_bg_mask_and_cmap(bg_img, black_bg):
+    """Helper function of _json_view_data."""
+    bg_mask = _safe_get_data(compute_brain_mask(bg_img),
+                             ensure_finite=True)
+    bg_mask = np.logical_not(bg_mask).astype(float)
+    bg_mask[bg_mask == 1] = np.nan
+    bg_cmap = copy.copy(matplotlib.cm.get_cmap('gray'))
+    if black_bg:
+        bg_cmap.set_bad('black')
+    else:
+        bg_cmap.set_bad('white')
+    return bg_mask, bg_cmap
+
+
+def _json_view_data(bg_img, stat_map_img, mask_img, bg_min, bg_max, black_bg,
+                    colors, cmap, colorbar):
     """Create a json-like viewer object, and populate with base64 data.
     Returns: json_view
 
@@ -319,8 +336,9 @@ def _json_view_data(bg_img, stat_map_img, mask_img, bg_min, bg_max, colors,
 
     # Create a base64 sprite for the background
     bg_sprite = BytesIO()
-    bg_data = _safe_get_data(bg_img, ensure_finite=True)
-    _save_sprite(bg_data, bg_sprite, bg_max, bg_min, None, 'gray', 'png')
+    bg_data = _safe_get_data(bg_img, ensure_finite=True).astype(float)
+    bg_mask, bg_cmap = _get_bg_mask_and_cmap(bg_img, black_bg)
+    _save_sprite(bg_data, bg_sprite, bg_max, bg_min, bg_mask, bg_cmap, 'png')
     json_view['bg_base64'] = _bytesIO_to_base64(bg_sprite)
 
     # Create a base64 sprite for the stat map
@@ -542,7 +560,7 @@ def view_img(stat_map_img, bg_img='MNI152',
 
     # Now create a json-like object for the viewer, and converts in html
     json_view = _json_view_data(bg_img, stat_map_img, mask_img, bg_min, bg_max,
-                                colors, cmap, colorbar)
+                                black_bg, colors, cmap, colorbar)
 
     json_view['params'] = _json_view_params(
         stat_map_img.shape, stat_map_img.affine, colors['vmin'],


Test patch:
diff --git a/nilearn/plotting/tests/test_html_stat_map.py b/nilearn/plotting/tests/test_html_stat_map.py
--- a/nilearn/plotting/tests/test_html_stat_map.py
+++ b/nilearn/plotting/tests/test_html_stat_map.py
@@ -244,8 +244,7 @@ def test_json_view_size():
     assert height == height_exp, "html viewer does not have expected height"
 
 
-def test_json_view_data():
-
+def _get_data_and_json_view(black_bg, cbar):
     # simple simulated data for stat_img and background
     bg_img, data = _simulate_img()
     stat_map_img, data = _simulate_img()
@@ -259,21 +258,25 @@ def test_json_view_data():
 
     # Build a sprite
     json_view = html_stat_map._json_view_data(
-        bg_img, stat_map_img, mask_img, bg_min=0, bg_max=1, colors=colors,
-        cmap='cold_hot', colorbar=True)
+        bg_img, stat_map_img, mask_img, bg_min=0, bg_max=1, black_bg=black_bg,
+        colors=colors, cmap='cold_hot', colorbar=cbar)
+    return data, json_view
+
 
+@pytest.mark.parametrize("black_bg", [True, False])
+@pytest.mark.parametrize("cbar", [True, False])
+def test_json_view_data(black_bg, cbar):
+    _, json_view = _get_data_and_json_view(black_bg, cbar)
     # Check the presence of critical fields
     assert isinstance(json_view['bg_base64'], str)
     assert isinstance(json_view['stat_map_base64'], str)
     assert isinstance(json_view['cm_base64'], str)
 
-    return json_view, data
-
-
-def test_json_view_to_html():
 
-    # Re use the data simulated in another test
-    json_view, data = test_json_view_data()
+@pytest.mark.parametrize("black_bg", [True, False])
+@pytest.mark.parametrize("cbar", [True, False])
+def test_json_view_to_html(black_bg, cbar):
+    data, json_view = _get_data_and_json_view(black_bg, cbar)
     json_view['params'] = html_stat_map._json_view_params(
         data.shape, np.eye(4), vmin=0, vmax=1, cut_slices=[1, 1, 1],
         black_bg=True, opacity=1, draw_cross=True, annotate=False,