# Plotting Heaviside function shows staircase plot

I'm trying to plot a Heaviside function over [-1,1]x[-1,1] using pgfplots. The function has a value of 1 when x+y > 1/2 and 0 otherwise (so the line is not aligned with x nor y axes). The problem is that the plot shows staircasing so the function does not look sharp close to the line. This is what I have so far in my MWE:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\pgfmathdeclarefunction{Heaviside}{2}{%
\pgfmathparse{#1+#2<-0.5 ? 0 : 1}%
}

\begin{document}
\begin{tikzpicture}

\begin{axis}

\addplot3[surf, domain=-1:1, y domain=-1:1, samples=20]
{Heaviside(x,y)};

\end{axis}
\end{tikzpicture}
\end{document}


which produces

There is nothing wrong with your plot. The solution to jaggedness for a 1D function plotted in 2D is to increase samples. You can do the same here, but you will quickly run into computer limits.

A solution can not involve a function, because the function would need to be evaluated in a huge number of points to show the discontinuity in a smooth way. The only solution I see is to use the fact that you know where the discontinuity is and then add the different areas with a patch plot like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{patchplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3[patch, shader=faceted, faceted color=black] coordinates {(-1,-1,0) (0.5,-1,0) (-1,0.5,0)};
\addplot3[patch, shader=faceted, patch type=polygon, vertex count=5, faceted color=black, forget plot] coordinates { (-1,0.5,1) (0.5,-1,1) (1,-1,1) (1,1,1) (-1,1,1)};
%\addplot3[patch, shader=interp, patch type=rectangle, patch refines=3, forget plot] coordinates { (-1,0.5,0) (0.5,-1,0) (0.5,-1,1) (-1,0.5,1)};
%\addplot3[patch, mesh, patch type=rectangle, black, forget plot] coordinates { (-1,0.5,0) (0.5,-1,0) (0.5,-1,1) (-1,0.5,1)};
\end{axis}
\end{tikzpicture}
\end{document}


• Can we specify the plotting domain instead of using coordinates? I ask because the objective is to plot the multiplication of this Heaviside function with another complex function, and of course I can't do that the way you describe it. Commented Sep 5, 2022 at 7:51
• I do not understand what your goal is. The plotting domain IS in the coordinates. If you can not analyse your function yourself, then the only options is to increase the samples. Try samples=100 Commented Sep 5, 2022 at 8:00
• So the function above is 1 in some part of the domain and 0 elsewhere. But the function I'm interested in is a multiplication of this function with a harmonic function. Therefore, I would have to multiply the function above by some complex function of sin and cos, and then plot the result. So if I use your approach above. I would have to specify the plotting domain for the non-zero component, and I don't think it can be done the way you show it. Commented Sep 5, 2022 at 11:27
• You need to ask a new question. This is the best I can answer for this question, and you have not even upvoted. Commented Sep 5, 2022 at 11:32
• The question is the same, all I ask is whether there's another way to specify the domain where a function is valid. You used coordinates so you plotted the function using a polygonal domain. There must be another way, perhaps by parameterization? I'm not sure but I guess yours is not the only way to address this problem (and thus why I haven't marked it as an answer yet). Commented Sep 5, 2022 at 12:01