Matplotlib,TypeError:ord()期望一个字符,但找到长度为0的字符串

时间:2014-03-30 02:30:07

标签: python ubuntu matplotlib

我不是python的专业用户,而且我用matplotlib编写一个(假设的)简单代码,用一列数字生成一个直方图,并用高斯拟合。

我尝试使用matplotlib.pyplot.savefig保存输出(" figure.ps"),但是我得到了这个错误:

Traceback (most recent call last):
  File "histo.py", line 85, in <module>
    plt.savefig("histo.ps")
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 471, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1185, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2021, in print_figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 469, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 421, in draw
     self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 898, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1997, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1042, in draw
    ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw, renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 993, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 747, in get_window_extent
    bbox, info = self._get_layout(self._renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 188, in get_text_width_height_descent
    renderer=self)
  File "/usr/lib/pymodules/python2.7/matplotlib/texmanager.py", line 608, in get_text_width_height_descent
    page = iter(dvi).next()
  File "/usr/lib/pymodules/python2.7/matplotlib/dviread.py", line 65, in __iter__
    have_page = self._read()
  File "/usr/lib/pymodules/python2.7/matplotlib/dviread.py", line 120, in _read
    byte = ord(self.file.read(1))
TypeError: ord() expected a character, but string of length 0 found

最奇怪的是,对于其他列或数字,它根本不会给我错误,但是给出错误的数字列并不包含特殊字符。我猜主要的错误信息是:

TypeError: ord() expected a character, but string of length 0 found

但我不明白为什么。该列现在包含3000个数字,通常我也运行此代码10000而没有问题。但是,在这种情况下,我注意到如果我将列从3000减少到2000,代码就会停止抱怨 此外,保留3000数字列,如果我没有尝试保存输出图,代码也不会崩溃。

有人可以给我一些建议吗?

1 个答案:

答案 0 :(得分:1)

错误是由乳胶编制长度为0的东西引起的。问题是,为什么会发生这种情况。

每次matplotlib生成一个需要tex的图,如果图中的乳胶项之前没有编译过,它们会被编译然后存储在〜/ .matplotlib / tex.cache /中。这样,如果你有轴标签,例如,在许多图中重复使用,matplotlib不需要担心为它们重新编译乳胶。即使您保存到与主目录所在的位置不同的装入空间,也是如此。因此,如果您的主目录中没有空间,当您尝试保存绘图时,无法添加tex缓存,从而导致上述错误。

总而言之,这是一个棘手的发现,但有时需要阅读documentation

Troubleshooting¶

  • 尝试删除.matplotlib / tex.cache目录。如果你不知道 在哪里找到.matplotlib,参见 .matplotlib目录位置