简体   繁体   English

Matplotlib 找不到 fonts

[英]Matplotlib cannot find fonts

I'm trying to use non-default fonts in matplotlib but keep on getting this warning/error.我正在尝试在 matplotlib 中使用非默认 fonts 但继续收到此警告/错误。

findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans

If I do如果我做

from matplotlib import pyplot as plt

plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = 'Helvetica'

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 2, 3], label='some label')
ax.legend()
ax.text(0.5, 0.5, 'some text', transform=ax.transAxes, size=25)

fig.savefig('text.png')

I get the error and both legend and text are in Dejavu Sans .我收到错误消息,图例和文本都在Dejavu Sans中。

However, if I load tex by adding the extra line when setting up rcParams但是,如果我在设置rcParams时通过添加额外的行来加载tex

plt.rcParams['text.usetex'] = True

the warning still occurs if I have ax.legend() (but goes away if I don't have ax.legend() ), although both the legend and the text are actually in Helvetica even if I get the warning.如果我有ax.legend()警告仍然会发生(但如果我没有ax.legend() ),尽管即使我收到警告,图例和文本实际上都在 Helvetica 中。

Can someone tell me why this happens?有人能告诉我为什么会这样吗? And what should I do to use non-default fonts when not loading tex ?不加载tex时我应该怎么做才能使用非默认 fonts ? I tried我试过了

sudo apt install msttcorefonts -qq
rm ~/.cache/matplotlib -rf

or uncomment the lines in matplotlibrc and rebuild but none of those worked.或取消注释matplotlibrc中的行并重新构建,但这些行都不起作用。

I had this issue.我有这个问题。 Trying to install atlas typewriter.尝试安装 atlas 打字机。 I partially used matplotlib font not found although I did not have a cache to delete, instead there were two json files (fontlist-330.json).我部分使用了 matplotlib 字体,虽然我没有要删除的缓存,但有两个 json 文件(fontlist-330.json)。 I manually added an entry to both jsons and it worked.我手动向两个 jsons 添加了一个条目,并且它起作用了。

  {
      "fname": "/Users/XXUSERNAMEXX/Library/Fonts/AtlasTypewriter-Regular.ttf",
      "name": "Atlas Typewriter",
      "style": "normal",
      "variant": "normal",
      "weight": "normal",
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM