编辑代码

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from matplotlib.dates import DateFormatter

# 创建数据集
data = {
    'Test Item': ['C-reactive protein (CRP)', 'Procalcitonin (PCT)', 'Interleukin-6 (IL-6)', 
                 'White Blood Cells (WBC)', 'Neutrophil % (N%)', 'Creatinine (Cr)', 'eGFR'],
    '2025-02-17 15:09:00': ['', '', '', 23.58, 0.967, 500, 9],
    '2025-02-17 20:36:00': ['', '', '', '', '', 435, 11],
    '2025-02-18 08:22:00': [219, '>100', 1675, 21.66, 0.535, 299, 17],
    '2025-02-18 08:26:00': [289, '>100', 1147, 21.13, 0.55, 299, 17],
    '2025-02-19 00:00:00': [125, 84.44, 598.8, 14.51, 0.908, 138, 42],
    '2025-02-19 10:47:00': [114, 81.55, 539.2, 13.39, 0.89, '', ''],
    '2025-02-20 00:00:00': [80, 69.58, 542.3, 9.69, 0.847, 199, 27],
    '2025-02-21 00:00:00': [66, 39.94, 446.3, 8.3, 0.839, 193, 28],
    '2025-02-22 00:00:00': [61, 22.06, 363, 7.26, 0.761, 490, 9],
    '2025-02-23 00:00:00': [53, 13, 357.5, 6.74, 0.736, 562, 8],
    '2025-02-24 00:00:00': [36, 7.13, 695.9, 7.04, 0.728, 586, 7],
    '2025-02-24 14:38:00': ['', '', '', 4.86, 0.801, 454, 10],
    '2025-02-24 16:09:00': ['', '', '', 3.42, 0.883, 401, 12],
    '2025-02-24 22:46:00': ['', '', '', 5.46, 0.652, 307, 16],
    '2025-02-25 00:00:00': [25, 2.43, 337.1, 4.38, 0.706, 235, 22],
    '2025-02-26 00:00:00': [28, 2.07, 292.8, 5.09, 0.925, 281, 18],
    '2025-02-27 00:00:00': [27, 1.45, 247.5, 4.91, 0.723, 318, 15],
    '2025-02-28 00:00:00': [27, 0.93, 240.8, 5.38, 0.725, 307, 16],
    '2025-02-28 14:35:00': ['', '', '', 4.77, 0.677, '', ''],
    '2025-02-28 17:08:00': ['', '', '', '', '', 192, 28],
    '2025-03-01 00:00:00': [22, 0.61, 245.2, 5.7, 0.706, 193, 28]
}

# 创建DataFrame
df = pd.DataFrame(data).set_index('Test Item').T
df.index = pd.to_datetime(df.index)

# 处理特殊值 (>100)
df = df.replace('>100', 101).apply(pd.to_numeric, errors='coerce')

# 创建多子图
fig, axes = plt.subplots(7, 1, figsize=(14, 20), sharex=True)
plt.suptitle('Clinical Laboratory Trends (Feb-Mar 2025)', fontsize=16, y=0.95)

# 添加每个指标的折线图
titles = [
    'CRP (mg/L) - Inflammation Marker',
    'PCT (ng/mL) - Infection Severity',
    'IL-6 (pg/mL) - Immune Response',
    'WBC (10⁹/L) - Infection Control',
    'Neutrophil % - Bacterial Infection',
    'Creatinine (μmol/L) - Kidney Function',
    'eGFR (mL/min) - Kidney Filtration'
]

colors = ['#E63946', '#1D3557', '#2A9D8F', '#E9C46A', '#F4A261', '#264653', '#6A994E']
markers = ['o', 's', '^', 'D', 'v', '<', '>']

for i, ax in enumerate(axes):
    col = df.columns[i]
    ax.plot(df.index, df[col], color=colors[i], marker=markers[i], markersize=6, linewidth=2)
    
    # 添加阈值线
    if col == 'Procalcitonin (PCT)':
        ax.axhline(y=0.5, color='r', linestyle='--', alpha=0.5, label='Normal Threshold (0.5)')
    elif col == 'White Blood Cells (WBC)':
        ax.axhline(y=11, color='r', linestyle='--', alpha=0.5, label='Normal Upper Limit')
        ax.axhline(y=4, color='r', linestyle='--', alpha=0.5)
    
    ax.set_title(titles[i], fontsize=12)
    ax.grid(alpha=0.2)
    
    # 特殊标记处理(PCT>100)
    if col == 'Procalcitonin (PCT)':
        over_limit = df[col] == 101
        ax.scatter(df.index[over_limit], df[col][over_limit], 
                  facecolor='none', edgecolor=colors[i], s=100, 
                  marker='s', label='>100 (Upper Limit)')

# 添加公共标签
fig.text(0.04, 0.5, 'Measurement Value', va='center', rotation='vertical', fontsize=12)
fig.text(0.5, 0.04, 'Date and Time', ha='center', fontsize=12)

# 优化X轴日期格式
date_format = DateFormatter("%m-%d")
for ax in axes:
    ax.xaxis.set_major_formatter(date_format)

# 添加图例
axes[1].legend(loc='upper right')

plt.tight_layout(rect=[0.03, 0.03, 1, 0.95])
plt.savefig('clinical_trends.png', dpi=300)
plt.show()