gpt4 book ai didi

python - Matplotlib,类型错误 : ord() expected a character, 但找到长度为 0 的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:46 26 4
gpt4 key购买 nike

我不是 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 个数字列,如果我不尝试保存输出图,代码也不会崩溃。

有谁可以给​​我一些建议吗?

最佳答案

该错误是由于 Latex 编译长度为 0 的内容而引起的。问题是,为什么会发生这种情况。

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

总而言之,这是一个棘手的发现,但有时阅读 documentation 是值得的。 :

疑难解答¶

  • 尝试删除 .matplotlib/tex.cache 目录。如果你不知道在哪里可以找到 .matplotlib,请参阅.matplotlib 目录位置
  • 关于python - Matplotlib,类型错误 : ord() expected a character, 但找到长度为 0 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739572/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com