编辑代码

import numpy as np
import matplotlib.pyplot as plt

# 初始化设置
plt.figure(figsize=(12, 10))
plt.style.use('seaborn-whitegrid')  # 使用科研风格模板

# 随机种子保证可重复性
np.random.seed(2024)

# 实验参数
total_cells = 50000
populations = [
    # 细胞类型           比例   X轴参数(均值,标准差) Y轴参数(均值,标准差)   颜色       标签
    ('Viable',         0.68,  (18, 4),         (17, 3),         '#3575D5', 'Viable (68%)'),
    ('Early Apoptosis',0.10,  (72, 7),         (25, 5),         '#4CAF50', 'Early Apoptosis (10%)'),
    ('Late Apoptosis', 0.20,  (68, 8),         (75, 8),         '#E53935', 'Late Apoptosis (20%)'),
    ('Necrosis',       0.02,  (22, 5),         (78, 6),         '#AB47BC', 'Necrosis (2%)')  # 自动计算剩余比例
]

# 生成数据
for label, ratio, x_params, y_params, color, legend_label in populations:
    n = int(total_cells * ratio)
    x = np.random.normal(x_params[0], x_params[1], n)
    y = np.random.normal(y_params[0], y_params[1], n)
    plt.scatter(x, y, s=6, alpha=0.3, color=color, edgecolors='none', label=legend_label)

# 门控分割线
plt.axvline(45, color='black', linestyle='--', linewidth=1.5, alpha=0.7)
plt.axhline(45, color='black', linestyle='--', linewidth=1.5, alpha=0.7)

# 注释设置
annotations = [
    (20, 20, 'Viable\n68%', '#3575D5'),
    (75, 20, 'Early\nApoptosis\n10%', '#4CAF50'),
    (75, 75, 'Late\nApoptosis\n20%', '#E53935'),
    (20, 75, 'Necrosis\n2%', '#AB47BC')
]

for x, y, text, color in annotations:
    plt.text(x, y, text, 
             ha='center', va='center',
             fontsize=11,
             color='black',
             bbox=dict(boxstyle='round', 
                     facecolor='white', 
                     alpha=0.8,
                     edgecolor=color))

# 坐标轴设置
plt.xlabel('Annexin V-FITC Intensity (a.u.)', fontsize=13, labelpad=12)
plt.ylabel('Propidium Iodide (PI) Intensity (a.u.)', fontsize=13, labelpad=12)
plt.xlim(0, 100)
plt.ylim(0, 100)
plt.xticks(np.arange(0, 101, 10), fontsize=10)
plt.yticks(np.arange(0, 101, 10), fontsize=10)

# 图例设置
legend = plt.legend(
    loc='upper right',
    frameon=True,
    framealpha=0.95,
    edgecolor='#333333',
    fontsize=12,
    markerscale=3,
    title='Cell Populations',
    title_fontsize=13
)
legend.get_frame().set_linewidth(1.5)

# 保存高清图像
plt.savefig('LargeScale_Apoptosis_Plot.png', 
           dpi=300, 
           bbox_inches='tight',
           facecolor='white')

plt.show()