6

I often have the following notebook in Mathematica, which I use to make a dataset and associated errors:

x = {0, 2, 4, 6, 10, 20};
y = {979, 146, 141, 157, 187, 274};
uncY = Sqrt[y];
dataVector = Thread[{x, y}];

I want to make an ErrorListPlot, so I need to add ErrorBar with each uncY-value as an element in each data pair. So the final dataVector should have the form

{{x_i, y_i}, Errorbar[uncY_i]}

What is an easy way to achieve this, without using a For-loop?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
BillyJean
  • 1,273
  • 7
  • 25

3 Answers3

9

If your errors input has the form :

errorList = {1, 2, 1, 3, 1, 2}

Then do :

Thread[{dataVector, Errorbar /@ errorList}]  

(* {  
 {{0, 979}, Errorbar[1]},
 {{2, 146}, Errorbar[2]},
 {{4, 141}, Errorbar[1]},
 {{6, 157}, Errorbar[3]},
 {{10, 187}, Errorbar[1]},
 {{20, 274}, Errorbar[2]}
 } *)
andre314
  • 18,474
  • 1
  • 36
  • 69
5

You can do all the manipulation in one line if you wish:

ErrorListPlot@MapThread[{{#1, #2}, ErrorBar[#2-uncY@#2 ]} &, {x, y}]
image_doctor
  • 10,234
  • 23
  • 40
4

I accidentally discovered that you can use convenient notation PlusMinus(±) for horizontal and vertical axes

ypm = Thread[y ± uncY]

enter image description here

ErrorListPlot[Thread[{x, ypm}]]

enter image description here

In your case you can use even more simple method as mentioned here:

ErrorListPlot[Thread[{x, y, uncY}]] 

Unfortunately, these nice options are still undocumented.

ybeltukov
  • 43,673
  • 5
  • 108
  • 212