6
$\begingroup$

Using GraphicsGrid, I have put two figures that share the same x-axis in a row. However, I failed to let them share a common LineLegend.

The code is (codes about subgraphs oniplot and cpplot are given later):

Legended[
    GraphicsGrid[{{Graphics[oniplot], Graphics[cpplot]}}],
    Placed[LineLegend[{"factor = 2", "factor = 3", "factor = 4", "factor = 5"}], Above]
        ]

As shown in the following figure, LineLegend has been produced as a text rather than a legend.

linelegend

Question: How to share LineLegend among sub-figures in GraphicsGrid?



For subgraph oniplot:

onireplica2data = { {10, 0}, {20, 0}, {50, 0}, {100, 0}, {150, 0}, {200, 0} };
onireplica3data = { {10, N[199.0 / 172309]}, {20, N[124.0 / 193971]}, {50, N[224.0/ 290346]}, {100, N[311.0/ 400617]}, {150, N[314.0/ 449031]}, {200, N[291.0/481764]} };
onireplica4data = { {10, N[9.0 / 275350]}, {20, N[37.0 / 322928]}, {50, N[12.0 / 554718]}, {100, N[17.0 / 768261]}, {150, N[70.0 / 822346]}, {200, N[9.0 / 959027]} };
onireplica5data = { {10, N[208.0 / 637379]}, {20, N[168.0/732348]}, {50, N[199.0/1029314]}, {100, N[270.0/1372560]}, {150, N[261.0/1610192]}, {200, N[261.0/1610192]} };

oniplot = ListPlot[ {onireplica2data, onireplica3data, onireplica4data, onireplica5data}, 
    Frame -> True, Joined -> True,
    PlotMarkers -> {Automatic, 15},
    FrameLabel -> {"Async (ms)", "Proportion"},
    FrameTicks -> {{10, 20, 50, 100, 150, 200}, Automatic},
    FrameStyle -> Directive[FontSize -> 12, FontFamily -> "Helvetica"],
    PlotRange -> All, Axes -> True, ImageSize -> Medium]

For subgraph cpplot:

cpreplica2data = { {10, 200000}, {20, 200000}, {50, 200000}, {100, 200000}, {150, 200000}, {200, 200000} }; 
cpreplica3data = { {10, 172309}, {20, 193971}, {50, 290346}, {100, 400617}, {150, 449031}, {200, 481764} };
cpreplica4data = { {10, 275350}, {20, 322928}, {50, 554718}, {100, 768261}, {150, 822346}, {200, 959027} }; 
cpreplica5data = { {10, 637379}, {20, 732348}, {50, 1029314}, {100, 1372560}, {150, 1610192}, {200, 1610192} };

cpplot = ListPlot[{cpreplica2data, cpreplica3data, cpreplica4data, cpreplica5data},
    Frame -> True, Joined -> True,
    PlotMarkers -> {Automatic, 15},
    FrameLabel -> {"Async (ms)", "Numbers"},
    FrameTicks -> {{10, 20, 50, 100, 150, 200}, Automatic},
    FrameStyle -> Directive[FontSize -> 12, FontFamily -> "Helvetica"],
    PlotRange -> All, Axes -> True, ImageSize -> Medium]
$\endgroup$

1 Answer 1

12
$\begingroup$

You have to add the index for the colors to LineLegend

Legended[Grid[{{Show[oniplot], Show[cpplot]}}], 
  LineLegend[97, {"factor = 2", "factor = 3", "factor = 4", "factor = 5"}]]

Plot

In order to have the legend above the plots and with markers:

Legended[GraphicsGrid[{{oniplot, cpplot}}], 
 Placed[LineLegend[97, Array["factor = " <> ToString@# &, 4, 2], 
 LegendMarkers -> {Automatic, 15}, LegendLayout -> "Row"], Above]]

Plot3

$\endgroup$
3
  • $\begingroup$ but then you lose markers, or you need to input them manually in legend. $\endgroup$
    – funnyp0ny
    Commented Nov 25, 2014 at 14:48
  • $\begingroup$ @funnypony to get the makers one just has to add the option LegendMarkers -> Automatic to LineLegend. $\endgroup$
    – Karsten7
    Commented Nov 25, 2014 at 14:52
  • 2
    $\begingroup$ Wrapping the plots with Show is unnecessary in later versions of Mathematica (since v6?). $\endgroup$
    – Bob Hanlon
    Commented Nov 25, 2014 at 15:58

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.