1

嘿嘿,

我正在使用与 macports 一起分发的最新版本(1.2.0)的 matplotlib。我遇到了运行此代码的 AssertionError(我猜是内部测试引起的)

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

X,Y = np.meshgrid(np.arange(0, 2*np.pi, .2), np.arange(0, 2*np.pi, .2))
U = np.cos(X)
V = np.sin(Y)

Q = plt.quiver(U, V)
plt.quiverkey(Q, 0.5, .9, 1., 'Label')

plt.gca().add_patch(plt.Circle((10, 10), 1))
plt.savefig('test.pdf')

我需要此代码的三个部分来重现错误:

  1. quiver plot 必须有一个用 quiver key 创建的键
  2. 必须向当前轴添加额外的补丁
  3. 我必须将图形保存为 PDF(我可以很好地显示它)

该错误不依赖于后端。我得到的回溯读取

Traceback (most recent call last):
  File "./test_quiver.py", line 15, in <module>
    plt.savefig('test.pdf')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 472, in savefig
    return fig.savefig(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2093, in print_figure
    **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1845, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2301, in print_pdf
    self.figure.draw(renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 999, in draw
    func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/quiver.py", line 306, in draw
    self.vector.draw(renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/collections.py", line 755, in draw
    return Collection.draw(self, renderer)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/collections.py", line 259, in draw
    self._offset_position)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 1548, in draw_path_collection
    output(*self.gc.pop())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2093, in pop
    assert self.parent is not None
AssertionError

如果它很重要:我在 Mac OS X 10.7.5 上,使用 python 2.7.3 和 matplotlib 1.2.0。你也得到这个错误吗?它是 matplotlib 中的错误吗?它依赖于系统吗?有一些解决方法吗?

4

2 回答 2

2

您可以保存为 eps 或 svg 并转换为 pdf。我发现生成小 pdf 文件的最佳方法是在 matplotlib 中另存为 eps,然后使用 epstopdf。

svg 也可以正常工作,您可以使用 Inkscape 转换为 pdf。svg 的一个副作用是文本被转换为路径(没有嵌入字体),这在某些情况下可能是可取的。

于 2012-12-14T10:14:47.623 回答
0

与 Ubuntu 13.04(稀有)一起分发的 matplotlib(v 1.2.1)也有这个错误。我不知道在新版本中它是否仍然是一个问题。

另一个解决方法(似乎对我有用)是完全draw_path_collection删除.../matplotlib/backends/backend_pdf.py.

于 2013-08-15T03:46:23.063 回答