yincheng.zhong
2025-11-27 1ebe8cd1247982a2c9d1d75b9c72d214eed4d581
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
修复matplotlib中文字体显示问题
可选:在calibration_analyzer.py开头导入此模块
"""
 
import matplotlib
import matplotlib.pyplot as plt
from pathlib import Path
import platform
 
 
def setup_chinese_font():
    """
    配置matplotlib中文字体
    自动检测操作系统并设置合适的中文字体
    """
    system = platform.system()
    
    if system == 'Windows':
        # Windows系统,尝试使用微软雅黑或SimHei
        fonts = ['Microsoft YaHei', 'SimHei', 'SimSun']
    elif system == 'Darwin':  # macOS
        fonts = ['PingFang SC', 'Heiti SC', 'STHeiti']
    else:  # Linux
        fonts = ['WenQuanYi Micro Hei', 'WenQuanYi Zen Hei', 'Noto Sans CJK SC', 'Droid Sans Fallback']
    
    # 设置字体
    matplotlib.rcParams['font.sans-serif'] = fonts
    matplotlib.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
    
    print(f"[字体配置] 系统: {system}")
    print(f"[字体配置] 尝试字体: {', '.join(fonts)}")
 
 
def test_chinese_display():
    """
    测试中文显示是否正常
    """
    setup_chinese_font()
    
    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot([0, 1, 2, 3], [0, 1, 4, 9], 'b-', linewidth=2, label='测试曲线')
    ax.set_xlabel('时间 (秒)')
    ax.set_ylabel('速度 (m/s)')
    ax.set_title('中文字体显示测试')
    ax.legend()
    ax.grid(True, alpha=0.3)
    
    output_file = Path(__file__).parent / 'font_test.png'
    plt.savefig(output_file, dpi=150, bbox_inches='tight')
    print(f"[字体测试] 测试图表已保存: {output_file}")
    print("[字体测试] 请打开图片检查中文是否正常显示")
    plt.close()
 
 
if __name__ == '__main__':
    test_chinese_display()