# Generate a single legend for multiple plots (e.g. DensityPlot)

Sometimes, it is useful to plot only a single legend for several figures, for example, those were produced by DensityPlot. The following is an example from the document.

Generate a numerical solution to plot:

usol = NDSolveValue[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]


The first plot for $$t\in[0,2]$$ and the 2nd one for $$t\in[2,10]$$

dplt1 = DensityPlot[usol[t, x], {t, 0, 2}, {x, 0, 5}, PlotLegends -> Automatic, PlotRange -> All, ImageSize -> 200]

dplt2 = DensityPlot[usol[t, x], {t, 2, 10}, {x, 0, 5}, PlotLegends -> Automatic, PlotRange -> All, ImageSize -> 200]


They give two plots with different legend ranges.

My questions are two-fold:

1. How to generate a single legend with the same max and min values for both figures. The max and min could be observed by firstly using PlotLegends -> Automatic and then plot a uniform legend with the global max/min values;

2. What is an appropriate method to export such a legend in .eps, to be edited further in graphic software, e.g. Illustrator? I have tried to export the automatically produced bar-legend with Export[".eps"], however, I found its ticks are not short lines but like rectangles, which are hard to edit, e.g. thickness or color.

• Why not just use {t, 0, 10}, {x, 0, 5} to make a single densityplot? Commented Aug 25, 2022 at 2:40

Legend can be generated separately with ArrayPlot using common color function. For better labels you can use MaTeX.

(* set default color for out of range data *)
BACKGROUND = LightBlue ;

(* set color scheme *)
SCHEME = "SunsetColors" ;

(* set data interval *)
{MIN, MAX} = {-1.0, +1.0} ;

(* set color function *)
COLOR = With[
{BACKGROUND=BACKGROUND, SCHEME = ColorData[SCHEME], MIN = MIN, MAX = MAX},
If[MIN <= # <= MAX, SCHEME[Rescale[#, {MIN, MAX}]], BACKGROUND] &
] ;

(* set plot size *)
{HSIZE, VSIZE, LSIZE} = {300, 300, 20} ;

(* generate legend *)
SIZE = 1000 ;
DATA = Reverse[Transpose[{Subdivide[MIN, MAX, SIZE]}]] ;
LEGEND = ArrayPlot[
DATA,
PlotLegends -> False,
Frame -> False,
ColorFunction -> COLOR,
ColorFunctionScaling -> False,
DataRange -> {{0, 1}, {MIN, MAX}}
] ;
LEGEND = Show[
Graphics[
{White, Opacity[0], Apply[Rectangle, Transpose[N[{{0.0, 1.0}, {MIN, MAX}}]]]},
AspectRatio -> HSIZE/LSIZE,
Frame -> True,
FrameTicks -> {{None, All}, {None, None}},
FrameStyle -> Directive[{Black}],
FrameTicksStyle -> Directive[{Black, 12}],
ImageSize -> {Automatic, VSIZE}
],
LEGEND
] ;

(* generate plots *)
ClearAll[foo] ;
foo[x_, y_] := +Cos[2*x*y]^2 ;
ClearAll[bar] ;
bar[x_, y_] := -Sin[2*x*y]^2 ;
plt1 = DensityPlot[foo[x, y], {x, -1, 1}, {y, -1, 1}, PlotLegends -> False, ColorFunction -> COLOR, ColorFunctionScaling -> False, ImagePadding -> PADDIND, PlotRangePadding -> None, ImageSize -> {VSIZE, HSIZE}, FrameTicks -> {{All, None}, {All, None}}, FrameStyle -> Directive[{Black}], FrameTicksStyle -> Directive[{Black, 12}]] ;
plt2 = DensityPlot[bar[x, y], {x, -1, 1}, {y, -1, 1}, PlotLegends -> False, ColorFunction -> COLOR, ColorFunctionScaling -> False, ImagePadding -> PADDIND, PlotRangePadding -> None, ImageSize -> {VSIZE, HSIZE}, FrameTicks -> {{All, None}, {All, None}}, FrameStyle -> Directive[{Black}], FrameTicksStyle -> Directive[{Black, 12}]] ;

(* combine plots *)
result = Grid[
{
{
Labeled[plt1, {Style["x", Bold, 12], Style["y", Bold, 12]}, {Bottom, Left}, RotateLabel -> True, Spacings -> 0.1],
Labeled[plt2, {Style["x", Bold, 12], Style["y", Bold, 12]}, {Bottom, Left}, RotateLabel -> True, Spacings -> 0.1],
Labeled[LEGEND, Style["color", Bold, 12], Left, RotateLabel -> True, Spacings -> 0.1]
}
},
Alignment -> Center, Spacings  -> 0
]


colorBar[arg_] := ColorData["GreenPinkTones"][Rescale[arg, {-2, 2}]]
plots = DensityPlot[#, {x, 0, 2 \[Pi]}, {y, 0, 2 \[Pi]},
ColorFunction -> (colorBar[#] &), ColorFunctionScaling -> False,
ImagePadding -> 80, ImageSize -> 350,
LabelStyle -> {FontFamily -> "LM Roman 12", Black,
FontSize -> 16}, FrameLabel -> {"x", "y"},
PlotPoints -> 60] & /@ {Sin[x y], 2 Sin[x y]};
compinplots = Grid[{# & /@ plots}, Spacings -> {-10, -10}];
barlgns =
BarLegend[{ColorData["GreenPinkTones"], {-2, 2}},
LegendLayout -> "Column", ChartingTickSide -> Right,
LabelStyle -> {FontFamily -> "LM Roman 12", Black, FontSize -> 16},
LegendMarkerSize -> 120, ColorFunctionScaling -> True,
LegendLabel -> "Z"];
final = Legended[compinplots, Placed[barlgns, {0.95, 0.5}]]


if you have more plots you can also this

Legended[Grid[{# & /@ plots, # & /@ plots}, Spacings -> {-10, -11.3}],
Placed[barlgns, {0.95, 0.5}]]
`