I want to plot the following function:

enter image description here

I can easily use Pgfplots to plot each section, but I could not unite the two functions without using a vertical function (which I think is an ugly solution). Is there a way to correctly plot conditional functions?


1 Answer 1



You can put any pgfmath expression (see pgf manual, chapter: "Mathematical Expressions") as parameter to \addplot: In your case:

ifthenelse( <condition>, <then>, <else>)

or the short form:

<condition> ? <then> : <else>

Note: You could also mark the discontinuity, instead of drawing a vertical line (see How can I add open and closed circles on either side of a jump discontinuity?).


enter image description here




        %\addplot[red, samples=300] { ifthenelse(x > 4, 0, .1*(.5*x-4)^2) };
        \addplot[blue, samples=300] { x > 4 ? 0 : .1*(.5*x-4)^2 };

  • Perfect answer! Thank you very much! never thought to check the manual will do that next time! Feb 8, 2023 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .