python - 尝试使用 Matplotlib 设置绘图字体时出错

标签 python matplotlib fonts plot

当我尝试在绘图中将字体设置为 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。添加标签而不设置字体效果很好。

此问题似乎与特定字体无关,因为尝试 HelveticaArial 会导致相同的问题。

我尝试删除 ~/.matplotlib/fontList.cache 中的字体缓存,然后卸载并重新安装 Matplotlib,并得到相同的错误。正如我所料,当我使用 matplotlib.font_manager.OSXInstalledFonts() 检查字体管理器时,我看到了已安装字体的完整列表。

我不确定是什么更改触发了此错误(可能正在升级到 10.10.2),但我能够以这种方式设置字体,直到我今天尝试 - 我什至尝试使用以前的工作代码。

最佳答案

当我使用 fontdict 时,我遇到了类似的问题。

我在这里找到了解决方案:http://matplotlib.1069221.n5.nabble.com/matplotlib-error-ValueError-failed-to-convert-font-family-name-to-ASCII-td44202.html

找到你的 matplotlibrc 文件:

import matplotlib
matplotlib.matplotlib_fname()

打开文件并更改图形后端,例如:
“后端:macosx”->“后端:TkAgg”

关于python - 尝试使用 Matplotlib 设置绘图字体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28307719/

相关文章:

python - matplotlib 离散点之间的填充

Python 正则表达式 : Extract volume (mL) from strings

python - 显示 scipy 树状图的聚类标签

python - Theano警告: The same cache key is associated to different modules

python-2.7 - 在 tkinter 窗口中嵌入 pyplot 并更新它

c++ - Mac OS X 上的 GetTextExtentPoint32 等价物

c# - Xamarin Android 同一 TextView 中的多种字体

fonts - 从图像和脚本创建 ttf

python - 除了监听探测请求之外,还有哪些方法可以用来查找 802.11 wifi 设备?

python - Django 中的 WordPress 小部件类型功能