0
$\begingroup$

This question already has an answer here:

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.

$\endgroup$

marked as duplicate by Öskå, Sjoerd C. de Vries, Yves Klett, Dr. belisarius plotting Dec 20 '14 at 6:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Have you seen this? $\endgroup$ – Öskå Dec 19 '14 at 15:39
  • $\begingroup$ 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! $\endgroup$ – Tom Zinger Dec 19 '14 at 15:45
  • $\begingroup$ 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. $\endgroup$ – David G. Stork Dec 19 '14 at 16:09
  • $\begingroup$ @DavidG.Stork V8 has a very bad legend maker :) $\endgroup$ – Öskå Dec 19 '14 at 16:12
1
$\begingroup$

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

Mathematica graphics

$\endgroup$

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