# set colours for different parts of a piecewise function's graph [duplicate]

I would like to create a graph of the piecewise function
$f(x) = \begin{cases} x + 2, & x < -1 \\ x^2, & -1 \leq x \leq 2 \\ 3, & x > 2\end{cases}$

where the changes in the definition of $f(x)$ are clearly demonstrated. Specifically, I would like to:

a) have each part of the definition graphed in a different colour;

b) have the appropriate open/closed disks (any simpler method than my tedious solution of plotting each disk?)

Here is what I have so far:

Plot[Piecewise[{{2+x,x<-1},{x^2,-1<x&&x<=2},{3,x>2}},0],{x,-5,6}, PlotStyle ->Thick, Epilog->{PointSize[0.03],Blue,Point[{-1,1}],PointSize[0.03],Blue,Point[{2,3}],PointSize[0.02],White,Point[{2,3}]}]


Which yields this: Again, although this has the dots, it's a very tedious solution, and I would like to be able to set the colour of the three parts, separately (ideally have Mathematica do it automatically).

I am looking for a solution that students using a sandboxed version of Mathematica (provided through WolframAlpha Pro) could implement.

• Please check if the linked threads address your needs. If they don't, edit your question to explain why, and we can reopen this question. – J. M. will be back soon Aug 28 '17 at 21:45
• Have edited question to reflect the nuance between the questions (viz. sufficiently simple to be appropriate for sandboxed, student-version). Also, in >1H of searching and looking at over a dozen "related questions" and even composing this question - neither of those links were proposed. Does not really encourage users to do the research to avoid repeat questions.. I have tried to improve tagging on the other two, but perhaps s/o with more experience can have a look as well. – Rax Adaam Aug 28 '17 at 21:58
• If you students actually have Mathematica (as opposed to just Wolfram Alpha), they should be able to use the routines in the linked threads (probably after some suitable repackaging). If they only have access to Alpha, I don't think they can do any custom plots like this at all. – J. M. will be back soon Aug 28 '17 at 21:59
• They have access to the "Wolfram Development Platform" which comes with Alpha Pro, which seems to be in between the two. The routines might work, but a) I wouldn't know where to begin, personally, and b) the routines are lightyears beyond students ability to implement. – Rax Adaam Aug 28 '17 at 22:02
• The Wolfram Development Platform is version of Mathematica Online with a variety of licenses which vary by the amount of disk space and cpu time allowed. All of them, however, include the full Wolfram Language. I doubt that plotting anything that students are likely to type in will hit the CPU limits. – Itai Seggev Aug 28 '17 at 22:53

Plot[Piecewise[{#}, None] & /@ {{2 + x,
x < -1}, {x^2, -1 < x && x <= 2}, {3, x > 2}} //
Evaluate, {x, -5, 6}, PlotRange -> All,
PlotStyle -> Thick,
Epilog -> {PointSize[0.03], Blue, Point[{-1, 1}], PointSize[0.03],
Blue, Point[{2, 3}], PointSize[0.02], White, Point[{2, 3}]}]


yields Update: Demonstration of manual color manipulation. Also some minimalist conditioning around piecewise point graphics.

Specify conditions, e.g.:

conds = {{2 + x, x < -1}, {x^2, -1 < x && x <= 2}, {3, x > 2}};


and plot

Plot[
Piecewise[{#}, None] & /@ conds //
Evaluate, {x, -5, 6}, PlotRange -> All,
PlotStyle -> {{Thick, Red}, {Thick, Blue}, {Thick,
RGBColor[0.5, 0.19, 0.5]}},
Epilog->Flatten[{PointSize[0.03], joinPiece[conds[[{1, 2}]], x],
joinPiece[conds[[{2, 3}]], x]}]]


yielding: using the following minimal auto-formatting based on continuity (assuming region a < region b):

joinPiece[{a_, b_}, x_] := Module[{myVal},
If[ (a[] /. FindMaximum[{x, a[]}, x][[2, 1]]) === (
b[] /. (myVal = FindMinimum[{x, b[]}, x])[[2, 1]]
), {Blue,
Point[{myVal[[2, 1, 2]], a[] /. myVal[[2, 1]]}]}, {White,
EdgeForm[{Thick, Blue}],
Disk[{myVal[[2, 1, 2]], b[] /. myVal[[2, 1]]}, .15]}]];

• Brilliant, perfect - thank you. This appears to be so much simpler than the routines suggested on the linked pages, you may wish to also post your solution there to receive proper credit. Cheers! – Rax Adaam Aug 28 '17 at 22:04
• John - is it possible to specify the colours using this approach? – Rax Adaam Aug 28 '17 at 22:06
• Yes, with PlotStyle, (e.g. PlotStyle->{ Red, Blue, RGBColor[0.5, 0.19, 0.5]}). I'll update my answer to include. – John Joseph M. Carrasco Aug 28 '17 at 22:12
• thank you kindly for the elaborations and your time. Am astounded it is this complicated to create such common graphs, but am glad to have found a relatively straight-forward implementation. – Rax Adaam Aug 29 '17 at 19:20