# Plotting a stepwise function from a series of points [closed]

I try to plot a step-wise function from a series of points, and I have the commands:

pacificpoints = {{-5, 2}, {-4.8, 6}, {-4.5, 3}, {-4.2, 14.5}, {-4,
5}, {-3.8, 8}, {-3.5, 5}, {-3.2, 12}, {-3.1, 10.5}, {-3,
13}, {-2.2, 2}, {-1.9, 11}, {-1.7, 5}, {-1.3, 9.8}, {-0.8, 2}, {0,
25.5}, {0.4, 1}, {0.6, 9}, {1.2, 6}, {1.5, 7.5}, {1.9,
1.8}, {2.2, 7}, {2.5, 4}, {3, 13}, {3.5, 5.5}, {3.8, 10}, {4.2,
4}, {4.5, 8}, {4.8, 5.5}, {5, 1}};
Remove[pacificfun]


The step-wise function is then prepared as:

pacificfun[x_] :=
Piecewise[
Table[{pacificpoints[[i, 2]], pacific[[i, 1]] > x}, {i,
Length[pacificpoints]}], 0];
Plot[pacificpoints[x], {x, -5, 5}, PlotRange -> Full]


But I get only a blank plot, while it works on another quite similar point-set:

alwynpoints = {{-5, 0}, {-4.5, 1.5}, {-4, 0}, {-3.8, -2}, {-3.2,
0}, {-3, 2}, {-2.8, -1.7}, {-2.0, 0}, {-1.8, 2}, {-1.6, 0}, {-1.2,
3.5}, {-1, -3.8}, {-0.5, 0}, {0, 14}, {0.8, -5}, {1, 0}, {1.5,
5}, {1.8, 0}, {2, -5}, {2.2, 0}, {2.5, 4.5}, {2.8,
0}, {3, -3}, {3.5, 0}, {4, 2}, {4.2, 0}, {4.5, -2}, {5, 0}};


I have checked the pacific data set, but nothing is wrong with it.

Any ideas how to plot this as piecewise?

Thanks

• You must plot "pacificfun[x]" and not "pacificpoints[x]" Nov 30, 2022 at 12:55
• Right, but that gives a really weird plot. It does not correspond to the points Nov 30, 2022 at 12:56
• IM, thanks, but I need a real piecewise function, and it works for the given command on the alwynpoints. Nov 30, 2022 at 13:03
• The definition of pacificfun contains the undefined array pacific. Replacing pacific by pacificpoints gives a reasonable plot. Nov 30, 2022 at 15:15

After replacing "pacific" by "pacificpoints" :

pacificfun[x_] =  Piecewise[
Table[{pacificpoints[[i, 2]], pacificpoints[[i, 1]] > x}, {i,Length[pacificpoints]}], 0];


we can plot the data:

Plot[pacificfun[x], {x, -5, 5}, PlotRange -> Full]


To check the first few data:

pacificfun /@ {-4.9, -4.6, -4.3, -4.1, -3.9}

(* {6, 3, 14.5, 5, 8} *)


This seems to correspnd to the picture.

• Thank you! .... Nov 30, 2022 at 13:27

Try the built-in ListStepPlot function if the Piecewise is not essential for further computation.

ListStepPlot[pacificpoints
, Left
, Joined -> False
]


If the function is indispensable:

pacificfun[x_] =
Piecewise@(({Last@#, First@# > x} & /@ pacificpoints)~
Join~{{0, True}});

Plot[pacificfun[x], {x, Splice[MinMax@pacificpoints[[All, 1]]]}]

• THanks Syed, but the Piecewise is absolutely essential Nov 30, 2022 at 14:05

The solution is

pacificfun[x_] :=
Piecewise[
Table[{pacificpoints[[i, 2]], pacificpoints[[i, 1]] > x}, {i,
Length[pacificpoints]}], 0];
Plot[pacificfun[x], {x, -5, 5}, PlotRange -> Full]