首页 > 解决方案 > Matplotlib 2.2.2 无法 xkcdify 绘图

问题描述

我将 matplotlib 2.2.2 与 Python 3.5.2 一起使用

当我尝试使用 matplotlib 的 xkcd 函数时,我得到一个正常的图形:

import matplotlib
print(matplotlib.get_cachedir())
print(matplotlib.__version__)

import matplotlib.font_manager
x = [f.name for f in matplotlib.font_manager.fontManager.ttflist if f.name.startswith("Humor")]
print(set(x))

from matplotlib import pyplot as plt
import numpy as np

plt.xkcd(scale=10, length=100, randomness=20)
plt.plot(np.sin(np.linspace(0, 10)))
plt.title('Whoo Hoo!!!')
plt.show()

输出:

/home/thatsme/.cache/matplotlib
2.2.2
{'Humor Sans'}

因此,安装了 Humor Sans,我删除了字体缓存,以防万一,正如其他线程所建议的那样,我增加了 jitter 参数,但仍然没有 xkcdification 的迹象:

在此处输入图像描述

奇怪的是,我在 Ubuntu 16.04 和 Windows 10 上将 Eclipse 作为双启动安装,并且 xkcd 功能在两个系统上都失败了。我原以为这些系统之间的 Python 和 Eclipse 安装有很大不同,但我们在这里。
任何想法,可能是什么问题?

Edit: As Hans pointed out this might be a problem specific for matplotlib 2.2.2
Even more bizarre - if I run the same script using with : without any other changes like

with plt.xkcd(scale=10, length=100, randomness=20):
    plt.plot(np.sin(np.linspace(0, 10)))
    plt.title('Whoo Hoo!!!')
plt.show()

the output is the expected xkcd figure: 在此处输入图像描述

Removing the with : statement brings back the first figure. The same behaviour again on both Linux and Windows, so seemingly a matplotlib version problem.

Edit: Now that we know it is a temporary, limited bug, back to the main question - How to get rid of the white edgecolor in xkcd

标签: pythonpython-3.xmatplotlib

解决方案


This is a bug purely present in matplotlib 2.2. The problem is that the garbage collector is too quick. See this comment and below.

这意味着目前您需要在上下文中使用 xkcd 样式才能使其工作。该问题应在下一个版本中修复。因此,问题中的代码在当前开发版本中按预期工作。


推荐阅读