1
$\begingroup$

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

example

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:

how it looks now

$\endgroup$
2

1 Answer 1

5
$\begingroup$

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]}

enter image description here


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]}}

enter image description here

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 @@ #]}} &)]

enter image description here

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 @@ #]}} &)]

enter image description here

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – stil
    Commented Mar 15, 2015 at 2:04
  • $\begingroup$ @stil, updated with the fix. $\endgroup$
    – kglr
    Commented Mar 15, 2015 at 2:56
  • $\begingroup$ 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. $\endgroup$
    – stil
    Commented Mar 15, 2015 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.