# How to add a common color legend to a grid of density plots?

I have a grid of density plots, say something like:

GraphicsGrid[Partition[Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}],
{i, 1, 4}], 2]]


I want to show a color legend bar next to this plot. I know that the option PlotLegends -> Automatic does this for a single density plot, so if I do

GraphicsGrid[Partition[Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}, PlotLegends -> Automatic],
{i, 1, 4}], 2]]


I get the same color legend bar repeated four times, one for each density plot. But naturally what I want is to show a single bar common to all the density plots in the GraphicsGrid. How do I do this?

As Cormullion pointed out, you will have to create the legend by hand as it would be difficult to have a legend created automatically by GraphicsGrid. However, the simplest method of attaching a legend is to use Legended directly. The structure of Legended when used like this is

Legended[ Graph | Graphics | Graphics3D, legend ]


Then, in your case, you use

Legended[
GraphicsGrid[Partition[Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3},
ColorFunction -> "LakeColors"],
{i, 1, 4}], 2]
],
BarLegend[{"LakeColors", {-1, 1}}, LegendLayout -> "Column"]
] Or, if you would like to place it below

Legended[
GraphicsGrid[Partition[Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3},
ColorFunction -> "LakeColors"],
{i, 1, 4}], 2]
],
Placed[
BarLegend[{"LakeColors", {-1, 1}}, LegendLayout -> "Row"],
Below
]
] When generated automatically using PlotLegends, Placed is inserted automatically, so you can use it to position the legend here.

• What I'm wishing for now is setting the size of the legend bar. You could say, just resize the graphics grid to make it smaller, then the legend will be larger in proportion to it. But the fonts will also be larger in proportion to it, so I don't want to do that. I simply want to make the legend longer, and maybe wider. Can I do that without having to create my own colour bar from scratch? Feb 22, 2013 at 21:13
• @Szabolcs you're looking for LegendMarkerSize, picking an appropriate size will require some fiddling, though. Feb 22, 2013 at 21:20
• @rcollyer Hi, rcollyer. You example is not right in MMA 10.4. The color of densityplot is different to that in legend Apr 27, 2016 at 6:33
• @matheorem right. You have to include ColorFunction -> "LakeColors" for it to work. Plus the second example had the wrong LegendLayout. Both fixed. If you want the default color scheme use "M10DefaultDensityGradient", instead. Apr 27, 2016 at 6:46
• @rcollyer I still think your method maybe flawed. If you try 10*i*Sin[i x] Sin[y/i], the color is still the same. But it should be different now for these 4 plots Apr 27, 2016 at 7:15

Best I could do. GraphicsGrid can't read my mind at all...

legend = BarLegend[{"LakeColors", {-1, 1}}, LegendLayout -> "Row"];

plots = Table[
DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}], {i, 1,
4}];

Column[{
GraphicsGrid[{
{plots[], plots[]},
{plots[], plots[]}}, ImageSize -> 300],
legend}, Center] • I don't have Mathematica 9 to test it, but shouldn't this also work? GraphicsGrid[{{plots[], plots[]}, {plots[], plots[]}, {legend, SpanFromLeft}}, Alignment -> Center, ImageSize -> 300]
– VLC
Feb 22, 2013 at 17:12
• @VLC Yes, almost, but the last row of GraphicsGrid is full height, rather than shorter... Feb 22, 2013 at 17:24
• You could use Grid instead of GraphicsGrid to allow for different heights. Feb 22, 2013 at 22:37
• @Verbeia That works too, and doesn't need ImageSize. Thanks! Feb 23, 2013 at 17:00

I don't have 9 on this computer but how about:

Labeled[
grid = GraphicsGrid[
Partition[
Table[DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}], {i,
1, 4}], 2]],

DensityPlot[y, {y, -1, 1}, {x, 0, 1}, AspectRatio -> 0.07,
ColorFunction -> "LakeColors",
FrameTicks -> {{None, None}, {Automatic, None}}, FrameStyle -> GrayLevel[0.6],
FrameTicksStyle -> GrayLevel[0.],
ImageSize -> First@AbsoluteCurrentValue[grid, ImageSize],
Mesh -> False, PlotRangePadding -> 0]
] To get outside ticks on the legend you could either enter a tick list manually, or make a tick function, or do a replace(all) on the DensityPlot.

 ticks = Join[
Table[{i, "", {0, 0.01}, GrayLevel[0.6]}, {i, -1, 1, 0.1}], {{-1.,
"-1.0", {0, 0.015}}, {-0.5, "-0.5", {0, 0.015}}, {0,
"0", {0, 0.015}}, {0.5, "0.5", {0, 0.015}}, {1.,
"1.0", {0, 0.015}}}]; These ticks, when compared with the built in label, look a little high. So we can nudge them lower. Highlight the value and press "Control" + "Down arrow" or go to the menu Insert > Typesetting > Nudge Down. This wraps adjustment boxes around the tick label. After nudging the ticks down you get this: << PlotLegends
ShowLegend[
GraphicsGrid[
Partition[
Table[DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}], {i, 1, 4}], 2]],
{ColorData["LakeColors"][1 - #1] &, 10, " 1", "-1", LegendPosition -> {1.1, -0.4}}] • Is there a way to place the color bar horizontally instead of vertically?
– a06e
Feb 22, 2013 at 15:09
• @becko ShowLegend[ GraphicsGrid[ Partition[ Table[DensityPlot[Sin[i x] Sin[y/i], {x, -4, 4}, {y, -3, 3}], {i, 1, 4}], 2]], {ColorData["LakeColors"][1 - #1] &, 10, " 1", "-1", LegendPosition -> {1.1, -0.4}, LegendOrientation -> Horizontal}] Feb 22, 2013 at 15:14
• <<PlotLegends  ಠ_ಠ
– rm -rf
Feb 22, 2013 at 15:23
• It's never been exactly Kosher. Feb 22, 2013 at 16:28
• drop shadows like that were popular ... in the 1980s :) Feb 22, 2013 at 17:26