Mathematica Plotting Multiple values on different domains

AI Thread Summary
To plot multiple constant values on different domains in Mathematica, use the Show function to combine individual plots into a single graph. Instead of generating separate plots, create a table of plots and then display them together with Show, ensuring they share the same axes. For adding a legend, include the PlotLegends option within the Table function to avoid multiple legends. This approach allows for a clean and organized visualization of the data. Utilizing these methods will enhance the clarity and effectiveness of your plots.
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 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}}]
 

Similar threads

Back
Top