该项目解决了AI Studio上Matplotlib生成图表无法显示汉字的问题,内置微软雅黑、楷体等五种字体。以国民经济核算季度数据为例,展示了散点图、折线图、直方图、饼图的绘制,供学习参考,致敬开源,无商业用途。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

解决了在 AI Studio 上用 Matplotlib 生成常见图表(散点图、折线图、直方图、饼图)无法显示汉字的问题。
大家好,我是行远见大。欢迎你与我一同建设飞桨开源社区,知识分享是一种美德,让我们致敬开源!
本项目旨在以计算机技术研究交流为目的,仅供大家学习与参考,不存在任何的商业目的与商业用途。
本项目内置了五种字体供大家学习与参考,字体的路径: ./fonts/
| 内置字体 | 字体名称 |
|---|---|
| msyh.ttc | 微软雅黑 |
| simkai.ttf | 常规楷体 |
| simsun.ttf | 常规宋体 |
| SimHei.ttf | 常规黑体 |
| SIMLI.TTF | 常规隶书 |
import numpy as npimport matplotlib.pyplot as pltimport matplotlib.font_manager as font_manager
# 查看字体的引用名fontpath = 'fonts/msyh.ttc' # 选择字体prop = font_manager.FontProperties(fname=fontpath)print(prop.get_name()) %matplotlib inlinefrom matplotlib.font_manager import FontProperties font = FontProperties(fname='fonts/msyh.ttc', size=16) # 设置大小
Microsoft YaHei
data = np.load('work/国民经济核算季度数据.npz', allow_pickle=True)
columns = data['columns']
values = data['values']print(columns)print(values)
data['values'].shape['序号' '时间' '国内生产总值_当季值(亿元)' '第一产业增加值_当季值(亿元)' '第二产业增加值_当季值(亿元)' '第三产业增加值_当季值(亿元)' '农林牧渔业增加值_当季值(亿元)' '工业增加值_当季值(亿元)' '建筑业增加值_当季值(亿元)' '批发和零售业增加值_当季值(亿元)' '交通运输、仓储和邮政业增加值_当季值(亿元)' '住宿和餐饮业增加值_当季值(亿元)' '金融业增加值_当季值(亿元)' '房地产业增加值_当季值(亿元)' '其他行业增加值_当季值(亿元)'] [[1 '2000年第一季度' 21329.9 ... 1235.9 933.7 3586.1] [2 '2000年第二季度' 24043.4 ... 1124.0 904.7 3464.9] [3 '2000年第三季度' 25712.5 ... 1170.4 1070.9 3518.2] ... [67 '2016年第三季度' 190529.5 ... 15472.5 12164.1 37964.1] [68 '2016年第四季度' 211281.3 ... 15548.7 13214.9 39848.4] [69 '2017年第一季度' 180682.7 ... 17213.5 12393.4 42443.1]]
(69, 15)
plt.figure(figsize=(10, 8))
plt.scatter(values[:, 1], values[:, 3], marker='o')
plt.scatter(values[:, 1], values[:, 4], marker='*')
plt.scatter(values[:, 1], values[:, 5], marker='D')
plt.xticks(range(0, 70, 4), values[range(0, 70, 4), 1], rotation=45, fontproperties=font)
plt.legend(['第一产业生产总值', '第二产业生产总值', '第三产业生产总值'], prop=font)
plt.title('2000-2017年各产业生产总值散点图', fontproperties=font)
plt.ylabel('生产总值(亿元)', fontproperties=font)
plt.savefig('work/2000-2017年各产业生产总值散点图.png')
plt.show()<Figure size 720x576 with 1 Axes>
plt.figure(figsize=(10, 8))
plt.plot(values[:, 1], values[:, 3], linestyle='solid')
plt.plot(values[:, 1], values[:, 4], marker='*')
plt.plot(values[:, 1], values[:, 5], marker='D')
plt.xticks(range(0, 70, 4), values[range(0, 70, 4), 1], rotation=45, fontproperties=font)
plt.legend(['第一产业生产总值', '第二产业生产总值', '第三产业生产总值'], prop=font)
plt.title('2000-2017年各产业生产总值散点图', fontproperties=font)
plt.ylabel('生产总值(亿元)', fontproperties=font)
plt.savefig('work/2000-2017年各产业生产总值折线图.png')
plt.show()<Figure size 720x576 with 1 Axes>
plt.figure(figsize=(10, 8))
plt.bar(columns[3:6], values[-1, 3:6], color='gold', width=0.6, tick_label=['1','2','3'])
plt.title('2017年第一季度各产业生产总值直方图', fontproperties=font)
plt.xlabel('产业', fontproperties=font)
plt.ylabel('生产总值(亿元)', fontproperties=font)
my_height = values[-1, 3:6]for i in range(len(my_height)):
plt.text(i, my_height[i]+1000, my_height[i], va='bottom', ha='center')
plt.savefig('work/2017年第一季度各产业生产总值直方图.png')
plt.show()<Figure size 720x576 with 1 Axes>
plt.figure(figsize=(10, 8))
labels = ['第一产业', '第二产业', '第三产业']
plt.pie(values[-1, 3:6], explode=[0.2, 0.01, 0.01], labels=labels, colors=['red','gold','orange'], autopct='%1.1f%%', textprops={'fontproperties':font})
plt.title('2017年第一季度各产业生产总值饼图', fontproperties=font)
plt.savefig('work/2017年第一季度各产业生产总值饼图.png')
plt.show()<Figure size 720x576 with 1 Axes>
以上就是『行远见大』让 Matplotlib 生成的图表显示汉字的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号