Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][FIX] allow non-symmetric cbar with plotly #4313

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions nilearn/plotting/js_plotting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def colorscale(cmap, values, threshold=None, symmetric_cmap=True,
warnings.warn('you have specified symmetric_cmap=False '
'but the map contains negative values; '
'setting symmetric_cmap to True',
stacklevel=3)
symmetric_cmap = True
stacklevel=4)
# symmetric_cmap = True
if symmetric_cmap and vmin is not None:
warnings.warn('vmin cannot be chosen when cmap is symmetric',
stacklevel=3)
Expand Down
6 changes: 5 additions & 1 deletion nilearn/plotting/surf_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,10 @@ def plot_surf_stat_map(surf_mesh, stat_map, bg_map=None,
"""
loaded_stat_map = load_surf_data(stat_map)

symmetric_cmap = True
if engine == "plotly" and not symmetric_cbar:
symmetric_cmap = False

# Call get_colorbar_and_data_ranges to derive symmetric vmin, vmax
# And colorbar limits depending on symmetric_cbar settings
cbar_vmin, cbar_vmax, vmin, vmax = get_colorbar_and_data_ranges(
Expand All @@ -1156,7 +1160,7 @@ def plot_surf_stat_map(surf_mesh, stat_map, bg_map=None,
surf_mesh, surf_map=loaded_stat_map,
bg_map=bg_map, hemi=hemi,
view=view, engine=engine, avg_method='mean', threshold=threshold,
cmap=cmap, symmetric_cmap=True, colorbar=colorbar,
cmap=cmap, symmetric_cmap=symmetric_cmap, colorbar=colorbar,
cbar_tick_format=cbar_tick_format, alpha=alpha,
bg_on_data=bg_on_data, darkness=darkness,
vmax=vmax, vmin=vmin,
Expand Down
58 changes: 58 additions & 0 deletions tmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Test plotting asymmetric color bar."""

import matplotlib.pyplot as plt
import numpy as np

from nilearn import datasets, plotting, surface
from nilearn.image import threshold_img

stat_img = datasets.load_sample_motor_activation_image()
stat_img = threshold_img(
stat_img,
threshold=2,
cluster_threshold=0,
two_sided=False,
copy=True,
)

fsaverage = datasets.fetch_surf_fsaverage()

curv_right = surface.load_surf_data(fsaverage.curv_right)
curv_right_sign = np.sign(curv_right)

texture = surface.vol_to_surf(stat_img, fsaverage.pial_right)

engine = "matplotlib"

plotting.plot_surf_stat_map(
fsaverage.infl_right,
texture,
hemi="right",
title="Surface right hemisphere",
colorbar=True,
threshold=1.0,
bg_map=curv_right_sign,
engine=engine,
symmetric_cbar=False,
cmap="black_red",
)
plt.show()

engine = "plotly"

print(f"Using plotting engine {engine}.")

fig = plotting.plot_surf_stat_map(
fsaverage.infl_right,
texture,
hemi="right",
title="Surface right hemisphere",
colorbar=True,
threshold=1.0,
bg_map=curv_right_sign,
bg_on_data=True,
engine=engine,
symmetric_cbar=False,
cmap="black_red",
)
fig.show()