[Universal Code + Case] Detailed explanation of SPC related control chart principles and logic codes

  • Xbar-R chart: used to monitor sample mean and range, suitable for small sample sizes.
  • Xbar-S chart: Similar to Xbar-R chart, but suitable for large sample sizes.
  • p chart: used to monitor the defect ratio within a certain time or sample size.
  • np chart: similar to p chart, but used for fixed sample size.
  • u chart: used to monitor the number of defects within the unit.
  • Figure c: Monitor the number of defects within a specific time or sample size.

Each type of control chart has its specific purpose and interpretation. For example, if a control chart shows that the data points are mostly within the control limits, this indicates that the process is stable. If data points fall outside control limits or exhibit non-random patterns, this may indicate an anomaly in the process.

Insert image description here
Insert image description here

Code and cases

I-MR photo

import matplotlib.pyplot as plt
import numpy as np

# 模拟数据
np.random.seed(0)
data = np.random.normal(loc=10, scale=1, size=30)

# 计算控制限
mean = np.mean(data)
std_dev = np.std(data)
upper_control_limit = mean + 3*std_dev
lower_control_limit = mean - 3*std_dev

# 绘制I-MR图
plt.figure(figsize=(10, 5))

# 绘制I图 (Individuals)
plt.subplot(1, 2, 1)
plt.plot(data, marker='o', linestyle='-')
plt.axhline(mean, color='green', linestyle='--')
plt.axhline(upper_control_limit, color='red', linestyle='--')
plt.axhline(lower_control_limit, color='red', linestyle='--')
plt.title('I图')
plt.xlabel('样本')
plt.ylabel('值')

# 绘制MR图 (Moving Range)
moving_ranges = [abs(data[i] - data[i-1]) for i in range(1, len(data))]
mean_mr = np.mean(moving_ranges)

plt.subplot(1, 2, 2)
plt.plot(moving_ranges, marker='o', linestyle='-')
plt.axhline(mean_mr, color='green', linestyle='--')
plt.title('MR图')
plt.xlabel('样本')
plt.ylabel('移动范围')

plt.tight_layout()
plt.show()

Insert image description here

  • Code explanation

    Data simulation: 30 random data points obeying normal distribution were generated.
    Calculate control limits: Calculate the mean and standard deviation of the data, and determine the upper and lower control limits accordingly.
    Draw I chart (Individuals Chart): shows the changes of each data point, and draws the average line and control limits.
    Draw MR chart (Moving Range Chart): shows the changes (moving range) between adjacent data points, and draws its average line.

  • Chart interpretation

    I chart: If most data points are within the control limits, the process is considered stable. If there are consecutive points outside the control limits, or if there is a specific pattern (such as a continuous rise or fall), it may indicate an abnormality in the process.
    MR chart: mainly used to monitor the variability of the process. If the movement is within control limits and there are no non-random patterns, the process variability is stable.

Xbar-R plot

import pandas as pd

# 创建模拟数据(5个样本,每个样本包含5个数据点)
np.random.seed(0)
data = np.random.normal(loc=20, scale=2, size=(5, 5))

# 将数据转换为DataFrame,便于处理
df = pd.DataFrame(data)

# 计算样本均值和样本范围
sample_means = df.mean(axis=1)
sample_ranges = df.max(axis=1) - df.min(axis=1)

# 计算Xbar-R图的控制限
grand_mean = sample_means.mean()  # 总体均值
mean_range = sample_ranges.mean() # 平均范围

# R图的控制限
r_chart_upper = mean_range * 2.114  # D4因子
r_chart_lower = mean_range * 0      # D3因子

# Xbar图的控制限
xbar_chart_upper = grand_mean + (r_chart_upper * 0.577) / np.sqrt(5) # A2因子
xbar_chart_lower = grand_mean - (r_chart_upper * 0.577) / np.sqrt(5)

# 绘制Xbar-R图
plt.figure(figsize=(10, 5))

# 绘制Xbar图
plt.subplot(1, 2, 1)
plt.plot(sample_means, marker='o', linestyle='-')
plt.axhline(grand_mean, color='green', linestyle='--')
plt.axhline(xbar_chart_upper, color='red', linestyle='--')
plt.axhline(xbar_chart_lower, color='red', linestyle='--')
plt.title('Xbar图')
plt.xlabel('样本')
plt.ylabel('均值')

# 绘制R图
plt.subplot(1, 2, 2)
plt.plot(sample_ranges, marker='o', linestyle='-')
plt.axhline(mean_range, color='green', linestyle='--')
plt.axhline(r_chart_upper, color='red', linestyle='--')
plt.axhline(r_chart_lower, color='red', linestyle='--')
plt.title('R图')
plt.xlabel('样本')
plt.ylabel('范围')

plt.tight_layout()
plt.show()

Insert image description here

  • Code explanation

    Data simulation: 5 groups of samples are created, each group contains 5 data points, and these data points obey the normal distribution.
    Calculate sample mean and range: The mean and range (difference between maximum and minimum values) are calculated for each sample.
    Calculate control limits:
    R chart (range chart): Calculate upper and lower control limits based on the average value of the sample range and the D4 and D3 factors.
    Xbar chart (mean chart): Calculate upper and lower control limits based on the population mean and A2 factor of the sample mean.
    Draw the Xbar-R chart:
    Xbar chart: shows the mean of each sample and marks the control limits.
    R chart: shows the range of each sample and marks the control limits.

  • Chart interpretation

    Xbar chart: Monitor changes in sample mean. If the mean is mostly within the control limits, it indicates that the process mean is stable. Exceeding control limits or exhibiting specific patterns indicates a possible problem.
    R chart: Monitor changes in sample range. The stability of the range indicates the level of control of process variability.

Xbar-S diagram

# 创建模拟数据(5个样本,每个样本包含10个数据点)
np.random.seed(0)
data = np.random.normal(loc=15, scale=3, size=(5, 10))

# 将数据转换为DataFrame
df = pd.DataFrame(data)

# 计算样本均值和标准差
sample_means = df.mean(axis=1)
sample_stds = df.std(axis=1, ddof=1)

# 计算Xbar-S图的控制限
grand_mean = sample_means.mean()  # 总体均值
mean_std = sample_stds.mean()     # 平均标准差

# S图的控制限
s_chart_upper = mean_std * 2.089  # B4因子
s_chart_lower = mean_std * 0.000  # B3因子

# Xbar图的控制限
xbar_chart_upper = grand_mean + (s_chart_upper / np.sqrt(10)) * 0.308  # A3因子
xbar_chart_lower = grand_mean - (s_chart_upper / np.sqrt(10)) * 0.308

# 绘制Xbar-S图
plt.figure(figsize=(10, 5))

# 绘制Xbar图
plt.subplot(1, 2, 1)
plt.plot(sample_means, marker='o', linestyle='-')
plt.axhline(grand_mean, color='green', linestyle='--')
plt.axhline(xbar_chart_upper, color='red', linestyle='--')
plt.axhline(xbar_chart_lower, color='red', linestyle='--')
plt.title('Xbar图')
plt.xlabel('样本')
plt.ylabel('均值')

# 绘制S图
plt.subplot(1, 2, 2)
plt.plot(sample_stds, marker='o', linestyle='-')
plt.axhline(mean_std, color='green', linestyle='--')
plt.axhline(s_chart_upper, color='red', linestyle='--')
plt.axhline(s_chart_lower, color='red', linestyle='--')
plt.title('S图')
plt.xlabel('样本')
plt.ylabel('标准差')

plt.tight_layout()
plt.show()

  • Code explanation

    Data simulation: 5 groups of samples are created, each group contains 10 data points, and these data points obey the normal distribution.
    Calculate the sample mean and standard deviation: The mean and standard deviation are calculated for each sample.
    Calculate control limits:
    S chart (standard deviation chart): Calculate upper and lower control limits based on the average of the sample standard deviation and the B4 and B3 factors.
    Xbar chart (mean chart): Calculate upper and lower control limits based on the population mean and A3 factor of the sample mean.
    Draw the Xbar-S chart:
    Xbar chart: shows the mean value of each sample and marks the control limits.
    S chart: shows the standard deviation of each sample and marks the control limits.

  • Chart interpretation

    Xbar chart: Monitor changes in sample mean. The stability of the mean indicates that the central tendency of the process is in control.
    S chart: Monitor changes in sample standard deviation. The stability of the standard deviation indicates the level of control of process variability.

Insert image description here

P picture

# 模拟数据 - p图(缺陷比率图)
np.random.seed(0)
# 假设有20个样本,每个样本包含100个单元
n_samples = 20
sample_size = 100
# 模拟数据:随机生成每个样本中的缺陷数(0到10之间)
defective_units = np.random.randint(0, 11, size=n_samples)

# 计算缺陷比率
p = defective_units / sample_size

# 计算p图的控制限
p_bar = np.mean(p)  # 平均缺陷比率
p_chart_upper = p_bar + 3 * np.sqrt(p_bar * (1 - p_bar) / sample_size)
p_chart_lower = p_bar - 3 * np.sqrt(p_bar * (1 - p_bar) / sample_size)
p_chart_lower = np.clip(p_chart_lower, 0, 1)  # 确保下限不小于0

# 绘制p图
plt.figure(figsize=(10, 5))
plt.plot(p, marker='o', linestyle='-')
plt.axhline(p_bar, color='green', linestyle='--')
plt.axhline(p_chart_upper, color='red', linestyle='--')
plt.axhline(p_chart_lower, color='red', linestyle='--')
plt.title('p图')
plt.xlabel('样本')
plt.ylabel('缺陷比率')
plt.ylim(0, 0.15)
plt.show()

Insert image description here

  • Code explanation

    Data simulation: 20 sample data were generated, each sample contained 100 units, and the number of defects in each sample was randomly generated (between 0 and 10).
    Calculate defect ratio: The defect ratio was calculated for each sample.
    Calculate control limits: Calculate upper and lower control limits based on the average defect ratio, ensuring that the lower limit is not less than 0.
    Draw a p chart: It shows the defect ratio of each sample, and marks the average defect ratio and control limits.

  • Chart interpretation

    p chart: used to monitor changes in defect ratios in different samples. If the majority of data points are within the control limits, the defect rate is stable. Data points that exceed control limits or exhibit specific patterns may indicate changes in process quality.

np control chart

# 模拟数据 - np图(固定样本量的缺陷数量图)
np.random.seed(0)
# 假设有20个样本,每个样本包含固定的50个单元
n_samples = 20
sample_size = 50
# 模拟数据:随机生成每个样本中的缺陷数(0到10之间)
defective_units = np.random.randint(0, 11, size=n_samples)

# 计算缺陷数量
np_values = defective_units

# 计算np图的控制限
np_bar = np.mean(np_values)  # 平均缺陷数量
np_chart_upper = np_bar + 3 * np.sqrt(np_bar * (1 - np_bar / sample_size))
np_chart_lower = np_bar - 3 * np.sqrt(np_bar * (1 - np_bar / sample_size))
np_chart_lower = np.clip(np_chart_lower, 0, np.inf)  # 确保下限不小于0

# 绘制np图
plt.figure(figsize=(10, 5))
plt.plot(np_values, marker='o', linestyle='-')
plt.axhline(np_bar, color='green', linestyle='--')
plt.axhline(np_chart_upper, color='red', linestyle='--')
plt.axhline(np_chart_lower, color='red', linestyle='--')
plt.title('np图')
plt.xlabel('样本')
plt.ylabel('缺陷数量')
plt.ylim(0, max(np_chart_upper, max(np_values)) + 1)
plt.show()

Insert image description here

  • Code explanation

    Data simulation: 20 sample data were generated, each sample contained a fixed 50 units, and the number of defects in each sample was randomly generated (between 0 and 10).
    Count the number of defects: Record the number of defects for each sample.
    Calculate control limits: Calculate upper and lower control limits based on the average number of defects, ensuring that the lower limit is not less than 0.
    Draw an np chart: It shows the number of defects for each sample, and marks the average number of defects and control limits.

  • Chart interpretation

    np chart: used to monitor the number of defects for a fixed number of units in different samples. If the majority of the data points are within the control limits, it indicates that the number of defects is stable. Data points that exceed control limits or exhibit specific patterns may indicate changes in process quality.

U picture

# 模拟数据 - u图(单位内的缺陷数图)
np.random.seed(0)
# 假设有20个样本,每个样本的单元数量不固定
sample_sizes = np.random.randint(40, 61, size=n_samples)  # 单元数在40到60之间
# 模拟数据:随机生成每个样本中的缺陷数
defective_units = np.random.randint(0, 11, size=n_samples)

# 计算单位内的缺陷数
u_values = defective_units / sample_sizes

# u图的控制限计算和绘图
u_chart_upper = [u_bar + 3 * np.sqrt(u_bar / n) for n in sample_sizes]
u_chart_lower = [u_bar - 3 * np.sqrt(u_bar / n) for n in sample_sizes]
u_chart_lower = np.clip(u_chart_lower, 0, np.inf)  # 确保下限不小于0

# 绘制u图
plt.figure(figsize=(10, 5))
plt.plot(u_values, marker='o', linestyle='-')
plt.axhline(u_bar, color='green', linestyle='--')

# 由于控制限是变量,需要逐点绘制
for i in range(n_samples):
    plt.plot([i, i], [u_chart_lower[i], u_chart_upper[i]], color='red', linestyle='--')

plt.title('u图')
plt.xlabel('样本')
plt.ylabel('单位内缺陷数')
plt.ylim(0, max(u_chart_upper) + 0.01)
plt.show()


Insert image description here

  • Code explanation

    Data simulation: 20 sample data were generated, each sample contained a variable number of units (between 40 and 60), and the number of defects in each sample was randomly generated.
    Calculate the number of defects per unit: The number of defects per unit is calculated for each sample.
    Calculate control limits: Calculate upper and lower control limits based on the average number of defects per unit, taking into account variations in sample size.
    Draw a u chart: It shows the number of defects per unit for each sample, and marks the average number of defects per unit and control limits.

  • Chart interpretation

    u chart: used to monitor the number of defects within a unit in different samples. If the majority of data points are within the control limits, it indicates that the number of defects per unit is stable. Data points that exceed control limits or exhibit specific patterns may indicate changes in process quality.

C picture

# 模拟数据 - c图(特定时间或样本量内的缺陷数图)
np.random.seed(0)
# 假设有20个样本
# 模拟数据:随机生成每个样本中的缺陷数
defective_units = np.random.poisson(lam=5, size=n_samples)  # 使用泊松分布模拟缺陷数

# 计算c图的控制限
c_bar = np.mean(defective_units)  # 平均缺陷数量
c_chart_upper = c_bar + 3 * np.sqrt(c_bar)
c_chart_lower = c_bar - 3 * np.sqrt(c_bar)
c_chart_lower = np.clip(c_chart_lower, 0, np.inf)  # 确保下限不小于0

plt.figure(figsize=(10, 5))
plt.plot(defective_units, marker='o', linestyle='-')
plt.axhline(c_bar, color='green', linestyle='--')
plt.axhline(c_chart_upper, color='red', linestyle='--')
plt.axhline(c_chart_lower, color='red', linestyle='--')
plt.title('c图')
plt.xlabel('样本')
plt.ylabel('缺陷数量')
plt.ylim(0, max(c_chart_upper, max(defective_units)) + 1)
plt.show()

Insert image description here

  • Code explanation

    Data simulation: 20 samples of data were generated, and the number of defects in each sample was randomly generated using Poisson distribution.
    Calculate the control limits of chart c: Calculate the upper and lower control limits based on the average number of defects.
    Draw chart c: shows the number of defects for each sample, and marks the average number of defects and control limits.

  • Chart interpretation

    Figure c: Used to monitor the number of defects within a specific time or sample size. If the majority of the data points are within the control limits, it indicates that the number of defects is stable. Data points that exceed control limits or exhibit specific patterns may indicate changes in process quality.

How to determine segment abnormality from SPC control chart

When we use SPC for process control, drawing is the foundation, and finding problems is the purpose. The SPC judgment principle will be involved. Through the changes in the points in the SPC control chart, we can judge whether there are problems in the production to monitor whether there are problems in the production process. in a state of control.

Simply put, when the following 8 point distributions appear in the control chart, we think that there are special reasons for the process. We must find out the special reasons for the abnormal point distribution and eliminate them, so as to curb the occurrence of defective products in advance. , to ensure product quality.

In order to make it easier to understand the eight discrimination principles, we use a diagram to describe them. First, let’s take a look at the diagram description: the upper and lower A/B/C six intervals represent 3 times, 2 times, and 1 times the standard deviation intervals of the sample value respectively.

Insert image description here

Judgment of changes in the midpoint of SPC control chart

Judgment criterion 1: Any point falls outside 3 standard deviations

Possible reasons: generally considered to be new employees, process method errors, machine failures, unqualified raw materials, measurement errors, calculation errors, changes in inspection methods or standards.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 2: 9 consecutive points fall on the same side of the center line

Possible reasons: generally considered to be new employees, process method errors, machine failures, unqualified raw materials, measurement errors, calculation errors, changes in inspection methods or standards.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 3: 6 consecutive points increasing or decreasing, that is, a series

Possible reasons: wear and tear of molds and other tools, reduced maintenance levels, and operators becoming more and more skilled.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 4: 14 consecutive adjacent points alternately up and down

Possible reasons: Using two devices or two operators in turns, data stratification is not enough.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 5: 2 of 3 consecutive points are on the same side of the center line and are greater than 2 times the standard deviation.

Possible reasons: generally considered to be new employees, process method errors, machine failures, unqualified raw materials, measurement errors, calculation errors, changes in inspection methods or standards.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 6: 4 out of 5 consecutive points are on the same side of the center line and are greater than 1 standard deviation

Possible reasons: generally considered to be new employees, process method errors, machine failures, unqualified raw materials, measurement errors, calculation errors, changes in inspection methods or standards.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 7: 15 consecutive points are within 1 standard deviation from the center line

Possible reasons: data falsification, control limit calculation errors, and insufficient data stratification.

Teach you step by step how to make SPC control charts and classify SPC control charts

Judgment criterion 8: 8 consecutive points on either side of the center line and no point within 1 standard deviation

Possible reasons: Insufficient data stratification.

Teach you step by step how to make SPC control charts and classify SPC control charts

Handling of SPC control chart abnormalities: When we discover SPC control abnormalities, we should first self-check whether the operation is strictly in accordance with the operating standards (SOP), and analyze and handle the problem with quality control professionals.

Reference link: https://www.infinityqs.cn/Aboutus/zixun/2955.html

Universal SPC template

A data-based SPC template has been developed. You only need to import data to produce large-screen pictures.

If necessary, send a private message to the blogger at the end of the article to obtain the code.

Insert image description here
Insert image description here

One word per text

Learning never ends

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/134921336