How to add a common color legend to a grid of density plots?
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.
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[[1]], plots[[2]]},
{plots[[3]], plots[[4]]}}, ImageSize -> 300],
legend}, Center]
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: