0
点赞
收藏
分享

微信扫一扫

ML:文本、图像等数值化数据相似度计算之余弦相似度计算三种python代码实现

北冥有一鲲 2023-06-12 阅读 63

现象

图表上中文变方框,日志中报错如下:

findfont: Generic family 'sans-serif' not found because none of the following families were found: 

解决办法

下载字体

http://xiazaiziti.com/210356.html

查询字体保存路径

查看配置文件路径

import matplotlib    
print(matplotlib.matplotlib_fname())

在这里插入图片描述
这里我们得到的路径是:

/opt/homebrew/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc

去掉/matplotlibrc ,拼上/fonts/ttf。我们得到最终字体文件保存路径为:

/opt/homebrew/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf

我们把上面下载的文件保存到些目录,并保证文件名为SimHei.ttf
在这里插入图片描述

修改配置文件matplotlibrc

/opt/homebrew/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc

修改我们上一步得到的配置文件。
修改内容如下:

#去掉前面的#
font.family:  sans-serif
#去掉前面的#,手动加SimHei
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#去掉前面的#,把True改为False
axes.unicode_minus: False  # use Unicode for the minus symbol rather than hyphen.  See
                           # https://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes

这时重新运行python程序,应该已经解决了中文乱码的问题了。如果还没有解决可能需要清空缓存。

清空缓存

执行下面代码可看到缓存路径:

import matplotlib
print(matplotlib.get_cachedir())

代码执行结果如下:

>>> import matplotlib
>>> print(matplotlib.get_cachedir())
/Users/itkey/.matplotlib

我们把这个目录删除就可以了。

rm -rf /Users/itkey/.matplotlib

至此中文可以正常显示了。

在这里插入图片描述

参考

https://blog.csdn.net/u012744245/article/details/119735461

举报

相关推荐

0 条评论