Skip to content

Commit 53dcbe2

Browse files
authored
Add support to propagate kwargs in Image.plot (#1315)
1 parent c4ea64e commit 53dcbe2

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/torchio/visualization.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ def plot_volume(
3737
output_path=None,
3838
show=True,
3939
xlabels=True,
40-
percentiles=(0.5, 99.5),
40+
percentiles: tuple[float, float] = (0.5, 99.5),
4141
figsize=None,
4242
title=None,
4343
reorient=True,
4444
indices=None,
45+
**imshow_kwargs,
4546
):
4647
_, plt = import_mpl_plt()
4748
fig = None
@@ -58,44 +59,44 @@ def plot_volume(
5859
slice_x = rotate(data[i, :, :], radiological=radiological)
5960
slice_y = rotate(data[:, j, :], radiological=radiological)
6061
slice_z = rotate(data[:, :, k], radiological=radiological)
61-
kwargs = {}
6262
is_label = isinstance(image, LabelMap)
6363
if isinstance(cmap, dict):
6464
slices = slice_x, slice_y, slice_z
6565
slice_x, slice_y, slice_z = color_labels(slices, cmap)
6666
else:
6767
if cmap is None:
6868
cmap = 'cubehelix' if is_label else 'gray'
69-
kwargs['cmap'] = cmap
69+
imshow_kwargs['cmap'] = cmap
70+
7071
if is_label:
71-
kwargs['interpolation'] = 'none'
72+
imshow_kwargs['interpolation'] = 'none'
7273

7374
sr, sa, ss = image.spacing
74-
kwargs['origin'] = 'lower'
75+
imshow_kwargs['origin'] = 'lower'
7576

7677
if percentiles is not None and not is_label:
7778
p1, p2 = np.percentile(data, percentiles)
78-
kwargs['vmin'] = p1
79-
kwargs['vmax'] = p2
79+
imshow_kwargs['vmin'] = p1
80+
imshow_kwargs['vmax'] = p2
8081

8182
sag_aspect = ss / sa
82-
sag_axis.imshow(slice_x, aspect=sag_aspect, **kwargs)
83+
sag_axis.imshow(slice_x, aspect=sag_aspect, **imshow_kwargs)
8384
if xlabels:
8485
sag_axis.set_xlabel('A')
8586
sag_axis.set_ylabel('S')
8687
sag_axis.invert_xaxis()
8788
sag_axis.set_title('Sagittal')
8889

8990
cor_aspect = ss / sr
90-
cor_axis.imshow(slice_y, aspect=cor_aspect, **kwargs)
91+
cor_axis.imshow(slice_y, aspect=cor_aspect, **imshow_kwargs)
9192
if xlabels:
9293
cor_axis.set_xlabel('R')
9394
cor_axis.set_ylabel('S')
9495
cor_axis.invert_xaxis()
9596
cor_axis.set_title('Coronal')
9697

9798
axi_aspect = sa / sr
98-
axi_axis.imshow(slice_z, aspect=axi_aspect, **kwargs)
99+
axi_axis.imshow(slice_z, aspect=axi_aspect, **imshow_kwargs)
99100
if xlabels:
100101
axi_axis.set_xlabel('R')
101102
axi_axis.set_ylabel('A')
@@ -120,7 +121,7 @@ def plot_subject(
120121
output_path=None,
121122
figsize=None,
122123
clear_axes=True,
123-
**kwargs,
124+
**plot_volume_kwargs,
124125
):
125126
_, plt = import_mpl_plt()
126127
num_images = len(subject)
@@ -152,7 +153,7 @@ def plot_subject(
152153
show=False,
153154
cmap=cmap,
154155
xlabels=last_row,
155-
**kwargs,
156+
**plot_volume_kwargs,
156157
)
157158
for axis, axis_name in zip(image_axes, axes_names):
158159
axis.set_title(f'{name} ({axis_name})')

0 commit comments

Comments
 (0)