# How do I add an error band to a plot?

I have two plots, one generated with Plot and the other generated by ListLinePlot. Both plots look the same. I am trying to plot the error band of 5% around Sin[[π x]. How to do this?

I have tried IntervalMarkers, but that did not work.

Plot[Sin[π x], {x, 0, 1}]
x = Table[i, {i, 0, 1, 0.01}];
y = Table[Sin[π*x[[i]]], {i, 1, Length[x]}];
data = Transpose[{x, y}];
ListLinePlot[data]


I am trying to get something like this

• Can you show your attempt at IntervalMarkers? – Lukas Lang Jan 14 at 9:33
• yy = Table[ Quantity[Around[y[[i]] \[PlusMinus] 0.05*y[[i]], x[[i]]], "USDollars"], {i, 1, Length[x]}]; ListLinePlot[prices, IntervalMarkers -> "Bands", InterpolationOrder -> 3, Sequence[PlotTheme -> "Scientific", PlotRange -> All]] – acoustics Jan 14 at 9:47
• Your syntax is off: The first argument of Around is the value, the second one should be the error. It doesn't understand ± for errors. Each data point should be {x, Around[y, yerr]}, so  yy = Table[{x[[i]],Quantity[Around[y[[i]],0.05*y[[i]]],"USDollars"]},{i,1,Length[x]}] does the trick. – Lukas Lang Jan 14 at 11:49
• I tried your suggestion, but not working – acoustics Jan 14 at 12:04

IntervalMarkers and Around can be used in a much simpler and more concise way to generate the same plot as Lucas Lang gets. Like so:

ListLinePlot[Table[{i, With[{y = Sin[π i]}, Around[y, .05 y]]}, {i, 0, 1, 0.01}],
IntervalMarkers -> "Bands",
PlotTheme -> "Scientific"]


Note: The code below is a fixed version of the attempt made by the OP (given in the comments of the question) without any additional changes. For a much cleaner solution, see @m_goldberg's answer instead.

You can use IntervalMarkers and Around like this:

x = Table[i, {i, 0, 1, 0.01}];
y = Table[Sin[π*x[[i]]], {i, 1, Length[x]}];
yy = Table[
{x[[i]], Quantity[Around[y[[i]], 0.05*y[[i]]], "USDollars"]},
{i, 1, Length[x]}];
ListLinePlot[yy, IntervalMarkers -> "Bands", InterpolationOrder -> 3,
Sequence[PlotTheme -> "Scientific", PlotRange -> All]]


• Basically you have the right idea, but I don't understand why you introduced all the unneeded stuff into the code, like Quantity and Sequence and options like PlotRange and InterpolationOrder. Sorry, but. because of these things I can't upvote this answer – m_goldberg Jan 14 at 13:47
• @m_goldberg That's because I simply took the code OP gave in the comments of the question and made the minimum amount of changes to get it working (i.e. to demonstrate what was wrong with theirs, instead of showing completely different code) - but you are right, the code as is is not very nice. I have added a note saying as much to the answer. – Lukas Lang Jan 14 at 14:17
• OK, I now see you picked up the OP's code from a comment. I have upvoted. – m_goldberg Jan 14 at 14:21

One way would be to use Filling.

data = Table[{x, Sin[Pi x]}, {x, 0, 2, 0.01}];
ndat = Length[data];
err = Table[0.2, {i,ndat}];(*you can get it from a list also*)
ListLinePlot[{
Table[{data[[i, 1]], data[[i, 2]]}, {i, ndat}],
Table[{data[[i, 1]], data[[i, 2]] + err[[i]]/2}, {i, ndat}],
Table[{data[[i, 1]], data[[i, 2]] - err[[i]]/2}, {i, ndat}]},
PlotStyle -> {{Thick, Orange}, Orange, Orange},
Filling -> {2 -> {3}} (*from 2nd to third line*)
]


When error is a fraction of the y value

err = Table[(15/100)*data[[i, 2]], {i,ndat}];(*15%*)
ListLinePlot[{Table[{data[[i, 1]], data[[i, 2]]}, {i, ndat}],
Table[{data[[i, 1]], data[[i, 2]] + err[[i]]/2}, {i, ndat}],
Table[{data[[i, 1]], data[[i, 2]] - err[[i]]/2}, {i, ndat}]},
PlotStyle -> {{Thick, Orange}, Orange, Orange},
Filling -> {2 -> {3}}]


• What is the percentage of error? Where you mentioned it? – acoustics Jan 14 at 12:26
• I leave it to you ;). err is the table for the error. I choose uniform error 0.2. You can make it a percentage of the y value, say, 0.05*data[[n,2]] or a random number. – Sumit Jan 14 at 14:28