Plotting Multiple values on different domains

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
member 428835
Hi PF!

I have a list of numbers, and I'm trying to plot each constant value on a 1-unit long interval. What I'm trying is this

Code:
a = Table[i, {i, 2, 8}];
For[i = 1, i < 5, i++, Plot[a[[i]], {x, i - 1, i}] // Print]

but that generates plots that are not on the same axes. Any help?
 
Last edited by a moderator:
Physics news on Phys.org
I have edited your post to put the Mathematica code inside code tags. Otherwise, it doesn't come out properly.

What you need to do is to generate the plots inside a table, and then use Show to plots all of them on the same graph:
Code:
Show[Table[Plot[a[[i]], {x, i - 1, i}], {i, 1, 4}], PlotRange -> {{0, 4}, {0, 6}}]
 
  • Like
Likes   Reactions: member 428835
DrClaude said:
I have edited your post to put the Mathematica code inside code tags. Otherwise, it doesn't come out properly.

What you need to do is to generate the plots inside a table, and then use Show to plots all of them on the same graph:
Code:
Show[Table[Plot[a[[i]], {x, i - 1, i}], {i, 1, 4}], PlotRange -> {{0, 4}, {0, 6}}]
Hey thanks! so if I have ##a## and ##b##, do you know how to create a plot legend, because when I add PlotLegends inside the table I obviously get 4 legends? If statement or is there a better way?
 
Last edited by a moderator:
You can add a PlotLegend in the Plot, e.g,
Code:
Show[Table[
  Plot[a[[i]], {x, i - 1, i}, PlotLegends -> {i}], {i, 1, 4}], PlotRange -> {{0, 4}, {0, 6}}]