termux中matplotlib无法显示中文问题的解决方法
作者:彩克拉夫斯基 时间:2022-12-16 00:07:16
问题背景
在开始正文之前,感谢用户名为怜索的朋友送给了我的博客2021年的第一个赞!
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import os
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'in'
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
#显示中文
plt.rcParams['font.sans-serif'] = ['simhei']
x = np.linspace(-1,1,50)#从(-1,1)均匀取50个点
y = 2 * x
plt.plot(x,y)
plt.xlabel("吸光值")
plt.ylabel("浓度")
plt.show()
上述代码在window和linux系统均能够在横纵坐标标题正确显示中文,但在termux中效果如下:
解决方案
从window或者当前系统中找到中文字体,或者前往https://github.com/dolbydu/font下载相应字体(该网站目前需要科学上网),本文以字体simhai.ttf(黑体)为例;
将simhai.ttf文件移动到/data/data/com.termux/files/usr/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf目录下;
修改matplotlibrc文件
#去掉注释
font.family : sans-serif
#去掉注释,并在冒号后的值前添加前文提及的中文字体
font.sans-serif : STKaiti, Simhei........
#删除matplotlib缓存
rm -rf ~/.cache/matplotlib
测试
还是上述代码:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import os
#设置坐标轴朝向
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'in'
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
#显示中文
plt.rcParams['font.sans-serif'] = ['simhei']
x = np.linspace(-1,1,50)#从(-1,1)均匀取50个点
y = 2 * x
plt.plot(x,y)
plt.xlabel("吸光值")
plt.ylabel("浓度")
#由于termux没有图像界面,因此不能够使用plt.show(),因此用plt.savefig()配合termux-open命令替代
plt.savefig("test2.png")
os.system("termux-open test2.png")
效果:
附加:matplotlib中文字体目录
黑体:SimHei
微软雅黑:Microsoft YaHei
微软正黑体:Microsoft JhengHei
新宋体:NSimSun
新细明体:PMingLiU
细明体:MingLiU
华文新魏:STXinwei
华文行楷:STXingkai
华文隶书:STLliti
花纹琥珀:STHupo
华文彩云:STCaiyun
方正姚体:FZYaoti
方正舒体:FZShuTi
标楷体:DFKai-SB
华文仿宋:STFangsong
华文中宋:STZhongsong
华文宋体:STSong
华文楷体:STKaiti
华文细黑:STXihei
幼圆:YouYuan
隶书:LiSu
楷体_GB 2313:Kaiti_GB2313
仿宋_GB2313:FangSong_GB2313
仿宋:FangSong
来源:https://blog.csdn.net/qq_41144723/article/details/112386829
标签:termux,matplotlib,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 3调用百度OCR API实现剪贴板文字识别
2022-12-13 19:01:14
.Net Core服务治理Consul搭建集群
2024-05-09 09:03:07
![](https://img.aspxhome.com/file/2023/4/129814_0s.jpg)
ASP,PHP与.NET伪造HTTP-REFERER方法及防止伪造REFERER的方法
2010-03-12 12:40:00
找到MySQL的优点
2024-01-23 01:28:47
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
2021-11-29 10:44:29
Python中列表的基本操作汇总
2021-08-20 23:21:27
Python脚本实现下载合并SAE日志
2023-04-13 06:41:41
Python基于Pymssql模块实现连接SQL Server数据库的方法详解
2024-01-15 03:13:17
![](https://img.aspxhome.com/file/2023/4/89174_0s.png)
vue.js2.0 实现better-scroll的滚动效果实例详解
2023-07-02 16:58:50
![](https://img.aspxhome.com/file/2023/4/139864_0s.png)
js 判断一组日期是否是连续的简单实例
2024-04-17 10:41:12
Python中return用法案例详解
2022-09-09 10:35:40
很全面的MySQL处理重复数据代码
2024-01-24 14:26:45
django Serializer序列化使用方法详解
2022-07-17 12:42:24
FCKeditor 编辑器实战技巧 Ⅰ
2008-10-08 10:22:00
![](https://img.aspxhome.com/file/uploadpic/20071/200713110656772.gif)
SQL学习笔记四 聚合函数、排序方法
2024-01-20 02:54:43
如何在Python里使用ChatGPT及ChatGPT是什么?注册方式?
2022-12-01 22:43:12
![](https://img.aspxhome.com/file/2023/8/124378_0s.jpg)
python中doctest库实例用法
2022-07-22 16:52:30
SQLSERVER记录登录用户的登录时间(自写脚本)
2024-01-27 15:07:24
![](https://img.aspxhome.com/file/2023/7/129867_0s.jpg)
Python对象的深拷贝和浅拷贝详解
2022-08-29 23:39:02
ASP强制刷新和判断文件地址是否存在
2007-09-16 17:11:00