Add a legend to a DiscretePlot - Version 8 [duplicate]

I have found several topics with legend issues but no answer to my problem. I have a function and I need to plot it for integers only (hence discrete plot) and then add a legend too. But PlotLegend does not work, its always red (my code gets red) and does not plot it. I can confirm that it does work when I use Plot instead of DiscretePlot. Nevertheless I need my variable to be positive integer.

function[gh_, ga_] :=
If[gh == ga, 1 + 0.18^((gh + ga)^0.25),
1 + 0.18^((gh - ga)^2)^0.25];

DiscretePlot[{function[0, i]^i, function[0 + 1, i]^i,
function[0 + 2, i]^i}, {i, 0, 30}, PlotRange -> Full]


any ideas? I use Mathematica 8.0.0.0.

• Have you seen this? – Öskå Dec 19 '14 at 15:39
• yes but it seems too complicated for what I needed to do. I cannot understand why I am not allowed to use PlotLegend with discreteplot. Nevertheless thanks for answering! – Fierce82 Dec 19 '14 at 15:45
• DiscretePlot[{function[0, i]^i, function[0 + 1, i]^i, function[0 + 2, i]^i}, {i, 0, 30}, PlotRange -> Full, PlotLegends -> {"x", "y", "z"}] works fine in 10.0. – David G. Stork Dec 19 '14 at 16:09
• @DavidG.Stork V8 has a very bad legend maker :) – Öskå Dec 19 '14 at 16:12

I voted to close this as a duplicate but since the OP said "it seems too complicated for what I needed to do" here is a way to do it.

Copy the whole code given by Jens and modify the function legendMaker by commenting Line[...] in f:

If[#1 === {None} || (PlotStyle /. {opts}) === None, {}, Line[{{-.1,0}, {.1,0}}]]


should be:

If[#1 === {None} || (PlotStyle /. {opts}) === None, {}, (* Line[{{-.1,0}, {.1,0}} *)]]


You can simply remove the whole line but commenting it allows you to use it afterwards.

Then simply use:

function[gh_, ga_] := If[gh == ga, 1 + 0.18^((gh + ga)^0.25), 1 + 0.18^((gh - ga)^2)^0.25];
p = DiscretePlot[{function[0, i]^i, function[0 + 1, i]^i, function[0 + 2, i]^i}, {i, 0, 30},
PlotRange -> Full];

opts = Sequence[Background -> Transparent, RoundingRadius -> 10];
Overlay[{p,
legendMaker[{"1", "2", "3"},
PlotStyle -> (ColorData[1][#] & /@ Range@3),
PlotMarkers -> {\[FilledSmallCircle]}, opts]},
Alignment -> {Right, Top}]