matplotlib的xkcd()无效

Mic*_*cha 9 python ubuntu 64-bit matplotlib

好的,我知道这个问题已经被问过很多次了,但是我没有这个问题:

我尝试在matbulotlib上使用xkcd-styleUbuntu 16.04 LTS 64位上使用python 2.7.12 64位,我使用matplotlib.org/xkcd/examples中的示例代码(见下文),但我仍然得到这个!

我做了什么

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

我有什么线索可以让这个工作?我很感激任何暗示!

问候,米哈

我使用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()
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 8

似乎matplotlib使用上下文的方式发生了变化.一个工作版本应该是手动使用上下文,

with plt.xkcd():
    # your plot here
plt.show()
Run Code Online (Sandbox Code Playgroud)

然后该示例将如下所示:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这符合当前版本的示例.问题中链接示例已过时.