7
$\begingroup$

For this toy problem

Needs["PlotLegends`"]

GraphicsGrid[
 Partition[
  Table[ListPlot[
    Table[{x, f[x]}, {f, {Sin, Cos}}, {x, 0, Pi, Pi/10.}], 
    PlotRange -> {{0, Pi}, {-1, 1}}, 
    PlotLegend -> {"Sine", "Cosine",}, LegendPosition -> {1.1, -0.4}, 
    PlotMarkers -> Automatic], {k, 1, 6, 1}], 3], Spacings -> 0, 
 ImageSize -> 700]

I was trying to make a single legend (precisely the legend you see here four times) for the entire GraphicsGrid rather than the same legend four times. How do I do so?


Edit: Same question but now with v9 code that doesn't require loading any package:

GraphicsGrid[
 Partition[
  Table[ListPlot[{Sqrt[Range[50]], Power[Range[50], (3)^-1]}, 
    PlotLegends -> 
     PointLegend[Automatic, {"one", "two"}, LegendFunction -> "Frame",
       LegendLabel -> "datasets"]], {k, 1, 6}], 3], ImageSize -> 800]

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ which version are you using? Version 9 has built-in plot legends which is better than the old package. $\endgroup$
    – rm -rf
    Feb 6, 2014 at 21:25
  • $\begingroup$ I am using v9... $\endgroup$
    – JohnD
    Feb 6, 2014 at 23:07

2 Answers 2

11
$\begingroup$

You can generate the legends separately with version 9's legends, but you'll have to manually input the colors (i.e. Automatic won't work):

With[{cols = {Red, Blue}},
    Column[{
        GraphicsGrid[
            Partition[
                Table[
                    ListPlot[{Sqrt[Range[50]], Power[Range[50], (3)^-1]}, PlotStyle -> cols], 
                    {k, 1, 6}
                ], 
                3], 
            ImageSize -> 800
        ],
        PointLegend[cols, {"one", "two"}, LegendLayout -> "Row", LegendMarkers -> Automatic,
            LegendFunction -> "Frame", LegendLabel -> "Datasets"]
    }, 
    Alignment -> Center]
]

$\endgroup$
3
  • $\begingroup$ You can use LegendMarkers -> Automatic in PointLegend to match PlotMarkers -> Automatic in ListPlot. The OP might want this. $\endgroup$
    – Szabolcs
    Feb 6, 2014 at 23:20
  • $\begingroup$ @Szabolcs Ah yes, thanks. Forgot to add that... Will edit now. $\endgroup$
    – rm -rf
    Feb 6, 2014 at 23:37
  • $\begingroup$ @rm-rf Nice:clean and simple! $\endgroup$ Feb 7, 2014 at 5:16
7
$\begingroup$

This is not any improvement over rm-rf. In v9 you can also use Legended:

Module[{f, col = {Red, Blue}, exp, leg, pm = {\[DiamondSuit], 20}},
 f[legend_] := 
  Framed[legend, FrameStyle -> Red, RoundingRadius -> 10, 
   FrameMargins -> 5, Background -> LightBlue];
 exp = GraphicsGrid[
   Partition[
    Table[ListPlot[{Sqrt[Range[0, 50, 5]], 
       Power[Range[0, 50, 5], (3)^-1]}, PlotStyle -> col, 
      PlotMarkers -> pm, Joined -> True], {k, 1, 6}], 3], 
   ImageSize -> 800, Frame -> True];
 leg = LineLegend[col, {"one", "two"}, LegendLayout -> "Row", 
   LegendMarkers -> pm, LegendFunction -> f, 
   LegendLabel -> "Datasets"];
 Legended[Image[exp], Placed[leg, {Center, Below}]]]

I made modifications just for illustrative purposes.

enter image description here

$\endgroup$

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.