카테고리 없음
[제조] 제품 결함 데이터 분석 1
insight2149
2025. 1. 8. 20:41
import pandas as pd
import matplotlib.pyplot as plt
# 데이터프레임 생성 (이미지 데이터를 CSV로 저장했다고 가정)
# 데이터 로드
df = pd.read_csv('/content/defects_data.csv')
df = pd.DataFrame(defect_data)
# defect_type과 severity별 발생 건수 집계
grouped_data = df.groupby(['defect_type', 'severity'])['defect_id'].count().reset_index()
# 피벗 테이블 생성 (그래프를 그릴 수 있도록 변환)
pivot_table = grouped_data.pivot(index='defect_type', columns='severity', values='defect_id').fillna(0)
# 합계 열 추가
pivot_table['Total'] = pivot_table.sum(axis=1)
# 막대 그래프 생성
ax = pivot_table.drop(columns='Total').plot(kind='bar', figsize=(10, 6), stacked=True, colormap='tab10', edgecolor='black')
# 각 막대 위에 합계 표시
for i, total in enumerate(pivot_table['Total']):
ax.text(i, total + 0.5, int(total), ha='center', fontsize=10, color='black')
# 그래프 꾸미기
plt.title('Defect Type vs Severity (with Total Counts)', fontsize=16)
plt.xlabel('Defect Type', fontsize=12)
plt.ylabel('Number of Defects', fontsize=12)
plt.legend(title='Severity', bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xticks(rotation=45)
plt.tight_layout()
# 그래프 출력
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
# 데이터프레임 생성 (이미지 데이터를 CSV로 저장했다고 가정)
# 데이터 로드
df = pd.read_csv('/content/defects_data.csv')
df = pd.DataFrame(defect_data)
# defect_type별 repair_cost 합계 계산
total_repair_cost = df.groupby('defect_type')['repair_cost'].sum().reset_index()
# 그래프 생성
plt.figure(figsize=(8, 6))
bars = plt.bar(total_repair_cost['defect_type'], total_repair_cost['repair_cost'], color=['blue', 'orange', 'green', 'red'])
# 그래프 위에 합계 표시
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width() / 2, yval, f'{yval:.2f}', ha='center', va='bottom', fontsize=10, color='black')
# 그래프 꾸미기
plt.title('Total Repair Cost by Defect Type')
plt.xlabel('Defect Type')
plt.ylabel('Total Repair Cost ($)')
plt.xticks(rotation=45)
plt.tight_layout()
# 그래프 출력
plt.show()