1

I connect remotely using:

ssh -X

then I have a code that has:

#! /usr/bin/env python     

import matplotlib  
matplotlib.use('Agg') 

...

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

...

fig.savefig(plotfile)

The code works until saving the plot. Then I get the following:

  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--

I think the problem has to do with saving remotely the plots, since I've used the program before and it ran fine. By searching I found the use of 'Agg' to be the solution in most cases, but it doesn't seem to work for me. Any ideas? Thanks

VGP
  • 85
  • 1
  • 6
  • Looks like a type conversion error `could not convert string to float: g--`. Try type casting. https://docs.python.org/2/library/stdtypes.html#built-in-types This may help: http://stackoverflow.com/questions/379906/parse-string-to-float-or-int – ρss Dec 17 '14 at 12:16
  • 1
    @pss, this is not good advice. What value do you expect when typecasting `g--` into a float value? To me it looks like a bug in matplotlib. – cel Dec 17 '14 at 12:50
  • 1
    @cel - It's not a bug. Matplotlib allows grayscale color specifications to be strings (e.g. `"0.1"`) for consistency with matlab. When given a sring as a color, matplotlib first checks to see if it's a color name or abbreviation it knows. If it's not, it then assumes it's a float representing a grayscale value. Thus the float conversion. What's presumably happened is that the OP inadvertently passed in a matlab-style line specifier (`"g--"`) as a color. – Joe Kington Dec 17 '14 at 19:57

1 Answers1

0

My guess is that you're probably inadvertently doing something similar to:

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

When you meant to do:

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

At any rate, matplotlib is somehow getting the string 'g--' as a color instead of just 'g'. The matlab-style plot specification g-- will work just fine, but only if passed in in the expected location to plot, not as a color.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463