问题描述
我的环境:
Ubuntu16.04
python3.7
jupyter
问题
使用plt.title
等设置标签为中文时,显示方框‘□’而非汉子。
解决办法
查看Ubuntu系统中的中文字体
在终端中运行如下命令,查看系统中安装的中文字体:
fc-list :lang=zh
从中选取一种字体文件。
手动选定字体
在文件中添加如下代码:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/System/Library/Fonts/STHeiti Light.ttc', size=16)
每一个中文标签都使用该字体
plt.plot([0, 1], [1, 2])
plt.title('显示中文', fontproperties=font)
plt.show()