在matplotlib 1.20中显示quvier键和补丁的bug的解决方法

时间:2012-12-14 10:03:52

标签: python numpy matplotlib

HeJ小鼠,

我正在使用与macports一起发布的matplotlib的最新版本(1.2.0)。我遇到了运行此代码的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. 箭袋图必须有一个用箭头键创建的密钥
  2. 必须为当前轴添加额外的补丁
  3. 我必须将图形保存为PDF(我可以很好地显示)
  4. 该错误不依赖于后端。我得到的回溯读取

    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中的错误吗?它是系统依赖的吗?有一些解决方法吗?

2 个答案:

答案 0 :(得分:2)

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

svg也可以,你可以使用Inkscape转换为pdf。 svg的副作用是文本被转换为路径(没有嵌入字体),这在某些情况下可能是合乎需要的。

答案 1 :(得分:0)

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

另一种解决方法(似乎对我有用)是完全删除draw_path_collection中的.../matplotlib/backends/backend_pdf.py功能。