2
$\begingroup$

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

Expected result

$\endgroup$
  • $\begingroup$ Can you show your attempt at IntervalMarkers? $\endgroup$ – Lukas Lang Jan 14 at 9:33
  • 1
    $\begingroup$ 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]] $\endgroup$ – acoustics Jan 14 at 9:47
  • $\begingroup$ 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. $\endgroup$ – Lukas Lang Jan 14 at 11:49
  • $\begingroup$ I tried your suggestion, but not working $\endgroup$ – acoustics Jan 14 at 12:04
2
$\begingroup$

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"]

plot

$\endgroup$
3
$\begingroup$

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]]

enter image description here

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – m_goldberg Jan 14 at 13:47
  • $\begingroup$ @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. $\endgroup$ – Lukas Lang Jan 14 at 14:17
  • $\begingroup$ OK, I now see you picked up the OP's code from a comment. I have upvoted. $\endgroup$ – m_goldberg Jan 14 at 14:21
3
$\begingroup$

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*)
]

enter image description here

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}}]

enter image description here

$\endgroup$
  • $\begingroup$ What is the percentage of error? Where you mentioned it? $\endgroup$ – acoustics Jan 14 at 12:26
  • 1
    $\begingroup$ 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. $\endgroup$ – Sumit Jan 14 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.