彻底解决python中jupyter画图matplotlib的中文显示问题。Mac版本和Windows版本

jupyter使用matplotlib进行画图会面临中文无法显示的问题,导致这样的原因是没有配置对应的中文字体,所以无法在画图时显示中文。

Windows版本解决方法

在Window中,采用以下代码

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  #用来正常显示负号

Mac版本解决方法

Mac中没有SimHei字体,所以Windows的代码无法使用,所以要先查询mac中支持的中文字体。以下代码可以查询mac中的所有字体。

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

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

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

以上都是mac系统上的字体,选择一种中文字体就可以显示。例如:

红色标记的就是黑体,将字体写入,代码如下:

import matplotlib

# 解决中文乱码
matplotlib.rcParams['font.sans-serif'] =  ['Heiti TC']
matplotlib.rcParams['font.serif'] =  ['Heiti TC']
matplotlib.rcParams['axes.unicode_minus'] = False

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIGC人工智残

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值