0
$\begingroup$

I have a list of data values, and I'd like to plot error bars showing the maxima and minima that each entry in my data list can take. The data and the maxima and minima are produced outside of Mathematica and stored as lists.

How can I plot the data as points, with the max/min values for each point shown as error bars?

From reading other Stack Exchange posts, I know that the Mathematica documentation for things like error bars and ErrorListPlot is woefully lacking. What would be the easiest way of plotting them as I need?

Thanks a lot! :)

$\endgroup$
  • 1
    $\begingroup$ Do you have any data that can be used by answerers as a demonstration? $\endgroup$ – J. M. is away Feb 22 '16 at 17:24
3
$\begingroup$

Suppose that you have your data in the following format: {xcoord, ycoord, error}. You can then transform it into a format usable with ErrorListPlot from the ErrorBars` built-in package.

Needs["ErrorBarPlots`"]

Let's generate some noisy data and errors:

data = Table[{x, 2 x + 3 + RandomReal[{-0.5, 0.5}], RandomReal[{0.3, 0.5}]}, {x, -2, 2, 0.2}]

{{-2., -0.631418, 0.401375}, {-1.8, -0.324501, 0.478881}, {-1.6, -0.38702, 0.417729}, {-1.4, 0.465309, 0.469964}, {-1.2, 0.349653, 0.400248}, {-1., 1.29763, 0.411828}, {-0.8, 0.98644, 0.30355}, {-0.6, 1.99419, 0.350669}, {-0.4, 1.83254, 0.305295}, {-0.2, 2.5138, 0.354081}, {0., 3.31652, 0.391467}, {0.2, 3.5273, 0.45806}, {0.4, 3.56931, 0.30968}, {0.6, 4.48919, 0.356931}, {0.8, 4.33623, 0.426343}, {1., 5.3088, 0.386963}, {1.2, 4.94164, 0.356271}, {1.4, 5.30908, 0.449249}, {1.6, 6.29378, 0.464905}, {1.8, 6.23364, 0.308909}, {2., 7.00361, 0.328092}}

ErrorListPlot wants data formatted as {{xcoord, ycoord}, ErrorBar[value]}. We can transform the data we have to fit that format:

formattedData = {{#1, #2}, ErrorBar[#3]} & @@@ data

{ {{-2., -0.631418}, ErrorBar[0.401375]}, 
  {{-1.8, -0.324501}, ErrorBar[0.478881]}, <<...>>} }

Then we use ErrorListPlot on the transformed data set:

ErrorListPlot[formattedData]

Mathematica graphics

$\endgroup$

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.