## How to draw dots at beginning and end of range when plotting

1

I'm trying to plot a Piecewise function which should look like this:

How can I have similiar dots (filled or empty) at beginning and end of range?

F[x_] := Piecewise[{{0, -Infinity < x <= -4}, {0.2, -4 < x <= -2}, {0.5, -2 < x <= 2}, {0.9, 2 < x <= 6}}, 1]
Plot[F[x], {x, -6, 10}, PlotTheme -> "Detailed"]


This is how my plot looks like right now:

Question was closed 2015-03-15T02:48:53.757

2

– Michael E2 – 2015-03-15T01:24:03.833

1It worked, however I'm surprised it's not included as part of Mathematica and requires external functions. – stil – 2015-03-15T01:42:32.943

5

Update: Post-processing using custom Arrowheads:

g1 = Graphics[Disk[]];
g2 = Graphics[{EdgeForm[Thick], FaceForm[White], Disk[]}];

plt /. Line[x_] :> {Arrowheads[{{-.01, 0, g2}, {.0125, 1, g1}}], Arrow[x]}


Original post:

One approach is to post-process the plot output to add filled and an empty circles to the lines:

f[x_] := Piecewise[{{0, -Infinity < x <= -4}, {0.2, -4 < x <= -2},
{0.5, -2 < x <= 2}, {0.9, 2 < x <= 6}}, 1]
plt=Plot[f[x], {x, -6, 10}, PlotTheme -> "Detailed"];

plt/. Line[x_] :> {Line[x], AbsolutePointSize[8], Point[Last@x],
AbsolutePointSize[8], Point[First@x],
{White, AbsolutePointSize[5], Point[First@x]}}


In version 9, you can specify the PlotStyle to add filled and empty circles to Line primitives (this trick doesn't seem to work in version 10):

Plot[f[x], {x, -6, 10}, PlotLegends -> "Expressions",
PlotStyle -> ({#, AbsolutePointSize[8], Point[Last @@ #],
Point[First @@ #], {White, AbsolutePointSize[5], Point[First @@ #]}} &)]


f2[x_] := Piecewise[{{x^2 - 5, -Infinity < x <= -4}, {x + 5, -4 <  x <= -2},
{10 - x^2, -2 < x <= 2}, {x^2 - 2 x - 4, 2 < x <= 6}}, 10]

Plot[f2[x], {x, -6, 10}, PlotLegends -> "Expressions",
PlotStyle -> ({#, AbsolutePointSize[8], Point[Last @@ #],
Point[First @@ #], {White, AbsolutePointSize[5], Point[First @@ #]}} &)]


It's much shorter solution than very long function in another thread. You could just reverse filled and empty dot - they should be opposite directed. – stil – 2015-03-15T02:04:16.043

@stil, updated with the fix. – kglr – 2015-03-15T02:56:44.843

Very nice. I wonder if you could hide dot when function domain is out of axis bounds. For example, when there is domain between minus infinity and -4, and the x axis is just between -6 and 10, there shouldn't be any dot at x=-6. – stil – 2015-03-15T14:51:39.143