0
$\begingroup$

I am trying to plot the outputs of a ListPlot with different colors for the 3 different cases and create a legend. I have tried with a simple For cycle, but then got that obviously that is not working because Mathematica correctly recognizes what is inside ListPlot as UNIQUE function, therefore my cycle is ineffective. How else can I do to obtain three different colors?

 fExact = Table[Table[Table[j i^2, {i, 10^k, 10^(k + 1), If[k >= 1, 10^k,10^(k 
 - 1)]}],{k, 0, 1, 1}], {j, {1, 2, 3}}];
 vector = Table[Table[Table[i, {i, 10^k, 10^(k + 1), If[k >= 1, 10^k, 10^(k 
 -1)]}],{k, 0, 1, 1}], {j, {1, 2, 3}}];
 Show[Table[Table[ListPlot[Transpose[{Transpose[{vector[[j]], fExact[[j]]}][[k]][[1]],
 Transpose[{vector[[j]], fExact[[j]]}][[k]][[2]]}], PlotStyle -> For[j = 1, j <= 3, j++, 
RGBColor[j, 0, 1]], 
Joined -> True], {k, 1, 1, 1}], {j, 1, 3, 1}], 
PlotRange -> {{0.001, 10}, {0.01, 10}}]

If I call:

try = Table[Table[ListPlot[Transpose[{Transpose[{vector[[j]], fExact[[j]]}][[k]][[1]], 
Transpose[{vector[[j]], fExact[[j]]}][[k]][[2]]}], Joined -> True, 
PlotStyle -> For[j = 0, j < 3, j++, RGBColor[j, 0, 1]]], {k, 1, 1,1}], {j, 1, 3, 1}]

I get:

enter image description here

Is there any way to access the PlotStyle option of the random try[[j]] element of try? In that way it should be easy to make a cycle and get what I want.

As for the legend, I tried PlotLegends->{j} but didn't work.

$\endgroup$
  • $\begingroup$ You can do PlotStyle->RGBColor[j,0,1]? (This isn't going to look very nice BTW.) I'd do something like PlotStyle->ColorData[97][j]. $\endgroup$ – N.J.Evans Apr 27 '17 at 13:54
  • $\begingroup$ @N.J.Evans why not nice? anyway, I 've just done what you suggested and still get that ALL the curves have the same colour following the first suggestion that you gave. Instead, the second one works! $\endgroup$ – Andrea G Apr 27 '17 at 13:58
  • $\begingroup$ rule number one for use of For is don't ever use it for anything (ever). If you insist on using it, realize It doesn't return anything. In this case its really unclear what you even intend by by using the outside Table iterator as a loop variable inside a For loop. $\endgroup$ – george2079 Apr 27 '17 at 14:09
  • 2
    $\begingroup$ RGBColor needs three arguments. Other things you can do for simple cases is like this {Red, Green, Blue}[[j]] , or Hue[j/4] . $\endgroup$ – george2079 Apr 27 '17 at 14:47
  • 2
    $\begingroup$ RGBColor arguments need to be in range 0-1 so RGBColor[j/4, 0, 1] may work (Although the colors will all be shades of purple so may not look very different ). $\endgroup$ – george2079 Apr 27 '17 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.