# Plot a list of functions with a corresponding list of ranges

I have 3 functions below and I want to plot them on the same graph with different range as follows.

• Plot f1 with x is in {2, 5} meaning that 2 ≤ x ≤ 5
• Plot f2 with x is in {3, 8} meaning that 3 ≤ x ≤ 8
• Plot f3 with x is in {1, 4} meaning that 1 ≤ x ≤ 4

I know that this can be done by plot each of them and then use Show function.
However, I'm trying to use only one Plot function to do it.
This is just an example as the number of functions and ranges are much larger than this.

f1 = 2 x;
f2 = x^2;
f3 = x^3 - 2 x^2 + 5;
functions = {f1,f2,f3};
ranges = {{2, 5}, {3, 8}, {1, 4}};
Plot[functions, ranges?]

• "However, I'm trying to use only one Plot function to do it." - why? Nevertheless, you can try using ConditionalExpression[] for this. Feb 17 at 20:25
• @J.M. I'm trying to apply this for another code so this method makes it easier for me apply it. Feb 17 at 20:31

{xmin, xmax} = MinMax[ranges];


### 1. ConditionalExpression

conditionalfunctions = MapThread[
ConditionalExpression[#, Between[#2] @ x] &,
{functions, ranges}];

Plot[conditionalfunctions, {x, xmin, xmax}]


You can also define conditionalfunctions as follows:

conditionalfunctions1 = MapThread[ConditionalExpression[#, #2 @ x] &] @
{functions, Between /@ ranges}

{functions, Between[x, #] & /@ ranges}

ConditionalExpression[functions, Between[x, #] & /@ ranges]


### 2. Piecewise

piecewisefunctions = MapThread[Piecewise[{{#, Between[x, #2]}}, Undefined] &] @
{functions, ranges};

Plot[piecewisefunctions, {x, xmin, xmax}]


### 3. Clip

functionswithclippedarg = MapThread[# /. x -> Clip[x, #2, {Null, Null}] &] @
{functions, ranges};

Plot[functionswithclippedarg, {x, xmin, xmax}]


• Using Between[]: Through[(Between /@ ranges)[x]], or MapThread[ConditionalExpression[#1, Between[x, #2]] &, {functions, ranges}]. Feb 17 at 20:28
• Thank you @J.M.; that's much cleaner.
– kglr
Feb 17 at 20:31
Clear["Global*"]

f1 = 2 x;
f2 = x^2;
f3 = x^3 - 2 x^2 + 5;
functions = {f1, f2, f3};
ranges = {{2, 5}, {3, 8}, {1, 4}};

Show[Plot @@@ Transpose[
{functions, Insert[#, x, 1] & /@ ranges}],
PlotRange -> All]


To use different PlotStyles

colors = {Red, Blue, Green};

Show[Plot[#[[1]], #[[2]], PlotStyle -> #[[3]]] & /@ Transpose[
{functions, Insert[#, x, 1] & /@ ranges, colors}],
PlotRange -> All]
`