17
$\begingroup$

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?

$\endgroup$

4 Answers 4

21
$\begingroup$

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"]
]

enter image description here

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
  ]
]

enter image description here

When generated automatically using PlotLegends, Placed is inserted automatically, so you can use it to position the legend here.

$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$
    – Szabolcs
    Feb 22, 2013 at 21:13
  • 1
    $\begingroup$ @Szabolcs you're looking for LegendMarkerSize, picking an appropriate size will require some fiddling, though. $\endgroup$
    – rcollyer
    Feb 22, 2013 at 21:20
  • $\begingroup$ @rcollyer Hi, rcollyer. You example is not right in MMA 10.4. The color of densityplot is different to that in legend $\endgroup$
    – matheorem
    Apr 27, 2016 at 6:33
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    Apr 27, 2016 at 6:46
  • $\begingroup$ @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 $\endgroup$
    – matheorem
    Apr 27, 2016 at 7:15
8
$\begingroup$

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]

plots

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

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]
 ]

enter image description here

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}}}];

enter image description here

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.

enter image description here

After nudging the ticks down you get this:

enter image description here

$\endgroup$
3
$\begingroup$
<< 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}}]

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Is there a way to place the color bar horizontally instead of vertically? $\endgroup$
    – a06e
    Feb 22, 2013 at 15:09
  • $\begingroup$ @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}] $\endgroup$ Feb 22, 2013 at 15:14
  • 2
    $\begingroup$ <<PlotLegends` ಠ_ಠ $\endgroup$
    – rm -rf
    Feb 22, 2013 at 15:23
  • 2
    $\begingroup$ It's never been exactly Kosher. $\endgroup$
    – rcollyer
    Feb 22, 2013 at 16:28
  • 4
    $\begingroup$ drop shadows like that were popular ... in the 1980s :) $\endgroup$
    – cormullion
    Feb 22, 2013 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.