Python 画图显示中文宋体

Python 使用matplotlib.pyplot库画图显示
中文宋体,英文Times New Roman

import matplotlib.pyplot as plt

# 中文宋体,英文Times New Roman
plt.rc('font', family=['Songti SC','Times New Roman'])

# 数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# 画折线图
plt.plot(x, y, marker='o', linestyle='-')

# 添加标题和标签,并设置字号大小
plt.title('折线图示例', fontsize=16)  # 设置标题字号为16
plt.xlabel('X轴标签', fontsize=14)  # 设置X轴标签字号为14
plt.ylabel('Y轴标签', fontsize=14)  # 设置Y轴标签字号为14

# 显示网格线
plt.grid(True)

# 显示图形
plt.show()

显示出所有支持的字体

# 查询当前系统所有字体
from matplotlib.font_manager import FontManager
import subprocess

mpl_fonts = set(f.name for f in FontManager().ttflist)

print('all font list get from matplotlib.font_manager:')
for f in sorted(mpl_fonts):
    print('\t' + f)

参考链接

posted @ 2024-04-07 16:02  zkx98  阅读(378)  评论(0编辑  收藏  举报