2
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ Why not use ParametricPlot3D[]? $\endgroup$ Mar 31, 2016 at 14:06
  • $\begingroup$ 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}] $\endgroup$
    – user9660
    Mar 31, 2016 at 14:09
  • $\begingroup$ Duplicate? Much like this earlier question which is only a discretisation away from yours. $\endgroup$ Mar 31, 2016 at 14:13
  • $\begingroup$ @J.M. I looked at that, but wasn't sure if it applied here. (I'm a Mathematica plotting novice.) $\endgroup$
    – jlconlin
    Mar 31, 2016 at 14:16
  • $\begingroup$ @Louis I was hoping for something more like the Plot3D, but that doesn't do what I need. In fact, it fails. $\endgroup$
    – jlconlin
    Mar 31, 2016 at 14:18

2 Answers 2

7
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ This is exactly what I'm thinking of. Can you change the color of each plot? $\endgroup$
    – jlconlin
    Mar 31, 2016 at 14:38
  • 1
    $\begingroup$ Working on that now $\endgroup$
    – Jason B.
    Mar 31, 2016 at 14:38
  • $\begingroup$ Would've used the indexed colors myself, tho. $\endgroup$ Mar 31, 2016 at 15:25
  • $\begingroup$ 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 $\endgroup$
    – Jason B.
    Mar 31, 2016 at 15:30
  • $\begingroup$ This is great. I don't understand all of it, but it does what I need it to do. Thanks a bunch. $\endgroup$
    – jlconlin
    Mar 31, 2016 at 15:48
4
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.