Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"float() argument must be a string or a number" when saving a png #2658

Closed
jankatins opened this issue Dec 6, 2013 · 4 comments
Closed

"float() argument must be a string or a number" when saving a png #2658

jankatins opened this issue Dec 6, 2013 · 4 comments

Comments

@jankatins
Copy link
Contributor

This throws an error with the png backend (save as pdf works):

layer = {'color': '#E69F00', 'linewidth': [91.489361702127653, 91.489361702127653, 94.893617021276583, 
                                           66.808510638297875, 76.17021276595743, 64.255319148936181, 
                                           80.425531914893611], 'linestyle': 'dashed'}
x = [2.6200000000000001, 2.875, 3.2149999999999999, 3.46, 3.4399999999999999, 3.4399999999999999, 2.77]
y = [21.0, 21.0, 21.399999999999999, 18.100000000000001, 19.199999999999999, 17.800000000000001, 19.699999999999999]
import matplotlib.pyplot as plt
plt.subplot(1, 1, 1)
plt.plot(x, y, **layer)
#plt.show()
plt.gcf().savefig("test_manual.png")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-ef603f1194b6> in <module>()
      2 plt.plot(x, y, **layer)
      3 #plt.show()
----> 4 plt.gcf().savefig("test_manual.png")

C:\portabel\Python27\lib\site-packages\matplotlib\figure.pyc in savefig(self, *args, **kwargs)
   1419             self.set_frameon(frameon)
   1420 
-> 1421         self.canvas.print_figure(*args, **kwargs)
   1422 
   1423         if frameon:

C:\portabel\Python27\lib\site-packages\matplotlib\backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2218                 orientation=orientation,
   2219                 bbox_inches_restore=_bbox_inches_restore,
-> 2220                 **kwargs)
   2221         finally:
   2222             if bbox_inches and restore_bbox:

C:\portabel\Python27\lib\site-packages\matplotlib\backends\backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    503 
    504     def print_png(self, filename_or_obj, *args, **kwargs):
--> 505         FigureCanvasAgg.draw(self)
    506         renderer = self.get_renderer()
    507         original_dpi = renderer.dpi

C:\portabel\Python27\lib\site-packages\matplotlib\backends\backend_agg.pyc in draw(self)
    449 
    450         try:
--> 451             self.figure.draw(self.renderer)
    452         finally:
    453             RendererAgg.lock.release()

C:\portabel\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

C:\portabel\Python27\lib\site-packages\matplotlib\figure.pyc in draw(self, renderer)
   1032         dsu.sort(key=itemgetter(0))
   1033         for zorder, a, func, args in dsu:
-> 1034             func(*args)
   1035 
   1036         renderer.close_group('figure')

C:\portabel\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

C:\portabel\Python27\lib\site-packages\matplotlib\axes.pyc in draw(self, renderer, inframe)
   2084 
   2085         for zorder, a in dsu:
-> 2086             a.draw(renderer)
   2087 
   2088         renderer.close_group('axes')

C:\portabel\Python27\lib\site-packages\matplotlib\artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

C:\portabel\Python27\lib\site-packages\matplotlib\lines.pyc in draw(self, renderer)
    560                         drawFunc(pe_renderer, gc, tpath, affine_frozen)
    561                 else:
--> 562                     drawFunc(renderer, gc, tpath, affine.frozen())
    563 
    564         if self._marker:

C:\portabel\Python27\lib\site-packages\matplotlib\lines.pyc in _draw_lines(self, renderer, gc, path, trans)
    936 
    937     def _draw_lines(self, renderer, gc, path, trans):
--> 938         self._lineFunc(renderer, gc, path, trans)
    939 
    940     def _draw_steps_pre(self, renderer, gc, path, trans):

C:\portabel\Python27\lib\site-packages\matplotlib\lines.pyc in _draw_dashed(self, renderer, gc, path, trans)
    983             gc.set_dashes(0, self._dashSeq)
    984 
--> 985         renderer.draw_path(gc, path, trans)
    986 
    987     def _draw_dash_dot(self, renderer, gc, path, trans):

C:\portabel\Python27\lib\site-packages\matplotlib\backends\backend_agg.pyc in draw_path(self, gc, path, transform, rgbFace)
    143                 self._renderer.draw_path(gc, p, transform, rgbFace)
    144         else:
--> 145             self._renderer.draw_path(gc, path, transform, rgbFace)
    146 
    147     def draw_mathtext(self, gc, x, y, s, prop, angle):

TypeError: float() argument must be a string or a number

<matplotlib.figure.Figure at 0x176f7128>
@efiring
Copy link
Member

efiring commented Dec 7, 2013

I don't understand; it is an error. Linewidth is a scalar, but you are feeding it a list. The odd thing is that pdf does not raise an exception.

@jankatins
Copy link
Contributor Author

So, something like this is not possible in matplotlib: http://docs.ggplot2.org/current/geom_line-22.png (from http://docs.ggplot2.org/current/geom_line.html -> forth image from bottom)

I only found this: http://stackoverflow.com/questions/19390895/matplotlib-plot-with-variable-line-width which suggest adding multiple lines for each part.

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
width = [.5, 1, 4.5, 1.75, 1.75]

for i in range(len(x)-1):
    plt.plot(x[i:i+2], y[i:i+2], linewidth=width[i])
plt.show()

-> doesn't look really good as the lines overlap and have steps in between the values.

@efiring
Copy link
Member

efiring commented Dec 7, 2013

Correct, mpl does not have a variable line width plotting method.

@efiring efiring closed this as completed Dec 7, 2013
jankatins added a commit to jankatins/ggplot that referenced this issue Dec 7, 2013
Currently, matplotlib does not allow for different sizes of one line
(see matplotlib/matplotlib#2658) so adding
a size mapping will not work (and produces an error on png devices
and does nothing on pdf devices).

Add a workaround in geom_line to detect such cases, print a warning
and set the size to a hardcoded value of "4" (can be changed by
'+ geom_line(size=x)'.

Also add updated baseline images for the corresponding tests.
@pelson
Copy link
Member

pelson commented Jan 8, 2014

Correct, mpl does not have a variable line width plotting method.

Other than using a LineCollection, which does not have the same interface as plt.plot.

has2k1 pushed a commit to has2k1/plotnine that referenced this issue Apr 25, 2017
Currently, matplotlib does not allow for different sizes of one line
(see matplotlib/matplotlib#2658) so adding
a size mapping will not work (and produces an error on png devices
and does nothing on pdf devices).

Add a workaround in geom_line to detect such cases, print a warning
and set the size to a hardcoded value of "4" (can be changed by
'+ geom_line(size=x)'.

Also add updated baseline images for the corresponding tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants