# Force two distance matrices to use and show a common legend

I have two distance matrices. Both are comparing two entities in the same way. I create each matrix using

generateDistanceMatrix[centroids_] :=
Module[{matrix},
matrix =
DistanceMatrix[centroids, DistanceFunction -> EuclideanDistance];
Times[matrix, 0.01]];


and then I visualize them using

visualizeDistanceMatrix[matrix_] :=
Module[{range},
range = Range[0, Length[matrix], 20];
ArrayPlot[matrix,
Frame -> True,
ColorFunction -> isoHcf,
ImageSize -> Full,
FrameTicksStyle -> Directive[FontSize -> 12],
FrameTicks -> {{range, None}, {None, range}},
FrameLabel -> {{None, Style["Residue Sequence", 12]},
{Style["Residue Sequence", 12], None}},
PlotLegends ->
{Placed[BarLegend[{isoHcf[#] &, {0, Max[matrix]}},
LabelStyle -> Directive[Black, FontSize -> 12],
LegendLabel -> "Distance (Angstroms)",
LegendMarkerSize -> 400,
LegendLayout -> "Column"], {{1, 1}, {0, 1}}]}]];


I would like to use Grid or something similar to show both distance matrices in the same figure, with a common legend. How can I change this function to force each distance matrix to use a common legend instead of their own?

• Just a side note: Why don't you use just generateDistanceMatrix[centroids_] := 0.01 DistanceMatrix[centroids, DistanceFunction -> EuclideanDistance]? Mar 22, 2020 at 8:21
• That would simplify it, thank you @HenrikSchumacher Mar 22, 2020 at 15:30
• You're welcome. (Btw. DistanceFunction -> EuclideanDistance is the default, so it can also be omitted.) Mar 22, 2020 at 15:32

## 1 Answer

ClearAll[visualizeDM, barLegend]
isoHcf = ColorData["TemperatureMap"];
visualizeDM[minmax_, is_: 80] := ArrayPlot[#,
ColorFunction -> Function[x, isoHcf[Rescale[x, minmax, MinMax@#]]],
Epilog -> MapIndexed[Text[#, #2 - .5] &, Reverse /@ Transpose[#], {2}],
FrameTicksStyle -> Directive[FontSize -> 12],
FrameTicks -> Automatic,
FrameLabel -> {{None, Style["Residue Sequence", 12]},
{Style["Residue Sequence", 12], None}},
ImageSize -> 1 -> is] &

barLegend[minmax_] := BarLegend[{isoHcf[Rescale[#, minmax]] &, minmax}, Automatic,
LabelStyle -> Directive[Black, FontSize -> 12],
LegendLabel -> "Distance (Angstroms)", LegendMarkerSize -> 400,
LegendLayout -> "Column"]


Example:

SeedRandom[1]
centroidlists = RandomReal[100, {2, 5}];
dms = Round[generateDistanceMatrix /@ centroidlists, .01];
dms[[1, 1, 2]] = dms[[1, 2, 1]] = dms[[2, 1, 2]] = dms[[2, 2, 1]] = .55;

minmax = MinMax[dms];

Legended[Row[visualizeDM[minmax] /@ dms, Spacer[5]], Placed[barLegend[minmax], Right]]


Note: I added text labels to check that the a given value is mapped to the same color in both plots. Remove the part Epilog -> ... to get rid of the labels.

• This works great! I wonder though about the legend. My color function yielded a continuous color scale, whereas this rescaled legend gives discrete colors. Is there a way to rescale continuously? (Let me know if any additional code would be helpful ) Mar 22, 2020 at 5:57
• @briennakh, remove Automatic from BarLegend to get a continuous gradient.
– kglr
Mar 22, 2020 at 6:33