6
$\begingroup$

I have a list of plot generated in loop using

z = List[];
For[i = 0, i < 10, i++,
  (
    a = Plot[(x + i)^2, {x, -10, 10}]; 
   AppendTo[z, a]
   )];
Show[z]

I would like to make the Show command use different color for each plot, how can I do this?

If relevant, that's not really my function generating the plot. I have a complicated procedure generating some data points which I plot using ListPlot with a joined line.

a = ListPlot[data, Joined -> True];
$\endgroup$
1
  • $\begingroup$ Taken at face value this question is a duplicate of (17250). The work-around, PlotStyle is in my opinion "easily found in the documentation." $\endgroup$
    – Mr.Wizard
    Jul 4, 2017 at 12:10

4 Answers 4

7
$\begingroup$
z = List[];
For[i = 0, i < 10, i++,
  (
    a = Plot[(x + i)^2, {x, -10, 10}, PlotStyle -> ColorData[97, i + 1]];
   AppendTo[z, a]
   )];
Show[z]

enter image description here

Alternatively

Plot[Evaluate@Table[(x + i)^2, {i, 0, 9}], {x, -10, 10},
 PlotRange -> {Automatic, 106}]

and with lists

ListLinePlot[Table[{x, (x + i)^2}, {i, 0, 9}, {x, -10, 10, 0.1}],
 PlotRange -> {Automatic, 106}]
$\endgroup$
2
  • 2
    $\begingroup$ I'd +1 but left For+AppendTo holds me :P I know it is not part of the question but still. $\endgroup$
    – Kuba
    Jul 4, 2017 at 11:03
  • 5
    $\begingroup$ @Kuba I often like to add in the minimum necessary to make the OP's code work, to show what was missing. Proper rewrites don't show that, as you can see by the alternate versions. $\endgroup$ Jul 4, 2017 at 11:19
4
$\begingroup$
Show @@ Table[Plot[(x + i)^2, {x, -10, 10},
   PlotStyle -> {Black, Red, Green, Blue}[[i + 1]]], {i, 0, 3}]

enter image description here

To add a legend:

colors = {Black, Red, Green, Blue};

Row[{
  Show @@ Table[Plot[(x + i)^2, {x, -10, 10},
     ImageSize -> 400,
     PlotStyle -> colors[[i + 1]]],
    {i, 0, 3}],
  SwatchLegend[colors, "i = " <> ToString@# & /@ Range[0, 3]]
  }]

enter image description here

You can also employ ListLinePlot

ListLinePlot[Table[{x, (x + i)^2}, {i, 0, 10, 2}, {x, -10, 10, 1}],
 PlotRange -> {Automatic, {Automatic, 10^2}},
 PlotMarkers -> Automatic,
 PlotLegends -> Range[0, 10, 2]]

enter image description here

$\endgroup$
3
$\begingroup$
fs = Array[Function[x, (x + #)^2] &, 10, 0]
Plot[Evaluate[#[x] & /@ fs], {x, -10, 10}, PlotRange -> {Automatic, 106}]
$\endgroup$
2
$\begingroup$

If the graphics a generated somewhere where you cannot get access to, then you can try to guess the color to replace and do this:

colorguess = ColorData[97][1];
Show[MapIndexed[{v, k} \[Function] (v /. colorguess -> ColorData[97][k[[1]]]), z]]

enter image description here

And as kuba mentioned, it is far better to generate your data z with the Table command. For and AppendTo are nasty things to use...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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