当前位置:网站首页>永久解决 matplotlib 中文乱码
永久解决 matplotlib 中文乱码
2022-07-19 21:15:00 【AI科技大本营】
作者 | 老表
来源 | 简说Python
今天给大家分享一个永久解决matplotlib中文乱码的笔记,希望对大家有所帮助。
首先需要你的电脑安装好了Python环境,并且安装好了Python开发工具。
如果你还没有安装,可以参考以下文章:
查看matplotlib安装目录
import matplotlib
# 查了安装路径
print(matplotlib.get_data_path())
'''
/Users/hhh/Library/Python/3.7/lib/python/site-packages/matplotlib/mpl-data
'''
将下载好的字体复制到
mpl-data/fonts/ttf
目录下
修改matplotlib设置文件,在
mpl-data/matplotlibrc
,利用文本模式打开即可,修改三个地方,修改好后保存即可:
# 第一个地方:font.family: sans-serif这一句前面的#去掉
font.family: sans-serif
# 第二个地方:在:(冒号)后加一个SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream ...
# 第三个地方:axes.unicode_minus后的值改为False
axes.unicode_minus: False
我们需要重新加载字体配置 如果你的matplotlib是3+以下,可以使用以下代码实现该步操作:
from matplotlib.font_manager import _rebuild
_rebuild()
如果你的matplotlib是3+及以上,在这些版本中私有方法_rebuild已被弃用,所以没法使用上面方法进行重新加载,不过更简单的,你只需要重新启动下你的python环境就可以了。
以jupyter为例子,点一下图中按钮重启内核,再运行代码即可显示中文。
要是还是不行,查看下matplotlib缓存存放位置,然后删除,再重启jupyter notebook试试。
# matplotlib缓存的目录
import matplotlib
print(matplotlib.get_cachedir())
'''
/Users/hhh/.matplotlib
'''
这个小绘图示例也给到大家方便检测。
# 随便绘制一个饼图
import matplotlib.pyplot as plt
fig1 = plt.figure() # 先创建一个图像对象
plt.pie([0.5, 0.3, 0.2], # 值
labels=['我', '你', '它'], # 标签
explode=(0, 0.2, 0), # (爆裂)距离
autopct='%1.1f%%', # 显示百分数格式
shadow=True) # 是否显示阴影
plt.show()
往期回顾
分享
点收藏
点点赞
点在看
边栏推荐
- Ott large screen "new audience rating system" is coming, and advertisers are no longer confused?
- 数据脱敏的功能与技术原理【详解】
- Set aside 300 hours to teach you how to make a Lego submarine! Netizens read all the masterpieces
- Good news | digital data won the "2022 love analysis · China's low code best practice case"
- Operation element case collection
- 【IEEE出版】2022年自然语言处理与信息检索国际会议(ECNLPIR 2022)
- dbeaver连接Oracle用户显示错误及用户不存在?
- 北京邮电大学|RIS辅助室内多机器人通信系统的联合深度强化学习
- [C # process control] - if/switch selection structure in C #
- Math.random , switch选择结构
猜你喜欢
详解“洋葱架构”
【面试必刷101】贪心算法、模拟、字符串
模拟实现库函数strstr--查找子字符串
JDBC quick start
面試官問我JVM的GC分代收集算法為什麼這麼設計
PyTorch笔记 - R-Drop、Conv2d、3x3+1x1+identity算子融合
Simple examples of pointer arrays and array pointers
TS learning notes
Open the physical space for the construction of maker education courses
Simulate the implementation library function strcat-- append a copy of the source string to the target string (understand the memory overlap problem)
随机推荐
TS 学习笔记
Ffmpeg video decoding
2022年找实习
华为云从入门到实战 | 云速建站服务与企业主机安全服务
Problems solved in the company
Es6-11 learning notes
[C # process control] - if/switch selection structure in C #
[QNX hypervisor 2.2 user manual]8.4 interprocessor interrupt
史上最全的 IDEA Debug 调试技巧(超详细案例)
北京邮电大学|RIS辅助室内多机器人通信系统的联合深度强化学习
LeetCode:733. 图像渲染【BFS】
cookie如何测试
2022 Henan Mengxin League game (2): Henan University of technology G - infinite
The interviewer asked me why the GC generational collection algorithm of JVM was so designed
ES6-11 学习笔记
4R分析redis处理客户端请求
留出300个小时,手把手教你做一个乐高潜水艇!网友看了都直呼大师之作
Goland语言入门,实现九九乘法表
你真的了解图像吗?(机器视觉)
史上最全的 IDEA Debug 调试技巧(超详细案例)