3
$\begingroup$

I'm trying to plot the following piecewise function. $$ f(x, y) = \begin{cases} 1, & \text{if $xy = 0 $} \\ 0, & \text{if $xy \neq 0$} \end{cases} $$

This is the code I've written:

f[x_, y_] := Piecewise[{{1, x*y == 0}, {0, True}}]
Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}]

This is the result I got:

The graph of f(x, y)

But the plot doesn't show the lines that are "lifted" from the surface. How do I fix this?

$\endgroup$
2
  • 1
    $\begingroup$ Plot3D[] is never going to plot any one-dimensional components by itself, so you need to do extra work like what was shown in the answers you got. (Similar considerations apply for ParametricPlot3D[] with two independent variables.) $\endgroup$ Commented Apr 10, 2023 at 8:51
  • $\begingroup$ @J.M.'spersistentexhaustion Thank you for pointing that out. That helps. $\endgroup$ Commented Apr 10, 2023 at 16:38

3 Answers 3

4
$\begingroup$

One way is to find the parametric equations of the missing lines and plot them separately using ParametricPlot3D, then combine the plots using Show:

p1=Plot3D[Piecewise[{{1,x*y==0}, {0, True}}], {x, -10, 10}, {y, -10, 10}];
p2=ParametricPlot3D[{t,0,1},{t,-10,10}];
p3=ParametricPlot3D[{0,t,1},{t,-10,10}];
Show[p1,p2,p3]

enter image description here

$\endgroup$
4
$\begingroup$
  • We set the Exclusions Style to any style to keep the exclusion parts.
f[x_, y_] := Piecewise[{{1, x*y == 0}, {0, True}}]
plot = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}, 
  ExclusionsStyle ->Red, 
  Exclusions -> "Discontinuities"]
  • Then we lift the exclusion parts to z=1.
indexs = Position[plot, GraphicsGroup];
Show[Delete[plot, indexs // Last // Most],
 DeleteCases[
   Delete[plot, indexs // First // Most], _Line, -1] /. {x_Real, 
    y_Real, z_Real} :> {x, y, 1}, Boxed -> False, Axes -> False, 
 PlotRange -> All]

enter image description here

  • test another condition.
Clear["Global`*"];
f[x_, y_] := Piecewise[{{1, Cos[x] + Cos[y] == 1/2}, {0, True}}]
plot = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}, 
  ExclusionsStyle -> Red, Exclusions -> "Discontinuities"]
indexs = Position[plot, GraphicsGroup];
Show[Delete[plot, indexs // Last // Most], 
 DeleteCases[
   Delete[plot, indexs // First // Most], _Line, -1] /. {x_Real, 
    y_Real, z_Real} :> {x, y, 1}, Boxed -> False, Axes -> False, 
 PlotRange -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ f[x_, y_] = Piecewise[{{1, Cos[x] + Cos[y] - 1/2 == 0}, {0, True}}]; plot = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}, ExclusionsStyle -> None]; curves = Plot3D[1, {x, -10, 10}, {y, -10, 10}, MeshFunctions -> Function[{x, y}, Cos[x] + Cos[y] - 1/2], Mesh -> {{0}}, MeshStyle -> Red, PlotStyle -> None, BoundaryStyle -> None]; Show[plot, curves, Boxed -> False, Axes -> False] $\endgroup$
    – cvgmt
    Commented Apr 10, 2023 at 1:29
4
$\begingroup$
 Show[Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10},
   PlotRange -> 3/2, BoxRatios -> 1, Boxed -> False, Axes -> False], 
 Plot3D[f[0, y], {x, -10, 10}, {y, -10, 10}, 
   PlotStyle -> None, BoundaryStyle -> None, Exclusions -> None, 
   MeshFunctions -> {# #2 &}, Mesh -> {{0}}, 
   MeshStyle -> Directive[Red, Thick]]]

enter image description here

$\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.