import pandas as pd import numpy as np from pathlib import Path from loguru import logger import matplotlib.pyplot as plt def generate(output_dir): logger.info("开始生成就业数据表") categories = ['城镇调查失业率', '青年失业率', '制造业PMI', '服务业PMI'] current = [5.1, 15.3, 49.8, 53.2] previous = [5.2, 15.6, 49.5, 53.0] change = [c - p for c, p in zip(current, previous)] data = pd.DataFrame({ '指标': categories, '当前值': current, '上期值': previous, '变动': change }) fig, ax = plt.subplots(figsize=(10, 6)) ax.axis('tight') ax.axis('off') table_data = [] for _, row in data.iterrows(): change_color = '#4CAF50' if row['变动'] >= 0 else '#F44336' table_data.append([ row['指标'], f"{row['当前值']:.1f}", f"{row['上期值']:.1f}", f"{row['变动']:+.1f}" ]) table = ax.table( cellText=table_data, colLabels=['指标', '当前值', '上期值', '变动'], cellLoc='center', loc='center', colColours=['#4A90E2'] * 4 ) table.auto_set_font_size(False) table.set_fontsize(12) table.scale(1, 2) for key, cell in table.get_celld().items(): if key[0] > 0 and key[1] == 3: cell.set_text_props(color='green' if '+' in cell.get_text().get_text() else 'red') ax.set_title('就业与PMI数据摘要', fontsize=16, fontweight='bold', pad=20) output_path = Path(output_dir) / 'employment_table.png' plt.savefig(str(output_path), dpi=150, bbox_inches='tight') plt.close() logger.info(f"就业数据表已生成: {output_path}") return str(output_path) if __name__ == "__main__": generate(Path.cwd())