# How to create several 3D plots of 2D functions in Mathematica?

I have multiple 2D functions defined and I'd like to plot them in (what I call) a fence plot. An example of a fence plot is

The three functions I want to plot (all defined on $-1 <= x <= 1$) are:

• $f(x) = 0.5x + 0.5$
• $g(x) = -0.5x + 0.5$, and
• $h(x) = 0.5$

Can someone show me how to do this in Mathematica?

• Why not use ParametricPlot3D[]? Mar 31, 2016 at 14:06
• f[x_] := 0.5 x + 0.5; g[x_] := [Minus]0.5 x + 0.5; h[x_] := 0.5; Plot[{f[x], g[x], h[x]}, {x, -1, 1}], you looking for? Or Plot3D[{f[x], g[x], h[x]}, {x, -1, 1}, {y, -1, 1}]
– user9660
Mar 31, 2016 at 14:09
• Duplicate? Much like this earlier question which is only a discretisation away from yours. Mar 31, 2016 at 14:13
• @J.M. I looked at that, but wasn't sure if it applied here. (I'm a Mathematica plotting novice.) Mar 31, 2016 at 14:16
• @Louis I was hoping for something more like the Plot3D, but that doesn't do what I need. In fact, it fails. Mar 31, 2016 at 14:18

So the idea here is to generate a plot, use the Filling->Axis option, then extract the polygons from that.

Options[fencePlot] = {"YValues" -> Automatic, "Colors" -> Automatic};
fencePlot[funcs_, {x_, xmin_, xmax_},
opts : OptionsPattern[{fencePlot, Graphics3D}]] :=
Module[{yv, pgons, colors},
yv = OptionValue["YValues"] /. Automatic -> Range[Length[funcs]];
colors = (OptionValue["Colors"] /. Automatic -> (ColorData[97])) /@
Range[Length[funcs]];
pgons = Table[{colors[[n]],
Cases[

Plot[funcs[[n]], {x, xmin, xmax}, Filling -> Axis,
PlotRange -> All] // Normal,
Polygon[__], Infinity] /.
Polygon[a__] :> Polygon[{#1, yv[[n]], #2} & @@@ a]}
, {n, Length@yv}];
Graphics3D[
pgons
, Evaluate@FilterRules[{opts}, Options[Graphics3D]] ,
Axes -> True]
]


Called via

fencePlot[{-.5 x + .5, .5 x + .5, .5}, {x, -1, 1}]


Or

fencePlot[Sin[π # x] & /@ Range[6, 0, -1], {x, 0, 1},
BoxRatios -> {1, 1, 1}]


• This is exactly what I'm thinking of. Can you change the color of each plot? Mar 31, 2016 at 14:38
• Working on that now Mar 31, 2016 at 14:38
• Would've used the indexed colors myself, tho. Mar 31, 2016 at 15:25
• I was looking at those but I don't fully understand them. Do they only take integer inputs? Do they all take the same range? They don't go from 0 to 1, so I'm at a loss Mar 31, 2016 at 15:30
• This is great. I don't understand all of it, but it does what I need it to do. Thanks a bunch. Mar 31, 2016 at 15:48

Using ParametricPlot3D as suggested by @J.M. in the OP comments.

With functions

f[x_] := 0.5 x + 0.5
g[x_] := -0.5 x + 0.5
h[x_] := 0.5


Then

ParametricPlot3D[
Evaluate[MapIndexed[{First@#2, u, v #1[u]} &]@{f, g, h}],
{u, -1, 1}, {v, 0, 1},
PlotRange -> Full,
PlotStyle -> Opacity[.85],
Mesh -> None]


ParametricPlot3D has attribute hold all so Evaluate needs to be called on MapIndexed for there to be three functions (each with its own colour) instead of one function (all three would have same colour). See PlotStlye for info on how to customise colours for the functions.

Hope this helps.