# Plot a piecewise function with black and white disks marking discontinuities

Let's say I'd like to plot Sign[x + 0.5]:

Plot[Sign[x + 0.5], {x, -1, 1}]


Mathematica will give this: This plot does not show very clearly the value of Sign[x + 0.5] at x = -0.5: is it 1? is it -1? No, it's 0, but there is no indication of which it is on the plot. This is what I'd like to achieve: (I used Paint to add the markers.) How can I make it Mathematica do it?

Update 2015-7-13: PlotPiecewise now handles discontinuities of non-Piecewise functions. (It basically did this before, but required the head to be Piecewise or contain a built-in discontinuous function, like Sign. All I did was brainlessly wrap a function in Piecewise. I also had to rewrite some of the Reduce/PiecewiseExpand code. One of the more complicated examples, which used to expand to a number of integer cases, kept the integer parameter instead of expanding; in another (not shown), the Ceiling of a complicated function was no longer expanded into a Piecewise function.

Update: PlotPiecewise now automatically tries to convert a non-Piecewise function into one using PiecewiseExpand. PiecewiseExpand will also do some simplification, such as factoring and reducing a rational function; to avoid that, pass a Piecewise function directly. PlotPiecewise will not alter the formulas in a Piecewise. Note, however, that Mathematica automatically reduces x (x - 1) / (x (x - 2)) but not (x^2 - x) / (x (x - 2)).

A while back I wrote a function PlotPiecewise to produce standard pre-calculus/calculus graphs of Piecewise functions. It works a lot like plot. It evolved from time to time, but it's nowhere near a complete package. For instance, PlotPiecewise is not HoldAll like Plot, because it was convenient for a certain use-case and my typical uses didn't need it either. There are a few options, but not really a complete set. Another limitation is that it was intended for rather simple piecewise functions, of the type one might ask students to graph by hand. For instance, it uses Reduce and Limit to find asymptotes but doesn't check if they worked; one should really check. It won't handle functions that are too complicated or that cannot be expanded as Piecewise functions.

I'll offer it, since it seems to do what the OP asks and I've already written. It seems worth sharing. I hope the community appreciates it. Code dump at the end.

OP's example

*Update: There is no longer a need to expand Sign. PlotPiecewise will automatically do it.**

PlotPiecewise[Sign[x + 1/2], {x, -1, 1}] More Examples

PlotPiecewise[
Piecewise[{
{ 1 / (-1 + 2 x),      0 < x <= 1},
{ 2,                       x == 0},
{ -x,                      x < 0},
{ (2 - x) / (-3 + x),  1 < x < 4},
{ -6 + x,              4 < x}},
Indeterminate],
{x, -2, 6.5},
AspectRatio -> Automatic, PlotRange -> {-2.8, 3.1},
Ticks -> {Range[-3, 6], Range[-4, 3]}] g[t_] := Piecewise[{
{Abs[1 + t], -2 <= t < 0},
{2, t == 0},
{2 - Cos[Pi*t], 0 < t < 2},
{(-16 - 12*t + t^3)/(8*(-4 + t)), t >= 2}},
Indeterminate];
PlotPiecewise[g[t], {t, -2, 6}, PlotRange -> {-2, 9}, DotSize -> Offset[{3, 3}]] Update: New cases handled

The updated code can do these things:

PlotPiecewise[Exp[(Sign[Sin[x^2]] - 3/4) x/2] - 1, {x, 0, 10}] PlotPiecewise[
Piecewise[{
{(-1 + 3*x)^(-1), -1 < x <= 1},
{Tan[Pi*x], 1 < x < 11/2}},
Indeterminate],
{x, -2, 6.5}, AspectRatio -> Automatic, PlotRange -> {-2.8, 3.1},
Ticks -> {Range[-3, 6], Range[-4, 3]}] New examples (2015-7-13)

PlotPiecewise[(2^x - 2)/(x - 1), {x, -2, 2},
"DotSize" -> Offset[{3, 3}],
"EmptyDotStyle" ->
EdgeForm[Directive[ColorData[97, 1], AbsoluteThickness[1.6]]]] A similar one:

PlotPiecewise[(2^x - 2)/(x^2 - 1), {x, -2, 2}] Code dump

Here is an update. It is I hope written in a somewhat better style. I also took the opportunity to add a couple of features. PlotPiecewise will automatically apply PiecewiseExpand, and if it produces a Piecewise function it will plot it. It will also handle a wider range of conditions in a Piecewise function. There is a bit more error checking, and some error messages have been added. It is still not HoldAll. Since Mr.Wizard prodded me to improve the code, I tried to keep it so that it would work in V7.

ClearAll[PlotPiecewise, PlotPiecewiseplot, PlotPiecewiseinit,
PlotPiecewisesolve, PlotPiecewiseexpand,
PlotPiecewiseannotatedPoints, PlotPiecewiseboundaryPoints,
PlotPiecewiseinteriorPoints, PlotPiecewisesowAnnotations,
PlotPiecewiseinDomain];

PlotPiecewise::usage =
"PlotPiecewise[Piecewise[...], {x, a, b}, opts]";
PlotPiecewise::limindet =
"Limit  is not numeric or infinite at ";
PlotPiecewise::nonpw =
"Function  is not a Piecewise function or did not expand to one";

PlotPiecewisedebug::debug = "";
PlotPiecewisedebug::plot = "";
PlotPiecewisedebug::annotation = "";
PlotPiecewisedebug::limit = "";
PlotPiecewisedebug =
Hold[PlotPiecewisedebug::debug, PlotPiecewisedebug::plot,
PlotPiecewisedebug::annotation, PlotPiecewisedebug::limit];
Off @@ PlotPiecewisedebug;

Options[PlotPiecewise] =
Join[{"DotSize" -> Automatic, "EmptyDotStyle" -> Automatic,
"FilledDotStyle" -> Automatic, "AsymptoteStyle" -> Automatic,
"BaseDotSize" -> Offset[{2, 2}],

"PiecewiseExpand" -> Automatic,           (* which fns.
to expand *)
"ContinuousEndpoints" -> Automatic},(* eval.
formula, not limit *)
Options[Plot]];
Options[EmptyDot] = Options[FilledDot] = Options[Asymptote] =
Options[PlotPiecewiseplot] = Options[PlotPiecewiseinit] =
Options[PlotPiecewise];

(* graphics elements *)
Clear[EmptyDot, FilledDot, Asymptote];
EmptyDot[pt_, opts : OptionsPattern[]] /;
OptionValue["EmptyDotStyle"] === None := {};
FilledDot[pt_, opts : OptionsPattern[]] /;
OptionValue["FilledDotStyle"] === None := {};
Asymptote[pt_, opts : OptionsPattern[]] /;
OptionValue["AsymptoteStyle"] === None := {};
EmptyDot[pt_, opts : OptionsPattern[]] := {White,
OptionValue["EmptyDotStyle"] /. Automatic -> {},
Disk[pt,
OptionValue["DotSize"] /.
Automatic -> OptionValue["BaseDotSize"]]};
FilledDot[pt_,
opts : OptionsPattern[]] := {OptionValue["FilledDotStyle"] /.
Automatic -> {},
Disk[pt,
OptionValue["DotSize"] /.
Automatic -> OptionValue["BaseDotSize"]]};
Asymptote[x0_, opts : OptionsPattern[]] := {Dashing[Large],
OptionValue["AsymptoteStyle"] /. Automatic -> {},

PlotPiecewise$inequality = Greater | Less | LessEqual | GreaterEqual; PlotPiecewise$discontinuousAuto = Ceiling | Floor | Round | Sign;
PlotPiecewise$discontinuousAll = Ceiling | Floor | Round | Sign |(*Min|Max|Clip|*)UnitStep | IntegerPart |(*FractionalPart|*)Mod | Quotient | UnitBox | UnitTriangle | SquareWave(*|TriangleWave|SawtoothWave*)(*|BernsteinBasis|\ BSplineBasis|Abs|If|Which|Switch*); PlotPiecewise$discontinuous = Ceiling | Floor | Round | Sign;

(* auxiliary functions*)

(*  causes Conditional solutions to expand to all possibilities;
(arises from trig eq, and C -- perhaps C, etc?
*)
PlotPiecewiseexpand[cond_Or, var_] :=
PlotPiecewiseexpand[#, var] & /@ cond;
PlotPiecewiseexpand[cond_, var_] :=
Reduce[cond, var, Backsubstitution -> True];
PlotPiecewisesolve[eq_, var_] /;
MemberQ[eq, PlotPiecewise$discontinuous, Infinity, Heads -> True] := PlotPiecewisesolve[# == C && C ∈ Integers && And @@ Cases[eq, Except[_Equal]], var] & /@ Cases[eq, PlotPiecewise$discontinuous[e_] :> e, Infinity];
PlotPiecewisesolve[eq_,
var_] := {var -> (var /. #)} & /@
List@ToRules@
PlotPiecewiseexpand[
Reduce[eq, var, Reals, Backsubstitution -> True],
var] /. {False -> {}};

(* limit routines for handling discontinuous functions,
which Limit fails to do *)
Needs["NumericalCalculus"];
PlotPiecewisenlimit[f_?NumericQ, var_ -> x0_, dir_] := f;
PlotPiecewisenlimit[f_, var_ -> x0_, dir_] :=
NLimit[f, var -> x0, dir];
PlotPiecewiselimit[f_, var_ -> x0_, dir_] /;
MemberQ[Numerator[f], PlotPiecewise$discontinuous, Infinity, Heads -> True] := Module[{y0, f0}, f0 = f //. (disc : PlotPiecewise$discontinuous)[z_] /;
FreeQ[z, PlotPiecewise$discontinuous] :> disc[ With[{dz = Abs[D[z, var] /. var -> N@x0]}, Mean[{z /. var -> N@x0, z /. var -> x0 - 0.1 Last[dir]/Max[1, dz]}]] ]; Message[PlotPiecewisedebug::limit, {f0, f, var -> x0, dir}]; Quiet[Check[y0 = PlotPiecewisenlimit[f0, var -> x0, dir], Check[y0 = Limit[f0, var -> x0, dir], If[! NumericQ[y0], y0 = Indeterminate]]], {Power::infy, Infinity::indet, NLimit::noise}]; y0 ]; PlotPiecewiselimit[f_, var_ -> x0_, dir_] := Module[{y0}, Quiet[Check[y0 = f /. var -> x0, Check[y0 = Limit[f, var -> x0, dir], If[! NumericQ[y0], y0 = Indeterminate]]], {Power::infy, Infinity::indet}]; y0 ]; PlotPiecewise$reverseIneq = {Less -> Greater, Greater -> Less,
LessEqual -> GreaterEqual};
PlotPiecewisereverseIneq[(rel : PlotPiecewise$inequality)[ args__]] := (rel /. PlotPiecewise$reverseIneq) @@
Reverse@{args};
PlotPiecewiseinDomain[] :=
LessEqual @@ PlotPiecewisedomain[[{2, 1, 3}]];
PlotPiecewiseinDomain[dom_] := LessEqual @@ dom[[{2, 1, 3}]];

(* annotatedPoints --
returns list of abscissas to be "annotated"
with dots/asymptotes
boundaryPoints --
returns list of boundaries numbers between \
pieces
interiorPoints --
returns list of points where the \
denominator is zero
*)
PlotPiecewiseannotatedPoints[allpieces_, domain_,
DeleteDuplicates@Flatten@Join[
PlotPiecewiseboundaryPoints[allpieces, domain],
PlotPiecewiseinteriorPoints[allpieces, domain],
];

PlotPiecewiseboundaryPoints[allpieces_, domain : {var_, _, _}] :=

With[{conditions =
DeleteDuplicates[
Equal @@@
Flatten[Last /@
allpieces /.
{HoldPattern@
Inequality[a_, rel1_, b_, rel2_,
c_] :> {PlotPiecewisereverseIneq[rel1[a, b]],
rel2[b, c]}, (rel : PlotPiecewise$inequality)[a_, b_, c_] :> {PlotPiecewisereverseIneq[rel[a, b]], rel[b, c]}} ]]}, Message[PlotPiecewisedebug::annotation, conditions]; var /. Flatten[ (* deletes no soln {}'s *) PlotPiecewisesolve[# && PlotPiecewiseinDomain[domain], var] & /@ conditions, 1] /. var -> {} (* no BPs in domain *) ]; PlotPiecewiseinteriorPoints[allpieces_, domain : {var_, _, _}] := MapThread[ Function[{formula, condition}, Flatten[ {With[{solns = PlotPiecewisesolve[ Denominator[formula, Trig -> True] == 0 && (condition /. {LessEqual -> Less, GreaterEqual -> Greater}) && LessEqual @@ PlotPiecewisedomain[[{2, 1, 3}]], PlotPiecewisevar]}, PlotPiecewisevar /. solns /. PlotPiecewisevar -> {} ], If[MemberQ[Numerator[formula], PlotPiecewise$discontinuous,
With[{solns =
PlotPiecewisesolve[
Numerator[formula] ==
0 && (condition /. {LessEqual -> Less,
GreaterEqual -> Greater}) &&
LessEqual @@ PlotPiecewisedomain[[{2, 1, 3}]],
PlotPiecewisevar]},
PlotPiecewisevar /. solns /. PlotPiecewisevar -> {}
],
{}
]}
]],
Transpose@allpieces
];

(* sowAnnotations - Sows irregular points, tagged with three ids;
"filled"    \[Rule] {x,y};
"empty"     \[Rule] {x,y};
"asymptote" \[Rule]  x;
*)
PlotPiecewisesowAnnotations[allpieces_,
domain : {var_, a_, b_}, {}] := {};
PlotPiecewisesowAnnotations[allpieces_, domain : {var_, a_, b_},
points_List] :=
(Message[PlotPiecewisedebug::annotation,
"sowAnn" -> {allpieces, points}];
PlotPiecewisesowAnnotations[allpieces, domain, ##] & @@@
Partition[{If[First[#] == a, Indeterminate, a]}~Join~#~
Join~{If[Last[#] == b, Indeterminate, b]}, 3, 1] &@
SortBy[points, N]);

PlotPiecewisesowAnnotations[allpieces_, domain : {var_, _, _},
xminus_, x0_?NumericQ, xplus_] :=

Module[{y0, yplus, yminus, f0, fminus, fplus},
f0 = First[
Pick @@ MapAt[# /. var -> x0 & /@ # &, Transpose@allpieces,
2] /. {} -> {Indeterminate}];
Quiet[y0 = f0 /. var -> N@x0, {Power::infy, Infinity::indet}];
If[xminus =!= Indeterminate, (* xminus ≠ left endpoint *)
fminus =
First[Pick @@
MapAt[# /. var -> Mean[{xminus, x0}] & /@ # &,
Transpose@allpieces, 2] /. {} -> {Indeterminate}];
yminus = PlotPiecewiselimit[fminus, var -> x0, Direction -> 1];
];
If[xplus =!= Indeterminate, (* xplus ≠ right endpoint *)
fplus = First[
Pick @@ MapAt[# /. var -> Mean[{x0, xplus}] & /@ # &,
Transpose@allpieces, 2] /. {} -> {Indeterminate}];
yplus = PlotPiecewiselimit[fplus, var -> x0, Direction -> -1];
];
If[Abs[yminus] == Infinity || Abs[yplus] == Infinity,
Sow[x0, "asymptote"]];
If[NumericQ[y0],
Sow[{x0, y0}, "filled"]];
Message[
PlotPiecewisedebug::annotation, {{x0, y0, f0}, {xminus, yminus,
fminus}, {xplus, yplus, fplus}}];
Sow[{x0, #}, "empty"] & /@
DeleteDuplicates@DeleteCases[Select[{yminus, yplus}, NumericQ], y0]
];

(* initialization of context variables *)

PlotPiecewiseinit[f : HoldPattern@Piecewise[pieces_, default_],
domain : {var_, _, _},
opts : OptionsPattern[]] :=
(PlotPiecewisedomain =
SetPrecision[domain, Infinity];
PlotPiecewisevar = var;
PlotPiecewiseallpieces =
If[default =!= Indeterminate,
Append[pieces,(*
add True case to pieces *)
{default,
If[Head[#] === Not, Reduce[#], #] &@
Simplify[Not[Or @@ (Last /@ pieces)]]}],
pieces] /. {formula_, HoldPattern@Or[e__]} :>
Sequence @@ ({formula, #} & /@ List[e]);
PlotPiecewise$discontinuous = OptionValue[ "PiecewiseExpand"] /. {Automatic -> PlotPiecewise$discontinuousAuto,
All -> PlotPiecewise$discontinuousAll, None -> {}}; Message[PlotPiecewisedebug::debug, "f" -> f] ); (* The main plotting function *) PlotPiecewiseplot[f : HoldPattern@Piecewise[pieces_, default_], domain : {var_, a_, b_}, opts : OptionsPattern[]] := Block[{PlotPiecewisevar, PlotPiecewisedomain, PlotPiecewiseallpieces, PlotPiecewise$discontinuous},
(* INITIALIZATION:
PlotPiecewisevar;
PlotPiecewisedomain;
PlotPiecewiseallpieces;
PlotPiecewise$discontinuous *) PlotPiecewiseinit[f, domain, opts]; Message[PlotPiecewisedebug::plot, "allpieces" -> PlotPiecewiseallpieces]; (* POINTS OF INTEREST *) With[{annotatedpoints = PlotPiecewiseannotatedPoints[ PlotPiecewiseallpieces, PlotPiecewisedomain, OptionValue["AdditionalPoints"]], plotopts = FilterRules[{opts}, Cases[Options[Plot], Except[Exclusions -> _]]]}, Message[PlotPiecewisedebug::plot, "annotatedpoints" -> annotatedpoints]; (* ANNOTATIONS *) With[{annotations = Last@Reap[ PlotPiecewisesowAnnotations[ PlotPiecewiseallpieces, PlotPiecewisedomain, annotatedpoints], {"asymptote", "empty", "filled"}]}, Message[PlotPiecewisedebug::plot, Thread[{"asymptote", "empty", "filled"} -> annotations]]; (* PROCESS PLOT *) With[{exclusions = Join[ If[OptionValue[Exclusions] === None, {}, Flatten[{OptionValue[Exclusions]}]], PlotPiecewisevar == # & /@ Flatten[First@annotations]](*can't we use annotatedpoints?*)}, With[{curves = Plot[f, domain, Evaluate@Join[{Exclusions -> exclusions}, plotopts]]}, Show[curves, Graphics[{ColorData, EdgeForm[ColorData], OptionValue[PlotStyle] /. Automatic -> {}, MapThread[ Map, {{Asymptote[#, PlotRange -> PlotRange[curves], opts] &, EmptyDot[#, opts] &, FilledDot[#, opts] &}, If[Depth[#] > 2, First[#], #] & /@ annotations}]}]]]] ]] ]; (* The user-interface *) PlotPiecewise[f : HoldPattern@Piecewise[pieces_, default_], domain_, opts : OptionsPattern[]] := PlotPiecewiseplot[f, domain, opts]; (* tries to expand f as a Piecewise function *) PlotPiecewisepweMethods = {"Simplification" -> False, "EliminateConditions" -> False, "RefineConditions" -> False, "ValueSimplifier" -> None}; PlotPiecewise[f_, domain : {var_, a_, b_}, opts : OptionsPattern[]] := Block[{PlotPiecewisegraphics}, (* restrict var in PiecewiseExpand/Reduce*) With[{a0 = If[# < a, #, # - 1/2] &@Floor[a], b0 = If[# > b, #, # + 1/2] &@Ceiling[b]}, With[{pwf = Assuming[a0 < var < b0, PiecewiseExpand[ f /. dis : PlotPiecewise$discontinuousAll[_] :> Piecewise[
Map[
{#[[1, -1]],
Replace[#[[2 ;;]], cond_ /; ! FreeQ[cond, C[_]] :>
(Reduce[#, var,
DeleteDuplicates@Cases[#, C[_], Infinity]] & /@
LogicalExpand[
cond /.
HoldPattern[And[e__?(FreeQ[#, var] &)]] :>
Reduce[And[e]]])
]} &,

List @@
Reduce[dis == C && C ∈ Integers &&
a0 < var < b0, {C, x}, Backsubstitution -> True]],
Indeterminate], Method -> PlotPiecewisepweMethods]]},
PlotPiecewisegraphics = PlotPiecewiseplot[pwf, domain, opts],
PlotPiecewisegraphics =
PlotPiecewiseplot[
Piecewise[{{f, -Infinity < var < Infinity}}, Indeterminate],
domain, opts]]
]];
PlotPiecewisegraphics
];

• quite a nice answer! – chris Dec 27 '13 at 10:28
• It looks like this code could benefit from refactoring. Would you care to work with me on that? – Mr.Wizard Dec 27 '13 at 13:25
• This is impressive. I'm baffled by the lack of ability to make common plots in Mathematica. :/ – user11426 Dec 27 '13 at 19:44
• @Mr.Wizard I went ahead and made some changes. If you have something to suggest, I would welcome it. Thanks! – Michael E2 Dec 30 '13 at 6:14
• @matan129 It should be possible. I probably tried a long time ago before I knew what I was doing, probably in 1990s. Many of my early attempts at package-writing were frustrating and I gave up writing them for a while. – Michael E2 Mar 16 '19 at 15:04

## Update:

To overcome the issue I mention in the comment, also to make it more general, here is an upgraded version, which can deal with clipping and pointwise exclusion, and preserve styles specified in the original plot.

The main function is this discontinuousHighlighter:

Clear[discontinuousHighlighter]
discontinuousHighlighter[origplot_, {excluMarker_, clipMarker_}, radius_: 3] :=
Module[{clipIntvX, black = Black, white, edgestyle},
edgestyle = {AbsoluteThickness, black};
white = If[# === None, White, #] &[Background /. AbsoluteOptions[origplot, Background]];
(* clipped intervals: *)
clipIntvX = Interval @@ Join @@ Cases[origplot,
{style___,lines:Longest[Line[_]..],___}/;Not[FreeQ[{style}, clipMarker]]:>{lines}[[All,1,All,1]],
∞];
origplot /.
(* exclusion boundary markers on curve: *)
{style___, Point[pts__]} /; Not[FreeQ[{style}, excluMarker]] :>
(black = If[# === {}, black, #[[-1]]] &@
DeleteCases[Cases[{style}, (RGBColor | Hue)[__], ∞], excluMarker];
edgestyle = Join[edgestyle, {style /. Directive | EdgeForm -> Sequence} // Flatten];
Flatten[{
EdgeForm[edgestyle],
FaceForm[white],
Module[{pt = #},
If[IntervalMemberQ[clipIntvX, pt[]],
{},
]
] & /@ pts} /. excluMarker -> Sequence[]]
) /.
(* exclusion markers on x axis: *)
{style___, lines : (Line[_] ..)} /; FreeQ[{style}, clipMarker] && Not[FreeQ[{style}, excluMarker]] :>
Join[
{style, lines} /. excluMarker -> Sequence[],
Flatten[{
EdgeForm[edgestyle],
FaceForm[black],
Disk[{Mean[#[[All, 1]]], 0}, Offset[radius]] & @@@ {lines}
}] /. excluMarker -> black
] /.
(* clipped interval: *)
{style___,lines:Longest[Line[_]..],post___}/;Not[FreeQ[{style},clipMarker]]&&Not[FreeQ[{post},clipMarker]] :>
({style, lines} /. clipMarker -> Sequence[]) //
Show[#, PlotRangeClipping -> False, PlotRangePadding -> Scaled[.05]] &
]


Basically, we calculate the x coordinates of the filled disks from the exclusion lines, and convert the endpoints on curve to hollow disks.

To use the function, wrap Plot[...] with it:

Module[{excluMarker = RGBColor @@ RandomReal[1, 3], clipMarker = RGBColor @@ RandomReal[1, 3]},
discontinuousHighlighter[
Plot[Exp[(Ceiling[Sin[x^2]] - 9/10) x] - 1.01, {x, -10, 10},
PlotStyle -> Directive[AbsoluteThickness],
PlotPoints -> 1000, MaxRecursion -> 15,
PlotRange -> {-1, 1},
ExclusionsStyle -> {
Directive[GrayLevel[.8], Dashed, excluMarker],
Directive[EdgeForm[{Lighter[Purple]}], excluMarker]
},
ClippingStyle -> Directive[Darker[Green], Thin, clipMarker],
Background -> Lighter[Yellow, .9]
],
{excluMarker, clipMarker},
3 ]
] You can use any style in ExclusionsStyle and ClippingStyle. As long as they contain excluMarker and clipMarker, the result should be parsed correctly by discontinuousHighlighter. And hollow disks will automatically fit the background color.

If you want to draw those disks and circles automatically, a convenient way would be post-processing a styled plot.

First we generate a unique mark for the ExclusionsStyle and ClippingStyle:

excluColor = RGBColor @@ RandomReal[1, 3];
clipColor = RGBColor @@ RandomReal[1, 3]


Use them to style the exclusions:

origplot = Plot[Floor[Tan[x]], {x, 0, π},
PlotStyle -> Directive[Thick],
Exclusions -> {Automatic, Cos[x] == 0},
ExclusionsStyle -> {excluColor, excluColor}] and the clipped intervals (we need this because the ExclusionsStyle seems to have a higher priority than ClippingStyle, and we don't want to mistake the clipped intervals for the discontinuous intervals):

clipplot = Plot[Floor[Tan[x]], {x, 0, π},
Exclusions -> {Automatic, Cos[x] == 0},
ExclusionsStyle -> None,
ClippingStyle -> clipColor]

clipIntv = Interval @@ Cases[clipplot,
{___, clipColor, lines : (Line[_] ..)} :>
Through[{Min, Max}@Flatten[#[[All, 1]] & @@@ {lines}]],
∞];


To draw the circles correctly, we need to know the aspect-ratio and set a appropriate radius:

radius = 0.02;
{rgx, rgy} = PlotRange.{-1, 1} /. AbsoluteOptions[origplot, PlotRange];
asprat = AspectRatio /. AbsoluteOptions[origplot, AspectRatio];


The remaining work is some replacements:

origplot /.
{excluColor, lines : (Line[_] ..)} :>
Flatten[{Black, Module[{x = Mean[#[[All, 1]]]},
If[IntervalMemberQ[clipIntv, x],
{},
Disk[{x, 0}, radius {1, 1/asprat rgy/rgx}]
]
] & @@@ {lines}}] /.
{excluColor, Point[pts__]} :>
Flatten[{EdgeForm[Black], FaceForm[White], Module[{pt = #},
If[IntervalMemberQ[clipIntv, pt[]],
{},
]
] & /@ pts}] //
Show[#, PlotRangeClipping -> False, PlotRangePadding -> Scaled[.05]] & • Nice! For discrete plots, one can also use DiscretePlot with ExtentMarkers – rm -rf Dec 27 '13 at 1:27
• @rm-rf Thanks. There are still issues here. e.g. for Plot[Exp[(Ceiling[Sin[x^2]]-9/10)x]-1,{x,0,10},PlotRange->{All,1}], some boundary points doesn't get circled. I'll try to fix it after lunch :) – Silvia Dec 27 '13 at 1:38
• Look up Offset[{dx, dy}, the last entry under Details. – Michael E2 Dec 27 '13 at 3:53
• @MichaelE2 Thanks, didn't know that! Will use it the next update. – Silvia Dec 27 '13 at 4:51
• @MichaelE2 I tried the Tooltip way, but they sometimes change to something like Tooltip[{}, "exclusion", TooltipStyle -> "TextStyling"], so I kept my RGBColor method. But Offset[radius] is really a great function. Thanks for the information! – Silvia Dec 27 '13 at 16:44

If you know a priori where are the discontinuities:

p = Plot[Sign[x + 0.5], {x, -1, 1}];
ap = AspectRatio /. AbsoluteOptions[p, AspectRatio]
epilog = {FaceForm[White], EdgeForm[Black],
Disk[{-1/2, -1}, .03 { ap, 1 }], Disk[{-1/2, 1}, .03 { ap, 1 }],
FaceForm[Black], Disk[{-1/2, 0}, .03 {ap, 1}]};

Plot[Sign[x + 0.5], {x, -1, 1}, Epilog -> epilog] • Thanks. This is great, but sadly, I may not know where discontinuities are. It would be great to generalise that; for more complicated functions it will be quite a hassle to specify these points. :( – user11426 Dec 26 '13 at 17:01
• On a related note, Wolfram|Alpha is able to create something very similar to what I'd like to: wolframalpha.com/input/?i=discontinuities+of+sign%28x%2B0.5%29 (Meaning Mathematica should be able too.) – user11426 Dec 26 '13 at 17:06
• @user11426 Plot[Sign[x + 0.5], {x, -1, 1}, ExclusionsStyle -> {Dashed, PointSize[Large]}] – Rojo Dec 26 '13 at 17:53
• I hope it is possible and simple to do this automatically for those discontinuities that Exclusions already detects – Rojo Dec 26 '13 at 17:54
• Yay! This is near perfect! But lacks white points. :( – user11426 Dec 26 '13 at 18:10