Matplotlib - Errorbar



An error bar is a graphical representation that shows the amount of uncertainty or variation in a set of data. It consists of line(s) that extend from a data point on a graph to indicate the range within which the true value is probably located.

Imagine you are measuring the height of a plant every week for a month. Each time you measure, there might be a little bit of variation due to factors like changes in weather. The error bar on your graph will show this range of possible values for each height measurement, giving you an idea of how confident you can be in the accuracy of your data −

Errorbar

Errorbar in Matplotlib

We can create an errorbar in Matplotlib using the errorbar() function. It allows you to represent uncertainty in both the x and y directions, making it useful to depict error bars in various types of plots, such as scatter plots, line plots, or bar plots.

The errorbar() Function

The errorbar() function in Matplotlib takes x and y coordinates, indicating the uncertainty in data points. This function is particularly useful for visualizing the range of possible values around each data point, providing a better understanding of how certain or uncertain the measurements are.

Following is the syntax of the errorbar() function in Matplotlib −

matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt='', barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, *, data=None, **kwargs)

Where,

  • x is the x-coordinates of the data points.
  • y is the y-coordinates of the data points.
  • yerr is the error in the y-direction. It can be a scalar or an array.
  • xerr is the error in the x-direction. It can be a scalar or an array.
  • fmt is the format code controlling the appearance of lines and markers.
  • If barsabove is True, error bars are drawn above data points.
  • lolims, uplims, xlolims, xuplims are the Booleans indicating whether the errors represent lower/upper limits or if x-limits are specified.
  • errorevery subsample the error bars by an integer factor.
  • capthick is the thickness of the caps on the error bars.

Basic Errorbar Plot

A basic errorbar plot in Matplotlib is a visual representation of data points along with their associated uncertainties (errors). It is formed using the errorbar() function, which adds vertical or horizontal error bars to each data point.

Example

In the following example, we are creating a simple error bar plot. We define 'x' and 'y' data points along with corresponding 'y-error' values. We then use the errorbar() function to plot the data with error bars −

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
y_error = [0.5, 0.3, 0.4, 0.2, 0.6]

# Creating a basic errorbar plot
plt.errorbar(x, y, yerr=y_error, fmt='o', label='Data with Errorbars')

# Customizing the plot
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Errorbar Plot')
plt.legend()

# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Basic Errorbar Plot

Horizontal Errorbars

A horizontal errorbars plot in Matplotlib is a visual representation of data points with associated uncertainties in the horizontal direction. It is created using the errorbar() function, where error bars are drawn horizontally to show the variability or intervals in the x-direction.

Example

In here, we are creating a plot with horizontal error bars. We first define y and x data points along with corresponding x-error values. We then use the errorbar() function to create the plot with horizontal error bars −

import matplotlib.pyplot as plt

# Data
y = [1, 2, 3, 4, 5]
x = [2, 4, 3, 5, 6]
x_error = [0.2, 0.3, 0.4, 0.1, 0.5]

# Creating horizontal errorbars
plt.errorbar(x, y, xerr=x_error, fmt='o', label='Data with Horizontal Errorbars')

# Customizing the plot
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Horizontal Errorbar Plot')
plt.legend()

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Horizontal Errorbars

Asymmetric Error Bars

In Matplotlib, asymmetric error bars allow you to represent varying levels of uncertainty or variation in both the positive and negative directions around each data point. This is useful when the errors in your measurements are not symmetrical.

You can customize the errorbar() function to handle asymmetric error bars by providing a list of two arrays to the "yerr" or "xerr" parameter, where the first array corresponds to the negative errors, and the second array corresponds to the positive errors.

Example

Now, we are creating asymmetric error bars. The "yerr" parameter takes a list of two arrays, representing negative and positive errors separately. This allows for different error magnitudes in each direction −

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
y_error_neg = [0.5, 0.3, 0.4, 0.2, 0.6]
y_error_pos = [0.2, 0.1, 0.3, 0.4, 0.5]

plt.errorbar(x, y, yerr=[y_error_neg, y_error_pos], fmt='o', label='Asymmetric Error Bars')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Asymmetric Error Bars')
plt.legend()
plt.show()

Output

Output of the above code is as follows −

Asymmetric Error Bars

Subsampled Errorbars

Subsampled error bars in Matplotlib is like selectively displaying only a subset of the error bars to improve the clarity and reduce visual noise in the plot. The errorbar() function provides the "errorevery" parameter for this purpose.

By setting "errorevery" to an integer value greater than 1, you can subsample the error bars to only show every Nth error bar, where N is the value of errorevery.

Example

In the given example, we are generating a scatter plot with error bars. We define x and y data points, along with "y_error" values for error bars. The error bars are plotted using a purple 'o' marker and are displayed every 2 data points, thus reducing noise −

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
y_error = [0.5, 0.3, 0.4, 0.2, 0.6]

# Creating errorbars with subsampling
plt.errorbar(x, y, yerr=y_error, fmt='o', color='purple', errorevery=2, label='Subsampled Errorbars')

# Customizing the plot
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Subsampled Errorbars Plot')
plt.legend()

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Subsampled Errorbars
Advertisements