Linux 系统 matplotlib 正常使用中文

  • 在该实验中用到的 Linux 系统是 Ubuntu 16.04。
  • 下载中文字体 simhei.ttf
  • 搜索 matplotlib 的字体安装位置:locate -b '\mpl-data'。由于我创建了虚拟环境,得到的路径为:
/home/yilonghao/ml/venv/lib/python3.5/site-packages/matplotlib/mpl-data
  • 切换到目标路径***意命令末尾的/fonts/ttf):
cd /home/yilonghao/ml/venv/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf
  • 将下载的字体拷贝到该路径下。
  • 删除当前用户的 matplotlib 缓存。
cd ~/.cache/matplotlib
rm -rf *.*

Windows 系统 matplotlib 使用中文

Windows 系统 matplotlib 可以直接通过在代码中添加 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] 使用中文。

示例

import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文


fig = plt.figure(figsize=(8, 6))
x_axis = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
y_axis = [100, 200, 300, 400, 300, 200, 100]
plt.bar(range(len(y_axis)), y_axis, width=1., color='rgb',tick_label=x_axis)
plt.ylabel('销量(KG)')
plt.title('水果销量统计图')
plt.savefig('./水果销量统计图(中文版).png')
plt.show()

Linux/Windows 系统 matplotlib 正常使用中文

相关文章:

  • 2021-08-16
  • 2022-12-23
  • 2021-09-02
  • 2021-08-29
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-31
  • 2021-12-14
  • 2022-12-23
  • 2021-08-29
  • 2022-01-11
  • 2022-12-23
  • 2021-05-04
相关资源
相似解决方案