我如何在matplotlib中使用Humor Sans?

2024-10-05 10:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用matploblib创建ROC曲线的XKCD样式的图形。它基本上很好用,但我不明白为什么它不使用幽默感。它肯定安装在我的系统上:

$ fc-list | grep -i humor
/home/user/.local/share/fonts/Humor-Sans.ttf: Humor Sans:style=Regular

如何使matplotlib使用无幽默字体?在

我已经尝试添加fontname='Humor Sans'

^{pr2}$

我的代码

import matplotlib.pyplot as plt
import numpy as np

with plt.xkcd():
    xs = np.linspace(0, 1, 50)
    plt.plot(xs, (1 / (-xs) + 50) / 50)
    plt.plot(xs, xs)
    plt.title('ROC Curve')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.savefig('xkcd-roc-explanation.png')

当前结果

enter image description here


Tags: importrateplotmatplotlibasnppltxkcd
2条回答

可以通过以下方式强制重建字体缓存:

import matplotlib.font_manager
matplotlib.font_manager._rebuild()

然后检查字体可用性

^{pr2}$

Matplotlib有自己的缓存文件。在我的系统中,我可以通过执行

mv ~/.cache/matplotlib/fontList.json ~/.cache/matplotlib/fontList-backup.json

相关问题 更多 >

    热门问题