48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
plt.rcParams['font.sans-serif'] = 'SimHei'
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
# 数据
|
|
risk_levels = ["高风险", "次高风险", "次低风险", "低风险"]
|
|
material = [41.7, 34.0, 58.3, 36.8]
|
|
equipment = [16.7, 18.0, 8.3, 10.5]
|
|
design = [37.5, 38.0, 33.3, 31.6]
|
|
manufacturing = [4.2, 10.0, 0.0, 21.1]
|
|
|
|
# 设置柱状图位置
|
|
x = np.arange(len(risk_levels))
|
|
width = 0.6
|
|
|
|
# 绘制堆叠柱状图
|
|
fig, ax = plt.subplots(figsize=(10,6))
|
|
|
|
bars_material = ax.bar(x, material, width, label="材料", color="#1f77b4")
|
|
bars_equipment = ax.bar(x, equipment, width, bottom=material, label="设备", color="#ff7f0e")
|
|
bars_design = ax.bar(x, design, width, bottom=np.array(material)+np.array(equipment), label="设计", color="#2ca02c")
|
|
bars_manufacturing = ax.bar(x, manufacturing, width, bottom=np.array(material)+np.array(equipment)+np.array(design), label="制造封测", color="#d62728")
|
|
|
|
# 添加柱内比例标签
|
|
for i in range(len(x)):
|
|
# 材料
|
|
ax.text(x[i], material[i]/2, f"{material[i]:.1f}%", ha='center', va='center', color='white', fontsize=10)
|
|
# 设备
|
|
ax.text(x[i], material[i]+equipment[i]/2, f"{equipment[i]:.1f}%", ha='center', va='center', color='white', fontsize=10)
|
|
# 设计
|
|
ax.text(x[i], material[i]+equipment[i]+design[i]/2, f"{design[i]:.1f}%", ha='center', va='center', color='white', fontsize=10)
|
|
# 制造封测
|
|
if manufacturing[i] > 0:
|
|
ax.text(x[i], material[i]+equipment[i]+design[i]+manufacturing[i]/2, f"{manufacturing[i]:.1f}%", ha='center', va='center', color='white', fontsize=10)
|
|
|
|
# 图表美化
|
|
ax.set_xticks(x)
|
|
ax.set_xticklabels(risk_levels)
|
|
ax.set_ylabel("占比 (%)")
|
|
ax.set_title("各风险等级企业结构占比(堆叠柱状图)")
|
|
ax.legend()
|
|
ax.set_ylim(0, 120)
|
|
ax.grid(axis='y', linestyle='--', alpha=0.7)
|
|
|
|
plt.tight_layout()
|
|
plt.show() |