#!/usr/bin/env python3 """ 测试图表中文显示 """ import matplotlib.pyplot as plt import numpy as np # 测试中文字体显示 def test_chinese_font(): print("测试中文字体显示...") # 设置中文字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 # 生成测试数据 x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # 创建图表 fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # 第一个图表 ax1.plot(x, y1, label='正弦函数') ax1.plot(x, y2, label='余弦函数') ax1.set_title('三角函数图像') ax1.set_xlabel('时间 (秒)') ax1.set_ylabel('幅值') ax1.legend() ax1.grid(True) # 第二个图表 categories = ['探测器1', '探测器2', '探测器3'] values = [30, 45, 25] ax2.bar(categories, values) ax2.set_title('各探测器信号数量') ax2.set_ylabel('数量') plt.tight_layout() # 保存图表 plt.savefig('test_chinese_font.png', dpi=150) print("图表已保存为 test_chinese_font.png") # 显示图表 plt.show() print("测试完成!") if __name__ == "__main__": test_chinese_font()