matplotlib中文字体设置
========== 自动检测中文字体 ==========
def setup_chinese_font():
"""自动检测并设置可用的中文字体"""
chinese_fonts = ["Microsoft YaHei", "FangSong", "KaiTi", "SimHei", "SimSun"]
found_font = None
for font in chinese_fonts:
try:
if findfont(FontProperties(family=[font])) not in ['', None]:
found_font = font
break
except:
continue
if found_font:
plt.rcParams['font.sans-serif'] = [found_font]
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
print(f"✅ 使用中文字体: {found_font}")
else:
print("⚠️ 未找到可用中文字体,请手动安装 SimHei.ttf 或指定字体路径。")
# 初始化中文字体
setup_chinese_font()
posted on 2025-06-27 18:02 Indian_Mysore 阅读(11) 评论(0) 收藏 举报
浙公网安备 33010602011771号