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

[FIX] Fix aspect ratio for flat maps #4184

Draft
wants to merge 6 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
2 changes: 2 additions & 0 deletions nilearn/plotting/data/html/surface_plot_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"surface-plot",
"select-view",
surfaceMapInfo["black_bg"],
surfaceMapInfo["aspect_ratio"],
);
layout["title"] = {
text: surfaceMapInfo["title"],
Expand Down Expand Up @@ -61,6 +62,7 @@
"surface-plot",
"select-view",
surfaceMapInfo["black_bg"],
surfaceMapInfo["aspect_ratio"],
);
}
</script>
Expand Down
20 changes: 11 additions & 9 deletions nilearn/plotting/data/js/surface-plot-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ function getCamera(plotDivId, viewSelectId) {
}
}
let cameras = {
"left": {eye: {x: -1.7, y: 0, z: 0},
"left": {eye: {x: -3.0, y: 0, z: 0},
up: {x: 0, y: 0, z: 1},
center: {x: 0, y: 0, z: 0}},
"right": {eye: {x: 1.7, y: 0, z: 0},
"right": {eye: {x: 3.0, y: 0, z: 0},
up: {x: 0, y: 0, z: 1},
center: {x: 0, y: 0, z: 0}},
"top": {eye: {x: 0, y: 0, z: 1.7},
"top": {eye: {x: 0, y: 0, z: 3.0},
up: {x: 0, y: 1, z: 0},
center: {x: 0, y: 0, z: 0}},
"bottom": {eye: {x: 0, y: 0, z: -1.7},
"bottom": {eye: {x: 0, y: 0, z: -3.0},
up: {x: 0, y: 1, z: 0},
center: {x: 0, y: 0, z: 0}},
"front": {eye: {x: 0, y: 1.7, z: 0},
"front": {eye: {x: 0, y: 3.0, z: 0},
up: {x: 0, y: 0, z: 1},
center: {x: 0, y: 0, z: 0}},
"back": {eye: {x: 0, y: -1.7, z: 0},
"back": {eye: {x: 0, y: -3.0, z: 0},
up: {x: 0, y: 0, z: 1},
center: {x: 0, y: 0, z: 0}},
};
Expand All @@ -100,7 +100,7 @@ function getCamera(plotDivId, viewSelectId) {

}

function getLayout(plotDivId, viewSelectId, blackBg) {
function getLayout(plotDivId, viewSelectId, blackBg, aspectRatio) {

let camera = getCamera(plotDivId, viewSelectId);
let axisConfig = getAxisConfig();
Expand All @@ -118,6 +118,8 @@ function getLayout(plotDivId, viewSelectId, blackBg) {
axis_bgcolor: '#333',
scene: {
camera: camera,
aspectmode: "manual",
aspectratio: aspectRatio,
xaxis: axisConfig,
yaxis: axisConfig,
zaxis: axisConfig
Expand All @@ -128,9 +130,9 @@ function getLayout(plotDivId, viewSelectId, blackBg) {

}

function updateLayout(plotDivId, viewSelectId, blackBg) {
function updateLayout(plotDivId, viewSelectId, blackBg, aspectRatio) {
let layout = getLayout(
plotDivId, viewSelectId, blackBg);
plotDivId, viewSelectId, blackBg, aspectRatio);
Plotly.relayout(plotDivId, layout);
}

Expand Down
10 changes: 10 additions & 0 deletions nilearn/plotting/html_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,14 @@ def view_surf(surf_mesh, surf_map=None, bg_map=None, threshold=None,
info['cbar_fontsize'] = colorbar_fontsize
info['title'] = title
info['title_fontsize'] = title_fontsize
info['aspect_ratio'] = _compute_aspect_ratio(surf_mesh)
return _fill_html_template(info, embed_js=True)


def _compute_aspect_ratio(surf_mesh):
surf_mesh = surface.load_surf_mesh(surf_mesh)
data_range = np.ptp(surf_mesh.coordinates, axis=0)
min_range = np.min(data_range[data_range != 0])
data_range /= min_range
data_range[data_range == 0] = 1
return dict(zip(['x', 'y', 'z'], [float(_) for _ in data_range]))
10 changes: 7 additions & 3 deletions nilearn/plotting/surf_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,6 @@ def _plot_surf_matplotlib(coords, faces, surf_map=None, bg_map=None,
antialiased=False,
color='white')

# reduce viewing distance to remove space around mesh
axes.set_box_aspect(None, zoom=1.3)

bg_face_colors = _compute_facecolors_matplotlib(
bg_map, faces, coords.shape[0], darkness, alpha
)
Expand Down Expand Up @@ -633,6 +630,13 @@ def _plot_surf_matplotlib(coords, faces, surf_map=None, bg_map=None,

if title is not None:
axes.set_title(title)

# Use box aspect ratio to simulate equal
# aspect ratio in data space
limits = np.array([getattr(axes, f'get_{axis}lim')()
for axis in 'xyz'])
axes.set_box_aspect(np.ptp(limits, axis=1), zoom=1.3)

# save figure if output file is given
if output_file is not None:
figure.savefig(output_file)
Expand Down