3
$\begingroup$

This is the sets of data I want to put into a form which will allow me to plot error bars from the min and max data.

avg = {{10, 0.662}, {20, 0.96}, {30, 0.916}, {40, 0.787}, {50,0.621}, {60, 0.453}}
min = {{0.55`}, {0.91`}, {0.85`}, {0.72`}, {0.58`}, {0.42`}}
max = {{0.76`}, {1.02`}, {0.99`}, {0.85`}, {0.68`}, {0.51`}}

This following code worked with n=1, creating one point on the graph. How do I make it loop to n=Length[avg] to plot all the points?

errmin = min[[n]] - avg[[n, 2]]
errmax = max[[n]] - avg[[n, 2]]
err = Flatten[{errmin, errmax}]
data = {{avg[[n]], ErrorBar[err]}}

ylabel = "Voltage (V \[PlusMinus] 0.05)"
xlabel = "Blade Angle (\[Degree] \[PlusMinus] 3)"
title = "Preliminary - Altering Blade Angle"
Needs["ErrorBarPlots`"]
ErrorListPlot[data, Joined -> True, InterpolationOrder -> 2, 
 Mesh -> Full, PlotMarkers -> Automatic, 
 AxesLabel -> {xlabel, ylabel}, GridLines -> Automatic, 
 GridLinesStyle -> Dotted, PlotLabel -> Style[title, Bold], 
 LabelStyle -> (FontFamily -> "Helvetica")]

I should point out I'm new to Mathematica today so please don't be too technical.

$\endgroup$

2 Answers 2

6
$\begingroup$

What you are looking for is MapThread. Specifically, I would generate your data using this code

data = MapThread[
        {#1, ErrorBar[{#2[[1]] - #1[[2]], #3[[1]] - #1[[2]]}]} &, 
        {avg, min, max}
]

which when input into your ErrorListPlot you get

enter image description here

Note, I added the option PlotRange -> All as it was getting cut off.


Now to explain the code. MapThread pulls a single element out of each list passed to it (the second parameter), and applies a function to them (the first parameter). In this case, the function is

{#1, ErrorBar[{#2[[1]] - #1[[2]], #3[[1]] - #1[[2]]}]} &

Here the #n terms (called Slots) refer to the parameters passed by number, so #1 is from avg, etc. The & at the end tells Mathematica that it is a pure function.

$\endgroup$
6
  • $\begingroup$ Ah that's brilliant, thank you! $\endgroup$
    – Theo
    Oct 27, 2012 at 19:28
  • $\begingroup$ @TheoWindebank you're welcome. Don't forget to vote for good answers, and to accept the one that is the most helpful. $\endgroup$
    – rcollyer
    Oct 27, 2012 at 19:30
  • $\begingroup$ @TheoWindebank you should also look closely at s0rce's answer as it talks a bit about vector operations which will simplify your life. $\endgroup$
    – rcollyer
    Oct 27, 2012 at 19:34
  • $\begingroup$ Say now I have multiple sets of data, can I use a similar method to make multiple graphs without just copying the code over and over? $\endgroup$
    – Theo
    Oct 27, 2012 at 20:10
  • $\begingroup$ @TheoWindebank I'm not sure what you mean. You can turn it into a function, or I'd do this: ErrorListPlot[MapThread[..., #], ...]& /@ {{avg, min, max}, ...} where the ellipses are filled in as appropriate. $\endgroup$
    – rcollyer
    Oct 27, 2012 at 20:43
5
$\begingroup$
avg = {{10, 0.662}, {20, 0.96}, {30, 0.916}, {40, 0.787}, {50, 
   0.621}, {60, 0.453}}
min = {{0.55`}, {0.91`}, {0.85`}, {0.72`}, {0.58`}, {0.42`}}
max = {{0.76`}, {1.02`}, {0.99`}, {0.85`}, {0.68`}, {0.51`}}

You can perform subtraction on the whole list

errmin = min[[All, 1]] - avg[[All, 2]];
errmax = max[[All, 1]] - avg[[All, 2]];

combine the errmin and errmax

error = Transpose[{errmin, errmax}];

combine data and error

dataerror = MapThread[{#1, ErrorBar[#2]} &, {avg, error}];

now plot it all

ErrorListPlot[dataerror, 
 Joined -> True, InterpolationOrder -> 2, Mesh -> Full, 
 PlotMarkers -> Automatic, AxesLabel -> {xlabel, ylabel}, 
 GridLines -> Automatic, GridLinesStyle -> Dotted, 
 PlotLabel -> Style[title, Bold], 
 LabelStyle -> (FontFamily -> "Helvetica")]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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