【经验】一劳永逸解决中文显示是大方块,不是乱码(最详细解释,有图有真相)

问题描述

在这里插入图片描述
大家好,我们在用 networkx 显示中文的时候,会发现不能显示中文。

解决办法

  1. 下载附件中的字体;
  2. 在 jupyter notebook 中执行
import matplotlib
print(matplotlib.__path__)

找到 matplotlib 的路径,然后 cd 到这个路径。 cd 到这个路径之后,继续 cd,cd 到 mpl-data/fonts/ttf 这个路径。 然后把 DejaVuSans.ttf 这个文件换成我们刚刚下在的文件,遂试图找到matplotlib调用的字体文件,还真找到了。因为matplotlib默认调用的为DejaVuSans.ttf字体文件,只要从windows拷贝一个你想要的字体文件,替换DejaVuSans.ttf,就没问题了。

$ mv SimHei.ttf nx.draw(city_graph, city_location, with_labels=True, node_size=10).ttf

其中,用到的 ttf 字体。
在Windows系统C:\Windows\Fonts 路径下
在这里插入图片描述
在这里插入图片描述

发布了42 篇原创文章 · 获赞 28 · 访问量 4961

猜你喜欢

转载自blog.csdn.net/KaelCui/article/details/105490052