Linux下python绘图缺字体Arial的处理方法

在Debian下用python绘图发现缺失字体,出现错误,如下:

findfont: Generic family 'sans-serif' not found because none of the following families were found: Arial

该错误原因是如下代码找不到字体:

matplotlib.rcParams["font.sans-serif"] = ["Arial"]

解决方法:

  • 配置matplotlib:用户主目录里找到(Ctrl+H)隐藏文件~/.config/matplotlib/,然后把windows字体如 arial.ttf 拷贝至目录内。
  • 删除缓存:删除matplotlib的缓存文件,以便让matplotlib重新加载新的字体设置,缓存文件通常位于用户主目录里的~/.cache/matplotlib/
  • 重新运行程序
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python matplotlib库中调整字体方法和示例: 1. 统一设置字体字体大小和字体粗细: ```python import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'Arial' # 设置字体 plt.rcParams['font.size'] = 12 # 设置字体大小 plt.rcParams['font.weight'] = 'bold' # 设置字体粗细 # 示例 plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('X轴标签') plt.ylabel('Y轴标签') plt.title('标题') plt.show() ``` 2. 单独设置坐标轴字体大小和标签字体大小: ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlabel('X轴标签', fontsize=14) # 设置坐标轴字体大小 ax.set_ylabel('Y轴标签', fontsize=14) # 设置坐标轴字体大小 ax.set_title('标题', fontsize=16) # 设置标题字体大小 ax.tick_params(axis='x', labelsize=12) # 设置x轴刻度标签字体大小 ax.tick_params(axis='y', labelsize=12) # 设置y轴刻度标签字体大小 # 示例 ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.show() ``` 3. 单独设置坐标轴字体粗细和标签字体粗细: ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlabel('X轴标签', fontweight='bold') # 设置坐标轴字体粗细 ax.set_ylabel('Y轴标签', fontweight='bold') # 设置坐标轴字体粗细 ax.set_title('标题', fontweight='bold') # 设置标题字体粗细 ax.tick_params(axis='x', labelweight='bold') # 设置x轴刻度标签字体粗细 ax.tick_params(axis='y', labelweight='bold') # 设置y轴刻度标签字体粗细 # 示例 ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.show() ``` 4. 单独设置坐标轴字体样式(斜体、正常、倾斜): ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlabel('X轴标签', fontstyle='italic') # 设置坐标轴字体样式为斜体 ax.set_ylabel('Y轴标签', fontstyle='normal') # 设置坐标轴字体样式为正常 ax.set_title('标题', fontstyle='oblique') # 设置标题字体样式为倾斜 # 示例 ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.show() ``` 5. 单独设置坐标轴字体旋转角度: ```python import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_xlabel('X轴标签', rotation=45) # 设置x轴标签字体旋转角度为45度 ax.set_ylabel('Y轴标签', rotation=90) # 设置y轴标签字体旋转角度为90度 # 示例 ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值