2

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 Fence Plot

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?

jlconlin
  • 247
  • 2
  • 7

2 Answers2

7

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}]

enter image description here

Or

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

enter image description here

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • This is exactly what I'm thinking of. Can you change the color of each plot? – jlconlin Mar 31 '16 at 14:38
  • 1
    Working on that now – Jason B. Mar 31 '16 at 14:38
  • Would've used the indexed colors myself, tho. – J. M.'s missing motivation Mar 31 '16 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 – Jason B. Mar 31 '16 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. – jlconlin Mar 31 '16 at 15:48
  • @J.M. - I modified it to use an indexed color function, but I can't seem to get it to work with both of these at the same time: "Colors" -> ColorData[97] and "Colors"->"Colors" -> ({EdgeForm[{Blue, Thick}], FaceForm[{Opacity[0], White}]} &) I want it to be more versatile. – Jason B. Mar 31 '16 at 15:59
4

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]

enter image description here

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.

Edmund
  • 42,267
  • 3
  • 51
  • 143