利用python编写一段uchart SPC控制图
时间: 2023-07-18 16:18:55 浏览: 61
好的,下面是一段利用Python编写的u图与c图控制图的代码:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 生成数据
data = np.random.normal(loc=10, scale=2, size=100)
df = pd.DataFrame(data, columns=['Value'])
# 计算统计量
mean = df['Value'].mean()
std = df['Value'].std()
# 计算控制限
ucl = mean + 3 * std / np.sqrt(len(df))
lcl = mean - 3 * std / np.sqrt(len(df))
cl = mean
# 绘制u图控制图
fig, ax = plt.subplots()
ax.axhline(ucl, color='r', linestyle='--', label='UCL')
ax.axhline(cl, color='b', linestyle='--', label='CL')
ax.axhline(lcl, color='r', linestyle='--', label='LCL')
ax.plot(df['Value'], 'o-', label='Value')
ax.legend()
ax.set_title('U Chart')
# 计算c值
df['Count'] = 1
count_mean = df['Count'].mean()
count_std = df['Count'].std()
# 计算控制限
cucl = count_mean + 3 * count_std
clcl = count_mean
cl = count_mean - 3 * count_std
# 绘制c图控制图
fig, ax = plt.subplots()
ax.axhline(cucl, color='r', linestyle='--', label='UCL')
ax.axhline(cl, color='b', linestyle='--', label='CL')
ax.axhline(clcl, color='r', linestyle='--', label='LCL')
ax.plot(df['Count'], 'o-', label='Count')
ax.legend()
ax.set_title('C Chart')
```
这段代码会生成两个图,一个是u图控制图,一个是c图控制图。其中,u图控制图用于监控连续型数据的变化,c图控制图用于监控计数型数据的变化。需要注意的是,这段代码中生成的数据是随机的,实际应用时需要根据具体的业务场景来生成数据。