# Plotting jump function without vertical lines

Consider this list plot:

ListPlot[{{0, 0}, {1, 2}, {3, 4}, {4, 2}, {6, 0}}]


I want to draw a jump function in this list plot such that for each point I get a line to the right until the next point occurs. I do not want to see the vertical lines (as is done automatically by ListLinePlot)! How can I achieve that?

Equivalently, how can I get rid of the vertical lines of a jump function in a ListLinePlot?

• mathematica.stackexchange.com/a/10502/193 Nov 12, 2014 at 14:59
• mathematica.stackexchange.com/q/8576/193 Nov 12, 2014 at 15:00
• Exclusions is an option for Plot, not for ListLinePlot or ListPlot. In my case my jumps are very close to each other, so when I try to somehow convert the data into a Piecewise function then I don't see all the jumps (the mash must be variable). Or is it possible to convert a ListLinePlot to Plot?
Nov 12, 2014 at 15:13
• Vaguely related: (30438) Nov 12, 2014 at 15:48

I believe this is what you want?

ListLinePlot[
{{0, 0}, {1, 2}, {3, 4}, {4, 2}, {6, 0}},
InterpolationOrder -> 0,
Frame -> True
] /. Line[x : {{_, _} ..}] :> (Line /@ Partition[x, 2])


Post-processing of the Graphics expression generated by ListLinePlot is used in the form of ReplaceAll. The Line is split into pairs of points using Partition, avoiding the drawing of vertical connecting segments.

Post-processing is a quick way to hack out a solution to a lot of plotting problems and it lets you work with the full range of e.g. ListLinePlot options. However it can also be slow and fragile. In the case above every Line expression is modified whether it originates from the plot itself or for example an Epilog option. I do in practice use post-processing and at times it can be the best solution. However often it is more robust and performs better to construct your own plotting function using Graphics directly. Here is a simple example:

myPlot[dat : {{_, _} ..}, opts : OptionsPattern[Graphics]] :=
Module[{rhold},
rhold[{{x_, y_}, {X_, Y_}}] := {{x, y}, {X, y}};
Graphics[Line[ rhold /@ Partition[dat, 2, 1] ], opts]
]


And its use:

myPlot[{{0, 0}, {1, 2}, {3, 4}, {4, 2}, {6, 0}},
BaseStyle -> {Red, AbsoluteThickness[2]},
Frame -> True]


You can create custom Options for your plot function to further customize its syntax. See:

• Exactly! Thanks! I tried to get rid of the vertical lines generated by ListLinePlot as you afterwards, but with the conventional for-loop, which created some trouble with indexing. Your functional solution is much nicer!
Nov 12, 2014 at 15:32
• @yadaddy You're welcome. Now that I confirmed what you want I'll give you another way to generate it. Look for an update in a few minutes. Nov 12, 2014 at 15:34
• @yadaddy Update complete. By the way, welcome to Mathematica Stack Exchange! Nov 12, 2014 at 15:43
• Thanks again. I understood the goal of the construction. Quite strange that this naturally appearing plot is not directly built in into Mathematica.
Nov 12, 2014 at 16:02
• @yadaddy FYI: I modified myPlot to use only a single Line expression; this speeds rendering in the Front End. Nov 13, 2014 at 14:37

If you just wrap your data with TemporalData, you can Plot or DiscretePlot the "PathFunction". In either case, there is no need for additional post- or pre-processing of the data to deal with jumps.

data = {{0, .5}, {1, 2}, {3, 4}, {4, 2}, {6, 1}};
td = TemporalData[data];


Using Plot and the Exclusions option:

Plot[Quiet@td["PathFunction"][x], {x, 0, 7}, PlotRange -> {0, 5},
Exclusions -> td["Times"][[1]], PlotStyle -> Directive[{Red, Thick}]]


Note: On v10.0.1 (Mac) you need to use td["Times"] instead of td["Times"][[1]] (Thanks: @BobHanlon)

Using DiscretePlot and the Extentsize option:

DiscretePlot[Quiet@td["PathFunction"][x], {x, 0, 7}, PlotRange -> {0, 5},
ExtentSize -> Right, Filling -> None, PlotStyle -> Directive[{Red, Thick}]]


Update: In both cases, adding the option

Epilog -> {Blue, PointSize[.02], Point[td["Path"]]}


gives

As an alternative to post-processing the plot, you can pre-process the data.

data = {{0, 0}, {1, 2}, {3, 4}, {4, 2}, {6, 0}};

func[x_] = Total[Partition[data, 2, 1] /.
{{x1_, y1_}, {x2_, y2_}} :>
y1*(UnitStep[x - x1] - UnitStep[x - x2])] //
Simplify


Piecewise[{{2, Inequality[1, LessEqual, x, Less, 3] || Inequality[4, LessEqual, x, Less, 6]}, {4, Inequality[3, LessEqual, x, Less, 4]}}, 0]

Plot[func[x],
{x, Min[data[[All, 1]]], Max[data[[All, 1]]]},
Epilog -> {Red, AbsolutePointSize[4], Point[data]},
PlotRange -> All]


• Very nice solution!
• @yadaddy I am curious why you switched the Accept to this answer. Is it not similar to my myPlot approach? However using Plot is not as efficient as drawing Lines directly. If there is a problem with my code please let me know and I shall try to correct it. Nov 13, 2014 at 5:31