Matplotlib’s errorbar() function helps visually represent data variability. People commonly use it to plot scientific data or experimental results, making it easy to display the margin of error for each data point.
In other words, error bars indicate measurement uncertainty or show confidence intervals for the data.
기본 사용방법
plt.errorbar(x, y, yerr=y_err, fmt='o', capsize=5, label="Data with Error Bars")
- x, y: Data coordinates
- xerr, yerr: Error values in the X, Y axis directions (can be set differently for each point)
- Type of error
- Single value: The same error is applied to all data points.
- Array: You can specify an individual error for each data point.
- Tuple: You can specify a separate lower and upper error for each data point. For example, yerr=(y_lower, y_upper).
- Type of error
- fmt=’o’: Marker style (use circular markers)
- capsize=5: Set the size of the caps at the end of the error bars
- label=“Data with Error Bars”: Add a legend
Options
- ecolor=’red’. : set the error bar color to red
- elinewidth=1.5 : Set the line thickness of the error bar
- capthick=2 : Set the thickness of the caps (bar ends)
- alpha=0.7 : Set transparency of the graph
Errorbar Code
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(10)
x = np.linspace(0, 10, 10)
y = np.sin(x)
yerr = np.random.uniform(0.1, 0.3, size=y.shape)
plt.errorbar(x, y, yerr=yerr, fmt='o', capsize=5, capthick=2)
"""
yerr: Error value of the y-axis.
fmt='o': Show data points as circles.
capsize=5: Size of the cap on the error bar.
capthick=2: Thickness of the cap of the error bar.
"""
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Error Bar Plot Example')
plt.grid(True) # Add grid for better visualization
plt.show()

Summary
- plt.errorbar() allows you to add an error bar in the X/Y axis direction.
- Set error values via yerr and xerr.
- Adjust the style using options like
ecolor
,elinewidth
, andcapsize
. - Set asymmetric error using
yerr=[lower limit, upper limit]
.
Error bars are a powerful tool for visually representing uncertainty in data. Matplotlib’s errorbar() function makes it easy to add various types of error bars, helping you clearly show confidence intervals or measurement errors in your data.