当我尝试在绘图中将字体设置为 Times New Roman 时,我在 Mac OS X (10.10.2) 上的 Matplotlib 1.4.2 中遇到了一个新错误。这是一个给出错误的 MWE:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X axis', fontname='Times New Roman')
如果我尝试此操作,我会收到 ValueError: failed to conversion font family name to ASCII
。添加标签而不设置字体效果很好。
此问题似乎与特定字体无关,因为尝试 Helvetica
或 Arial
会导致相同的问题。
我尝试删除 ~/.matplotlib/fontList.cache
中的字体缓存,然后卸载并重新安装 Matplotlib,并得到相同的错误。正如我所料,当我使用 matplotlib.font_manager.OSXInstalledFonts()
检查字体管理器时,我看到了已安装字体的完整列表。
我不确定是什么更改触发了此错误(可能正在升级到 10.10.2),但我能够以这种方式设置字体,直到我今天尝试 - 我什至尝试使用以前的工作代码。
最佳答案
当我使用 fontdict 时,我遇到了类似的问题。
找到你的 matplotlibrc 文件:
import matplotlib
matplotlib.matplotlib_fname()
打开文件并更改图形后端,例如:
“后端:macosx”->“后端:TkAgg”
关于python - 尝试使用 Matplotlib 设置绘图字体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28307719/