【问题标题】:matplotlib's xkcd() not workingmatplotlib 的 xkcd() 不工作
【发布时间】:2018-03-21 11:07:34
【问题描述】:

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

我尝试在 Ubuntu 16.04 LTS 64-bit 上的 ma​​tplotlib 中使用 xkcd-stylepython 2.7.12 64-位,我使用来自matplotlib.org/xkcd/examples 的示例代码(见下文),但我仍然得到this

我已经做了什么

  • 通过 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()

【问题讨论】:

  • 未应用 xkcd 样式(如您在我的 linked image 中所见),其他一切正常。 :) 但问题已通过接受的答案解决 - 我在使用 matplotlib 2.2 时使用了 matplotlib 1.3 的过时示例。可悲的是,matplotlib 的文档并没有给出您正在查看哪个版本的线索。我的错!
  • 啊,对不起,我没有看到链接的图像。一旦您有足够的声誉,您就可以直接在问题中嵌入图像,这样就不会再发生这种情况了:)

标签: python ubuntu matplotlib 64-bit


【解决方案1】:

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()

这符合current version of the exampleexample linked to in the question 已过时。

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 2013-11-08
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2011-09-14
    • 2021-07-06
    相关资源
    最近更新 更多