远程保存绘图的问题

时间:2014-12-17 11:23:01

标签: python matplotlib

我使用以下方式远程连接:

  

ssh -X

然后我有一个代码:

#! /usr/bin/env python     

import matplotlib  
matplotlib.use('Agg') 

...

fig = plt.figure(figsize=(8.5,9.)) 

...

fig.savefig(plotfile)

代码一直有效,直到保存图表。然后我得到以下内容:

  Traceback (most recent call last):
  File "./lf_compared.py", line 171, in <module>
    fig.savefig(plotfile)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1470, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 2194, in print_figure
    **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/backends/backend_ps.py", line 989, in print_ps
    return self._print_ps(outfile, 'ps', *args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/backends/backend_ps.py", line 1020, in _print_ps
    **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/backends/backend_ps.py", line 1110, in _print_figure
    self.figure.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1079, in draw
    func(*args)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/axes/_base.py", line 2092, in draw
    a.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/legend.py", line 462, in draw
    self._legend_box.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/offsetbox.py", line 273, in draw
    c.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/offsetbox.py", line 273, in draw
    c.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/offsetbox.py", line 273, in draw
    c.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/offsetbox.py", line 273, in draw
    c.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/offsetbox.py", line 814, in draw
    self._text.draw(renderer)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/text.py", line 554, in draw
    gc.set_foreground(self.get_color())
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/backend_bases.py", line 991, in set_foreground
    self._rgb = colors.colorConverter.to_rgba(fg)
  File "/cosma/local/matplotlib/1.4.2/lib/python/matplotlib-1.4.2-py2.7-linux-x86_64.egg/matplotlib/colors.py", line 376, in to_rgba
    'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
ValueError: to_rgba: Invalid rgba arg "g--"
to_rgb: Invalid rgb arg "g--"
could not convert string to float: g--

我认为这个问题与远程保存图表有关,因为我以前使用过该程序并且运行正常。通过搜索我发现在大多数情况下使用'Agg'作为解决方案,但它似乎对我不起作用。有任何想法吗?感谢

1 个答案:

答案 0 :(得分:0)

我的猜测是你可能无意中做了类似的事情:

plt.plot(x, y, color='g--')

当你打算做的时候:

plt.plot(x, y, 'g--')

无论如何,matplotlib以某种方式将字符串'g--'作为颜色而不仅仅是'g'。 matlab风格的绘图规范g--可以正常工作,但只有在预期位置传递给plot时才能正常工作,而不是作为颜色。