I have some function f[x,a] and I would like to plot it, on a subdomain of {x,0,1} that is dependent on x and a in some way, g[x,f,a]<0. At the moment I have, for example

Plot[Evaluate[Table[f[x,a], {a,3}]], {x,0,1}]

But f isn't well-defined beyond this subdomain so the output is nonsense and I would like it clipped. But the subdomain is different for each plot, etc. How can I add some clipping that enforces that domain constraint?

(This may not be easy in the general case, but it is sufficient here to assume that the constraint is satisfied at x=0 and there will be some minimal nonzero x from which it will be violated and clipping from there is enough)

A minimal example can be really simple: f[x_,a_]:=a x; and g[x_,f_,a_]:=f+a/10-1;

  • $\begingroup$ please post a complete self contained example. Including definition of f. You can limit the plot range using the PlotRange option $\endgroup$
    – Nasser
    Nov 5 '17 at 19:38
  • $\begingroup$ @Nasser but it seems that PlotRange is constant, and I do not have an analytical expression for the limiting x since in reality f and g are solutions to equations, so they're interpolating functions, say. $\endgroup$
    – bjorne
    Nov 5 '17 at 19:54

Might be kind of makeshift, but one possibility is to use the Piecewise function to limit the domain (setting the "default" value to Undefined or Indeterminate):

f[x_, a_] := a*x;
g[x_, f_, a_] := f[x, a] + a/10 - 1;

fplot[x_, a_] := Piecewise[
  {{f[x, a], g[x, f, a] < 0}},

 Evaluate[Table[fplot[x, a], {a, 3}]], {x, -1, 1},
 PlotRange -> {{-1, 1}, {-1, 1}},
 PlotLegends -> (("a = " <> ToString@#) & /@ Range[3]),
 AspectRatio -> 1

Example Plot

Hopefully this is somewhat close to what you had in mind!

  • $\begingroup$ That's an excellent idea $\endgroup$
    – bjorne
    Nov 5 '17 at 20:23
  • 1
    $\begingroup$ Instead of None, the more conventional symbols used for this are either Undefined or Indeterminate. $\endgroup$
    – J. M.'s torpor
    Nov 5 '17 at 20:28
  • $\begingroup$ Good to know, thanks! $\endgroup$
    – Anne
    Nov 5 '17 at 20:33

You can use RegionFunction to restrict the region that is displayed, although the pure function can only depend on the x and y coordinates. For your example, you could use:

g[x_, y_] := y + y/(10 x) - 1 < 0


    Evaluate @ Table[f[x, a], {a, 3}], {x, -1, 1},
    RegionFunction -> g,
    PlotRange->{{-1, 1}, {-1, 1}}

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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