Matplotlib绘图:Helvetica Neue字体加粗无效,但Arial字体可以正常工作

3

我在使用matplotlib绘图时,设置'Helvetica Neue'字体的粗体时遇到了问题。使用'Arial'时可以正常工作,但是'Helvetica Neue'的粗体却无法正常工作。有什么建议吗?

这是我正在使用的Python代码:

mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 14

mpl.rcParams['font.family'] = 'sans-serif'
#mpl.rcParams['font.sans-serif'] = ['Arial Narrow']
#mpl.rcParams['font.sans-serif'] = ['Arial']
mpl.rcParams['font.sans-serif'] = ['Helvetica Neue']
mpl.rcParams["font.weight"] = "bold"
mpl.rcParams["axes.labelweight"] = "bold"
mpl.rcParams["axes.labelsize"] = 16

params = {'mathtext.default': 'regular'}
plt.rcParams.update(params)

.
.
.
.

axes.set_xlabel("hello world")
axes.set_ylabel('hello world  1x10$^{555}$ (unit)')

这里是附加的图片: 输入图像描述输入图像描述


你有一个名为“Helvetica Neue”的字体吗?在我看来,它似乎被默认为Arial。 - Tim Roberts
1
Latex通常有自己的字体集合。您可以通过import matplotlib.font_manager / from pprint import pprint / pprint(matplotlib.font_manager.fontManager.ttflist)获取列表。 - Tim Roberts
我运行了你提到的代码,对于Helvetica它确实给了我这个结果: <Font 'Helvetica Neue' (HelveticaNeue.ttc) normal normal 400 normal>嗯... 它应该是“.ttf”而不是“.ttc”。所有扩展名为“.ttf”的字体似乎都可以工作。 - Dhruv Patel
但是是否有加粗变体列出?除非有特定字体,否则无法创造加粗字体。 - Tim Roberts
好的!终于成功了!我使用在线数据转换器(https://transfonter.org/ttc-unpack)将.ttc格式的Helvetica字体转换为.ttf格式。然后将转换后的.ttf文件粘贴到<python install patl>/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf路径下,接着使用rm -rf ~/.matplotlib/fontlist.cache 和 rm -rf ~/.matplotlib/fontlist.json命令删除matplotlib缓存,最后重新运行我的代码。一切都很顺利。 - Dhruv Patel
显示剩余2条评论
1个回答

1

终于成功让它工作了。以下是我所采取的步骤。

  1. Convert the desired .ttc font files to .ttf using the online font converter.

  2. Then paste these .ttf converted files into >>

        <python install patl>/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf
    
  3. Remove the matplotlib cache (important step to see the changes) using

    rm -rf ~/.matplotlib/fontlist.cache
    

    and

    rm -rf ~/.matplotlib/fontlist.json
    
  4. Re-run the same code as in the question. It worked all well!


1
我和Helvetica字体的fontweight='bold'不起作用有相同的情况,而且这个解决方案也不起作用。我正在使用Mac OS。有什么想法吗? - s223

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接