Annotated RDM plots

Some advanced manipulations on your RDM plots

import matplotlib.pyplot as plt
import numpy
from rsatoolbox.rdm.rdms import RDMs
from rsatoolbox.vis.rdm_plot import show_rdm, Symmetry, MultiRdmPlot

Let’s start with an example RDMs object, with some random dissimilarity values between 0.25 and 0.75

dissim_utv = 0.25+(0.5*numpy.random.rand(5, 36)) ## 9x9 rdm
rdms = RDMs(

Let’s define the clusters to highlight or contour as a boolean vector with as many values as pairs in the RDM

mask = numpy.zeros([36], dtype=bool)
mask[[1, 2]] = True
mask[[11, 12, 13, 18, 19]] = True

Now we make a basic RDM plot

show_rdm(rdms, vmin=0, vmax=1, show_colorbar='figure')


Here we use the mask to specify clusters to highlight

show_rdm(rdms, vmin=0, vmax=1, show_colorbar='figure', overlay=mask)


But we can also visualize the clusters with a contour:

show_rdm(rdms, vmin=0, vmax=1, show_colorbar='figure', contour=mask)

You can customize the color and choose the side(s) of the RDM to plot on:

# Symmetry.BOTH, Symmetry.UPPER, Symmetry.LOWER
show_rdm(rdms, vmin=0, vmax=1, show_colorbar='figure', overlay=mask, overlay_color='blue', overlay_symmetry=Symmetry.LOWER)

Use a CSS-style hexadecimal RGBA color string to specify a color and transparency:

color = '#0000ff40' # Red (00), Green (00), Blue (ff = 16*16 = 256), alpha (opacity) 40%
show_rdm(rdms, vmin=0, vmax=1, show_colorbar='figure', overlay=mask, overlay_color=color)

We can make a new RDMs object that includes the mask

rdms_plus = RDMs(
    dissimilarities=numpy.concatenate([dissim_utv, mask[numpy.newaxis, :]]),
    rdm_descriptors=dict(name=['a', 'b', 'c', 'd', 'e', 'mask'])

Now we can specify the mask via RDM descriptor:

show_rdm(rdms_plus, vmin=0, vmax=1, show_colorbar='figure', contour=('name', 'mask'))

You can also build the plot in Object-Oriented style:

rdm_plot = MultiRdmPlot(rdms)
rdm_plot.vmin = 0
rdm_plot.vmax = 1
rdm_plot.addOverlay(mask, 'blue', Symmetry.UPPER)
rdm_plot.addContour(mask, 'red', Symmetry.LOWER)