Python matplotlib 无法显示中文、字体缺失(Mac)

Python利用Matplotlib绘图无法显示中文字体的解决方案 - 路饭网

报错:

/Applications/anaconda3/lib/python3.9/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 33457 (\N{CJK UNIFIED IDEOGRAPH-82B1}) missing from current font.

Mac OS的字体设置和Win不一样,按照一般的流程下载SimHei再放到对应目录没有用。

首先在Jupyter中输入以下命令查看matplotlib中的字体列表:

#查询一下matplotlib中拥有哪些语言
from matplotlib.font_manager import FontManager
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)
    
#Heiti TC
#Songti SC

经过筛选发现Mac中matplotlib包含Heiti TC和Songti SC两种中文字体。

输入以下命令设置字体,测试效果:

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font", family='Heiti TC')
 
squares = [1, 4, 9, 16, 25]
fig, ax = plt.subplots()
ax.plot(squares, linewidth=3)
#设置图表标题并给坐标轴加上标签。
ax.set_title("主题", size=14)
ax.set_xlabel("横坐标", size=14) # color='red'
ax.set_ylabel("纵坐标", size=14) # color='red'
 
ax.tick_params(axis='both', labelsize=14)
plt.show()

 发现效果很不错,当成模板解决其他问题就好。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值