UserWarning: Glyph 39640 missing from current font问题

文章讲述了如何在支持中文的系统(如本地环境)和GoogleColab这样的环境中,通过设置matplotlib的字体和安装中文字体文件来确保图表中的中文正常显示,包括解决负号显示问题和创建包含中文标题的图表实例。

是因为不支持中文字体导致的,设置为一个支持中文的字体就行了。

另外,上面的改动会引起负号显示为方块,需要额外再加一条设置。

在中文系统上

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置为一个支持中文的字体,如"SimHei"
plt.rcParams['axes.unicode_minus'] = False    # 解决负号'-'显示为方块的问题

在不支持中文的google colab上

上面的代码是在本机系统就有中文字体的情况下生效。
而在Google colab上,默认连中文字体都没有。

这样就需要先安装中文字体文件:

!wget -O /usr/share/fonts/truetype/liberation/simhei.ttf "https://www.wfonts.com/download/data/2014/06/01/simhei/chinese.simhei.ttf"

显示结果如下:

--2024-02-01 15:07:41--  https://www.wfonts.com/download/data/2014/06/01/simhei/chinese.simhei.ttf
Resolving www.wfonts.com (www.wfonts.com)... 172.67.129.58, 104.21.1.127, 2606:4700:3037::6815:17f, ...
Connecting to www.wfonts.com (www.wfonts.com)|172.67.129.58|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10050870 (9.6M) [application/octetstream]
Saving to: ‘/usr/share/fonts/truetype/liberation/simhei.ttf’

/usr/share/fonts/tr 100%[===================>]   9.58M  16.0MB/s    in 0.6s    

2024-02-01 15:07:42 (16.0 MB/s) - ‘/usr/share/fonts/truetype/liberation/simhei.ttf’ saved [10050870/10050870]

然后用font manager将字体读取进来:

import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/liberation/simhei.ttf')
plt.rcParams['axes.unicode_minus'] = False

之后在使用的时候指定字体属性:

fontproperties=zhfont

我们看个例子:

import numpy as np
import matplotlib.pyplot as plt

import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/liberation/simhei.ttf')
plt.rcParams['axes.unicode_minus'] = False

X = np.array([[20240102],[20240103],[20240104],[20240105],[20240108],[20240109]]).reshape(-1,1)
y = [2962.28,2967.25,2954.35,2929.18,2887.54,2893.25]

plt.figure()
plt.title('2024年1月上证指数走势图',fontproperties=zhfont)
plt.xlabel('日期',fontproperties=zhfont)
plt.ylabel('收盘价',fontproperties=zhfont)
plt.plot(X,y,'o',c='r')
plt.grid(True)
plt.show()

图片显示如下:

✓ 特征保存到: E:\Yolov8\multi_polar_spectral\results\features.json 总样本数: 3 E:/Yolov8/multi_polar_spectral/fixed_analysis.py:355: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. fig, axes = plt.subplots(2, 3, figsize=(15, 10)) E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 20851 (\N{CJK UNIFIED IDEOGRAPH-5173}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 38190 (\N{CJK UNIFIED IDEOGRAPH-952E}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 29305 (\N{CJK UNIFIED IDEOGRAPH-7279}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 24449 (\N{CJK UNIFIED IDEOGRAPH-5F81}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 33258 (\N{CJK UNIFIED IDEOGRAPH-81EA}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 21160 (\N{CJK UNIFIED IDEOGRAPH-52A8}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 25512 (\N{CJK UNIFIED IDEOGRAPH-63A8}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 21017 (\N{CJK UNIFIED IDEOGRAPH-5219}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 37327 (\N{CJK UNIFIED IDEOGRAPH-91CF}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:471: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. plt.tight_layout() E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 20851 (\N{CJK UNIFIED IDEOGRAPH-5173}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 38190 (\N{CJK UNIFIED IDEOGRAPH-952E}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 29305 (\N{CJK UNIFIED IDEOGRAPH-7279}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 24449 (\N{CJK UNIFIED IDEOGRAPH-5F81}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 33258 (\N{CJK UNIFIED IDEOGRAPH-81EA}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 21160 (\N{CJK UNIFIED IDEOGRAPH-52A8}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 25512 (\N{CJK UNIFIED IDEOGRAPH-63A8}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 21017 (\N{CJK UNIFIED IDEOGRAPH-5219}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 37327 (\N{CJK UNIFIED IDEOGRAPH-91CF}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:/Yolov8/multi_polar_spectral/fixed_analysis.py:475: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. plt.savefig(output_path, dpi=300, bbox_inches='tight') E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 20851 (\N{CJK UNIFIED IDEOGRAPH-5173}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 38190 (\N{CJK UNIFIED IDEOGRAPH-952E}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 29305 (\N{CJK UNIFIED IDEOGRAPH-7279}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 24449 (\N{CJK UNIFIED IDEOGRAPH-5F81}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 33258 (\N{CJK UNIFIED IDEOGRAPH-81EA}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 21160 (\N{CJK UNIFIED IDEOGRAPH-52A8}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 25512 (\N{CJK UNIFIED IDEOGRAPH-63A8}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 21017 (\N{CJK UNIFIED IDEOGRAPH-5219}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 37327 (\N{CJK UNIFIED IDEOGRAPH-91CF}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:83: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. FigureCanvasAgg.draw(self) E:/Yolov8/multi_polar_spectral/fixed_analysis.py:476: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. plt.show() E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 20851 (\N{CJK UNIFIED IDEOGRAPH-5173}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 38190 (\N{CJK UNIFIED IDEOGRAPH-952E}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 29305 (\N{CJK UNIFIED IDEOGRAPH-7279}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 24449 (\N{CJK UNIFIED IDEOGRAPH-5F81}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 33258 (\N{CJK UNIFIED IDEOGRAPH-81EA}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 21160 (\N{CJK UNIFIED IDEOGRAPH-52A8}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 25512 (\N{CJK UNIFIED IDEOGRAPH-63A8}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 23548 (\N{CJK UNIFIED IDEOGRAPH-5BFC}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 21017 (\N{CJK UNIFIED IDEOGRAPH-5219}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 23454 (\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 38469 (\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 37327 (\N{CJK UNIFIED IDEOGRAPH-91CF}) missing from current font. FigureCanvasAgg.draw(self) E:\Deeplearning_soft\pycharm\Pycharm\PyCharm 2020.1\plugins\python\helpers\pycharm_matplotlib_backend\backend_interagg.py:65: UserWarning: Glyph 20540 (\N{CJK UNIFIED IDEOGRAPH-503C}) missing from current font. FigureCanvasAgg.draw(self)
最新发布
12-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jtag特工

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

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

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

打赏作者

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

抵扣说明:

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

余额充值