11

Suppose I have the following simple list.

l = {0,2,5,9,14};

I want to to make a plot that looks like the figure below.

I.e. I want to have a horizontal line between all the numbers in the list that increases by 1 unit in vertical height between the numbers aswell, so it looks more or less like a staircase. list step function

How can I do it? I've tried Discreteplot and kinda works, the only thing is that the axes are inverted and I can't add vertical lines that connects with the horizontal lines like in the figure.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Turbotanten
  • 691
  • 5
  • 18
  • I don't understand why it is so difficult to see that only one of the answers below reproduces the OP's figure. Yet the wrong answers have garnered five upvotes altogether so far. Makes me think this is a deeper question than it first appeared to me. (+1) – Michael E2 Oct 09 '17 at 12:56

7 Answers7

16
l = {0, 2, 5, 9, 14};
a0 = Total[UnitStep[t - l]] - 1;
Plot[a0, {t, First[l], Last[l]}, Exclusions -> None, Frame -> True, 
 PlotStyle -> {Directive[Red, Thick]}]

enter image description here

Anjan Kumar
  • 4,979
  • 1
  • 15
  • 28
  • I've tried the different solutions given in this thread and this seems like the solution with the least computational time required. – Turbotanten Oct 24 '17 at 07:41
9

The simplest way to make it is ListLinePlot with option InterpolationOrder:

l = {0,2,5,9,14};
ListLinePlot[l, InterpolationOrder -> 0, PlotStyle -> Directive[Red, Thick], 
    PlotRange -> {{0, 7}, {-5, 17}}]

The generated plot looks like bellow:

step function plot

Revision

Thanks @Michael E2 for reminding me. I misunderstand @Turbotanten at first. Bellow is my new revision.

data = Thread[{{0, 2, 5, 9, 14}, Range[0, 4]}];
ListLinePlot[data, InterpolationOrder -> 0, 
    PlotStyle -> Directive[Red, Thick], Frame -> True]

enter image description here

To eliminate the right most vertical line:

ListLinePlot[data /. {x_, y_} /; x == Max[l] -> {x, 3}, InterpolationOrder -> 0, 
    PlotStyle -> Directive[Red, Thick], Frame -> True]

enter image description here

PureLine
  • 1,310
  • 7
  • 20
8

Also, if you have a version 10.2 or above, you could use ListStepPlot

Module[
 {l},
 l = {0, 2, 5, 9, 14};
 ListStepPlot[l]
 ]

EDIT
Reversing the axis

Module[
 {l},
 l = {0, 2, 5, 9, 14};
 ListStepPlot[l, ScalingFunctions -> {"Reverse", Identity}]
 ]
e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
4

One needs to use the second documented form of ListStepPlot for this:

ListStepPlot[
    Thread[{{0,2,5,9,14},Range[0,4]}],
    PlotRange->{{0,14},{0,5}},
    PlotStyle->Red
]

enter image description here

Update

You could also use StepFunction from my answer to (30055). Here is an example:

sf = StepFunction[Thread[{{0,2,5,9,14},Range[0,4]}], Right];

Plot[sf[t], {t, 0, 14}, PlotStyle->Directive[Thick, Red]]

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
3

Some other ways. Use Exclusions -> None in the first case to get joined steps.

Plot[Length@l*CDF[EmpiricalDistribution[l], x] - 1, {x, Min@l, Max@l}, PlotStyle -> Red]

Mathematica graphics

Plot[LengthWhile[l, # <= x &], {x, Min@l, Max@l}]
Plot[Count[l, y_ /; y <= x], {x, Min@l, Max@l}]

Mathematica graphics

Less simple, more direct:

xx = l;
yy = Range[0, Length@l - 2];
Graphics[
 {Red, AbsoluteThickness[1.6], 
  Line@ Riffle[{Most@xx, yy}\[Transpose], {Rest@xx, yy}\[Transpose]]},
 Options@ListPlot]

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747
2

Here is another way to do it that I came up with

l = {0, 2, 5, 9, 14};
v[x_] := Sum[HeavisideTheta[x - l[[i]]], {i, 1, Length[l]}];
Plot[v[x], {x, 0, Last[l]}, Exclusions -> None, Frame -> True, 
 PlotStyle -> {Directive[Red, Thick]}]
Turbotanten
  • 691
  • 5
  • 18
2

One always has the option of constructing a Piecewise[] function directly:

l = {0, 2, 5, 9, 14};

f[x_] = Piecewise[Transpose[{Range[0, Length[l] - 2],
                             #1 < x <= #2 & @@@ Partition[l, 2, 1]}], Indeterminate]

(This version is right-continuous, but it should not be too hard to modify if you prefer a left-continuous version.)

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574