使用matplotlib

时间:2016-11-19 16:35:41

标签: python matplotlib latex dvi

我正在使用matplotlib(启用了LaTex和agg后端),以便使用pcolormesh函数显示流体动力学模拟。 系统:Ubuntu 16.04 Python:Python 2.7.12 Matplotlib:1.5.1-1ubuntu1 我也在使用twinx twiny添加额外的轴在保存~300 png图像后,我在处理标题时遇到指向非工作dvi转换命令的崩溃。我总是使用pylab.close("all")以便在每次保存过程后关闭我的数字。

pylab.rc('text', usetex=True)
pylab.rc('text.latex', preamble=[r"\usepackage{amsmath}", r"\usepackage{amssymb}", r"\usepackage[alsoload=astronomy]{siunitx}"])

pylab.close("all")
f, ax = pylab.subplots(1)
cmap = pylab.cm.jet
cmap.set_bad('w',1.)
if in logquantity:
    plot = ax.pcolormesh(x, y, quantity , vmax=maxvalue, vmin=minvalue, norm=LogNorm(vmax= maxvalue, vmin = minvalue), cmap=cmap)
else:
    plot = ax.pcolormesh(x, y, quantity, vmax=maxvalue, vmin=minvalue, cmap=cmap)
ax.set_title("Title")
ax.set_aspect("equal")
ax.set_xscale("log")
ax.grid()
...<More Formatting stuff>
ax2 = ax.axes.twiny()
ax3 = ax.twinx()
...<Formatting labels, setting scales etc>
pylab.savefig("test.png", dpi = 800, bbox_inches='tight')
pylab.close("all")

pylab.savefig("test.png", dpi = 800, bbox_inches='tight')
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 689, in savefig
    fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
  File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2026, in draw_idle
    self.draw(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/text.py", line 792, in draw
    mtext=mtext)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 253, in draw_tex
    Z = texmanager.get_grey(s, size, self.dpi)
  File "/usr/lib/python2.7/dist-packages/matplotlib/texmanager.py", line 583, in get_grey
    pngfile = self.make_png(tex, fontsize, dpi)
  File "/usr/lib/python2.7/dist-packages/matplotlib/texmanager.py", line 524, in make_png
    'dvipng: \n\n' % dvifile + report)
RuntimeError: dvipng was not able to process the following file:
$HOME/.cache/matplotlib/tex.cache/2664bc1e879cb69289e1663e63a1c713.dvi
Here is the full report generated by dvipng:

No dvipng error report available.

如果我检查提到的.cache位置,我可以完美地找到提到的dvi文件。如果我在它上面使用dvipng,我会得到完全正确渲染的标题,它应该放在图中。我假设某种缓存/缓冲区正在运行。如果它只涉及使用plot,semilogy,semilogx和loglog的线图,我会在更多的图像后得到这个错误。如果anoyone认为它们与此问题相关,我还可以提供格式详细信息(或任何其他附加信息)。

提前感谢您的回答: - )

修改 字符串为$\rho$$\left[6.96e-20 \frac{\si{\gram}}{\si{\centi \meter \cubed}}\right]$, $t =\num{0.00e+00}\text{y}$, $n_R = \num{300}$, $n_{\varphi} = \num{50}$, $n_z = \num{401},\ [C.L.] = 1.14e+02\text{pc}$

Png Version of cached file 这是将缓存文件复制到另一个位置并使用dvipng手动将其转换为png时的结果。

问题并非特定于此情节。任何情节都可能发生这种情况。无论前300个图是什么,它都可以正常工作。它并不总是与崩溃相同的情节。这是调查问题的问题之一。 有一个循环遍历所有数量,然后调用函数绘制

for quantityname in quantities:
    quantitydict = quantities[quantityname]
    quantity = quantitydict["data"]

每个时间步数中有18个这样的数量。 EDIT2(更多资讯) 当运行它的机器没有太多可用的主内存(低于10GB,脚本在运行时使用大约1GB)时,更常出现错误。 此外,有时表现不同。它会出现LaTeX错误。在运行中创建的所有先前的绘图工作正常,我复制该行并在LaTeX中编译它我得到了正确的结果。错误消息:

    pylab.savefig("test.png", dpi = 800, bbox_inches='tight')
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 688, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1565, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2180, in print_figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 527, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/text.py", line 749, in draw
    bbox, info, descent = textobj._get_layout(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/text.py", line 361, in _get_layout
    ismath=ismath)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
    renderer=self)
  File "/usr/lib/python2.7/dist-packages/matplotlib/texmanager.py", line 675, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/usr/lib/python2.7/dist-packages/matplotlib/texmanager.py", line 422, in make_dvi
    report))
RuntimeError: LaTeX was not able to process the following string:
'CFL $\\\\Delta t_\\\\text{min}$$\\\\left[4.65e+05 \\\\text{yrs}\\\\right]$, $t =\\\\num{3.80e+03}\\\\text{y}$, $n_R = \\\\num{300}$, $n_{\\\\varphi} = \\\\num{50}$, $n_z = \\\\num{401},\\\\ [C.L.] = 1.14e+02\\\\text{pc}$'
Here is the full report generated by LaTeX: 

No latex error report available.

0 个答案:

没有答案