Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 11 down vote accepted

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}],
    {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}],
    {i, 1, 4}], 2]
  ],
  Placed[
    BarLegend[{"LakeColors", {-1, 1}}, LegendLayout -> "Column"],
    Below
  ]
]

enter image description here

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

share|improve this answer
    
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? –  Szabolcs Feb 22 '13 at 21:13
1  
@Szabolcs you're looking for LegendMarkerSize, picking an appropriate size will require some fiddling, though. –  rcollyer Feb 22 '13 at 21:20
<< 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

share|improve this answer
    
Is there a way to place the color bar horizontally instead of vertically? –  becko Feb 22 '13 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}] –  belisarius Feb 22 '13 at 15:14
1  
<<PlotLegends` ಠ_ಠ –  rm -rf Feb 22 '13 at 15:23
2  
It's never been exactly Kosher. –  rcollyer Feb 22 '13 at 16:28
3  
drop shadows like that were popular ... in the 1980s :) –  cormullion Feb 22 '13 at 17:26

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

share|improve this answer
    
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] –  VLC Feb 22 '13 at 17:12
    
@VLC Yes, almost, but the last row of GraphicsGrid is full height, rather than shorter... –  cormullion Feb 22 '13 at 17:24
    
You could use Grid instead of GraphicsGrid to allow for different heights. –  Verbeia Feb 22 '13 at 22:37
    
@Verbeia That works too, and doesn't need ImageSize. Thanks! –  cormullion Feb 23 '13 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]
 ]

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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