xxxxxxxxxx
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Errorbar with bar plot when used with 1 variable
ax.bar("Label 1", df["col1"].mean(), yerr=df["col1"].std())
ax.bar("Label 2", df["col2"].mean(), yerr=df["col2"].std())
ax.set_ylabel("Y axis label")
plt.show()
# Errorbar with line plot when used with 2 variables
ax.errorbar(df["col1"], df["col2"], yerr=df["col3"])
ax.errorbar(df["col1"], df["col2"], yerr=df["col3"])
ax.set_ylabel("Y axis label")
plt.show()
xxxxxxxxxx
x = [0,1,2,3,4,5]
y = [3,2,4,5,1,5]
yerr=[0.3,0.2,0.4,0.5,0.1,0.5]
fig, ax = plt.subplots(figsize=(8,8))
ax.errorbar(x, y, yerr=yerr, fmt='-o', color='k')
ax.set_title('My title')
ax.set_ylabel('My ylabel')
ax.set_xlabel('My xlabel')
plt.show()
xxxxxxxxxx
#Plot symmetric error bar in both dimension
#if only one dimension needed, omit xerr or yerr
plt.errorbar(x, y, xerr=xerr, yerr=yerr)
xxxxxxxxxx
import matplotlib.pyplot as plt
import numpy as np
# Sample data
categories = ['Category 1', 'Category 2', 'Category 3']
values = [10, 15, 12]
errors = [1, 2, 1]
# Plotting the bar plot with error bars
plt.bar(categories, values, yerr=errors, capsize=5, color='skyblue', alpha=0.7)
# Adding labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot with Error Bars')
# Show the plot
plt.show()