Matlab

How to Create a Line Plot with Error Bars in MATLAB Using errorbar()

The error bars are vertical or horizontal bars plotted in the line plots to represent errors or uncertainty in any data point of a data set. These error bars are used to visualize the variability of data and to communicate the reliability of the results. There are various properties to plot error bars such as width, size, and color of the bar, all this can be done using the built-in errorbar() function in MATLAB.

This guide will go through different syntaxes to create error bars in MATLAB explaining various examples.

How to Create a Line Plot with Error Bars in MATLAB?

In MATLAB, we can easily create error bars in line plots by using the errorbar() function. This function accepts some mandatory and optional input arguments to create line plots with error bars on each data set point.

Syntax
In MATLAB, you can use the errorbar() function in the following ways:

errorbar(y,err)
errorbar(x,y,err)
errorbar(x,y,neg,pos)
errorbar(___,ornt)
errorbar(___,LineSpec)

Here,

The function errorbar(y, err) generates a line plot having vertical error bars corresponding to each data point in the data set y. The variable err includes the length of the error bar above as well as below the data points. So, the total length of the error bar becomes double to the value of err.

The function errorbar(x,y, err) generates a line plot corresponding to x versus y having error bars on each data point.

The function errorbar(x,y,neg,pos) generates error bars on each data point corresponding to x versus y where the neg variable contains the error bar’s length below the data point while pos contains the error bar’s length above the data point respectively.

The function errorbar(___,ornt) is used for setting up the orientation of the error bar. The default value of “ornt” represents vertical however we can change it by specifying “horizontal” for horizontal error bars and “both” for both vertical as well as horizontal error bars. We can use the “ornt” option with any previous syntax.

The function errorbar(___, LineSpec) creates the error bars using any syntax specifying bar colors, bar width, and more.

Example 1: How to Create Line Plot with Vertical Error Bars Having Equal Length?

This MATLAB code generates vertical error bars having equal length err corresponding to the line plot using MATLAB’s errorbar(y,err) function.

y = 10:10:100;
err = 10*ones(size(y));
errorbar(y,err)

Example 2: How to Create a Line Plot with Vertical Error Bars Having Different Lengths?

The given example creates the vertical error bars corresponding to the line plots having different lengths using the errorbar(x,y,neg,pos) function in MATLAB.

x = 1:10;
y = 10:10:100;
neg = 10*ones(size(y));
pos = 5*ones(size(y));
errorbar(x,y,neg,pos)

Example 3: How to Create Line Plot with Horizontal Error Bars?

In this MATLAB code, we use the errorbar(x,y,ornt) function to generate horizontal error bars corresponding to each data point on the specified line plot.

x = 1:10;
y = 10:10:100;
err = 10*ones(size(y));
errorbar(x,y,err,"horizontal")

Example 4: How to Create a Line Plot with Vertical and Horizontal Error Bars Having Line Properties?

In this example, we use the errorbar(x,y,err,”both”, LineSpec) function to generate vertical and horizontal error bars having specified line properties.

x = 1:10;
y = 10:10:100;
err = 5*ones(size(y));
errorbar(x,y,err,"both", "MarkerEdgeColor","blue","MarkerFaceColor",[0.75 0.95 1])

Conclusion

In MATLAB, the errorbar() is a useful built-in MATLAB function that helps us to create error bars corresponding to each data point in the given data set. We can use error bars for graphically representing the data point errors. This guide has demonstrated the working of the errorbar() function in MATLAB by providing its syntaxes and some basic examples to help us understand the workings of this function.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.