EXPERIMENT No. 9
Python Lab Practical Record · Python programming laboratory
Read with diagrams, search and self-test
9A. Sample average and range, X-bar and R charts
Aim
To develop a Python program to calculate the sample average (XQ ) and sample range (R) for the given data and plot the X-bar and R charts using matplotlib.
Program (xbar_r_chart.py)
# Exp 9A: Sample average (X-bar) and range (R), and X-bar and R charts import matplotlib.pyplot as plt
samples = [
[10, 11, 12, 11],
[11, 12, 13, 12],
[12, 13, 14, 13],
[11, 12, 12, 13],
[10, 11, 12, 12]
]
xbar = []
R = []
sample_no = [1, 2, 3, 4, 5]for sample in samples:
average = sum(sample) / len(sample)
range_value = max(sample) - min(sample)
xbar.append(average)
R.append(range_value)
print("Sample =", sample)
print("Average =", average)
print("Range =", range_value)
print()plt.plot(sample_no, xbar, marker='o')
plt.title("X-bar Chart")
plt.xlabel("Sample number")
plt.ylabel("Average")
plt.show()plt.plot(sample_no, R, marker='o')
plt.title("R Chart")
plt.xlabel("Sample number")
plt.ylabel("Range")
plt.show()Output
Sample = [10, 11, 12, 11] Average = 11.0 Range = 2
Sample = [11, 12, 13, 12] Average = 12.0 Range = 2
Sample = [12, 13, 14, 13] Average = 13.0 Range = 2
Sample = [11, 12, 12, 13] Average = 12.0 Range = 2
Sample = [10, 11, 12, 12] Average = 11.25 Range = 2
X-bar chart
R chart
Result
The sample averages (XQ ) and ranges (R) were calculated, and the X-bar and R charts were plotted using matplotlib.