matplotlib的xkcd()无法正常工作。

9

好的,我知道这个问题已经被问了很多次,但是我无法让它工作:

我尝试在Ubuntu 16.04 LTS 64位系统上使用Python 2.7.12 64位版本的Matplotlib,并使用来自matplotlib.org/xkcd/examples的示例代码,但我仍然得到这个结果!

我已经做了什么

  • 通过pip安装了Matplotlib(版本2.2.2)
  • 安装了xkcdxkcd script字体(来自ipython/xkcd-font
  • 安装了Humor Sans字体(来自imkevinxu/xkcdgraphs
  • 更新了字体缓存数据库(fc-cache -fv)
  • 删除了~/.cache/matplotlib目录(整个目录)
  • 重新启动电脑
  • 验证了字体是否在~/.cache/matplotlib/fontList.json中

有什么线索可以让我让它工作吗?我感激任何提示!

问候,Micha

我使用matplotlib.org/xkcd/examples的示例代码:

from matplotlib import pyplot as plt
import numpy as np

plt.xkcd()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xticks([])
plt.yticks([])
ax.set_ylim([-30, 10])

data = np.ones(100)
data[70:] -= np.arange(30)

plt.annotate(
    'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
    xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))

plt.plot(data)

plt.xlabel('time')
plt.ylabel('my overall health')

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.bar([-0.125, 1.0-0.125], [0, 100], 0.25)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.set_xticks([0, 1])
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([0, 110])
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
plt.yticks([])

plt.title("CLAIMS OF SUPERNATURAL POWERS")

plt.show()

1
xkcd风格没有被应用(正如您在我链接的图片中所看到的那样),其他一切都运行良好。 :)但是,接受的答案解决了问题 - 我使用的是matplotlib 1.3的过时示例,而同时使用的是matplotlib 2.2。不幸的是,matplotlib的文档没有提示您正在查看哪个版本。我的错! - Micha
啊,抱歉,我没有看到链接的图片。一旦您有足够的声望,您将能够直接在问题中嵌入图片,这样就不会再发生这种情况了 :) - Graipher
1个回答

9

看起来matplotlib使用上下文的方式发生了变化。一个可行的版本应该是手动使用上下文:

with plt.xkcd():
    # your plot here
plt.show()

例如,代码将会变成下面这样:
from matplotlib import pyplot as plt
import numpy as np

with plt.xkcd():
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    plt.xticks([])
    plt.yticks([])
    ax.set_ylim([-30, 10])

    data = np.ones(100)
    data[70:] -= np.arange(30)

    plt.annotate(
        'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
        xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))

    plt.plot(data)

    plt.xlabel('time')
    plt.ylabel('my overall health')

plt.show()

这里输入图片描述

这与当前版本的示例相符。问题中链接的示例已过时


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