升级matplotlib后matplotlib rgb颜色不起作用

时间:2017-06-13 15:29:19

标签: python matplotlib jupyter-notebook

我刚将matplotlib升级到matplotlib2.0。到目前为止,我看不出在基本用途中应该有任何改变,但我现在有了这个奇怪的用rgb编码:

myDF_DoMS.mean().plot(color =(0.2,0.2,0.7),xticks=np.arange(1,31,1))
plt.plot([1,32],[zeMeanS,zeMeanS],color=(0.2,0.7,0.9))
plt.xlabel('xlabel')
plt.ylabel('Some Score')
plt.title(Study+"\n A name")
plt.show()

来自内核的投诉:

ValueError:无效的RGBA参数:0.2

(下面的完整错误堆栈)

突然0.2不再是浮动了! 如果我把''有用 而在第2行,另一个' rgb'第2行的列表工作正常(颜色=(0.2,0.7,0.9))...

我有点迷失了。

设定: 用于完美工作的jupyter笔记本中的单元格的副本。 在anaconda环境中的Python 2.7。 Windows 10作为操作系统。

以下是输出:

 ValueError                                Traceback (most recent call
 last) C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\IPython\core\formatters.pyc
 in __call__(self, obj)
     305                 pass
     306             else:
 --> 307                 return printer(obj)
     308             # Finally look for special method names
     309             method = get_real_method(obj, self.print_method)

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\IPython\core\pylabtools.pyc
 in <lambda>(fig)
     238 
     239     if 'png' in formats:
 --> 240         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
     241     if 'retina' in formats or 'png2x' in formats:
     242         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\IPython\core\pylabtools.pyc
 in print_figure(fig, fmt, bbox_inches, **kwargs)
     122 
     123     bytes_io = BytesIO()
 --> 124     fig.canvas.print_figure(bytes_io, **kw)
     125     data = bytes_io.getvalue()
     126     if fmt == 'svg':

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\backend_bases.pyc
 in print_figure(self, filename, dpi, facecolor, edgecolor,
 orientation, format, **kwargs)    2198                    
 orientation=orientation,    2199                     dryrun=True,
 -> 2200                     **kwargs)    2201                 renderer = self.figure._cachedRenderer    2202                 bbox_inches = self.figure.get_tightbbox(renderer)

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\backends\backend_agg.pyc
 in print_png(self, filename_or_obj, *args, **kwargs)
     543 
     544     def print_png(self, filename_or_obj, *args, **kwargs):
 --> 545         FigureCanvasAgg.draw(self)
     546         renderer = self.get_renderer()
     547         original_dpi = renderer.dpi

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\backends\backend_agg.pyc
 in draw(self)
     462 
     463         try:
 --> 464             self.figure.draw(self.renderer)
     465         finally:
     466             RendererAgg.lock.release()

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\artist.pyc
 in draw_wrapper(artist, renderer, *args, **kwargs)
      61     def draw_wrapper(artist, renderer, *args, **kwargs):
      62         before(artist, renderer)
 ---> 63         draw(artist, renderer, *args, **kwargs)
      64         after(artist, renderer)
      65 

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\figure.pyc
 in draw(self, renderer)    1142     1143            
 mimage._draw_list_compositing_images(
 -> 1144                 renderer, self, dsu, self.suppressComposite)    1145     1146             renderer.close_group('figure')

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\image.pyc
 in _draw_list_compositing_images(renderer, parent, dsu,
 suppress_composite)
     137     if not_composite or not has_images:
     138         for zorder, a in dsu:
 --> 139             a.draw(renderer)
     140     else:
     141         # Composite any adjacent images together

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\artist.pyc
 in draw_wrapper(artist, renderer, *args, **kwargs)
      61     def draw_wrapper(artist, renderer, *args, **kwargs):
      62         before(artist, renderer)
 ---> 63         draw(artist, renderer, *args, **kwargs)
      64         after(artist, renderer)
      65 

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\axes\_base.pyc
 in draw(self, renderer, inframe)    2424            
 renderer.stop_rasterizing()    2425 
 -> 2426         mimage._draw_list_compositing_images(renderer, self, dsu)    2427     2428         renderer.close_group('axes')

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\image.pyc
 in _draw_list_compositing_images(renderer, parent, dsu,
 suppress_composite)
     137     if not_composite or not has_images:
     138         for zorder, a in dsu:
 --> 139             a.draw(renderer)
     140     else:
     141         # Composite any adjacent images together

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\artist.pyc
 in draw_wrapper(artist, renderer, *args, **kwargs)
      61     def draw_wrapper(artist, renderer, *args, **kwargs):
      62         before(artist, renderer)
 ---> 63         draw(artist, renderer, *args, **kwargs)
      64         after(artist, renderer)
      65 

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\lines.pyc
 in draw(self, renderer)
     801                 self._set_gc_clip(gc)
     802 
 --> 803                 ln_color_rgba = self._get_rgba_ln_color()
     804                 gc.set_foreground(ln_color_rgba, isRGBA=True)
     805                 gc.set_alpha(ln_color_rgba[3])

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\lines.pyc
 in _get_rgba_ln_color(self, alt)    1342     1343     def
 _get_rgba_ln_color(self, alt=False):
 -> 1344         return mcolors.to_rgba(self._color, self._alpha)    1345     1346     # some aliases....

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\colors.pyc
 in to_rgba(c, alpha)
     141         rgba = _colors_full_map.cache[c, alpha]
     142     except (KeyError, TypeError):  # Not in cache, or unhashable.
 --> 143         rgba = _to_rgba_no_colorcycle(c, alpha)
     144         try:
     145             _colors_full_map.cache[c, alpha] = rgba

 C:\Program
 Files\Anaconda2\envs\moonshade\lib\site-packages\matplotlib\colors.pyc
 in _to_rgba_no_colorcycle(c, alpha)
     192         # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
     193         # Test dimensionality to reject single floats.
 --> 194         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
     195     # Return a tuple to prevent the cached value from being modified.
     196     c = tuple(c.astype(float))

 ValueError: Invalid RGBA argument: 0.2

 <matplotlib.figure.Figure at 0x14e54ac8>

1 个答案:

答案 0 :(得分:0)

如果要绘制的数据框有3列,则颜色规范为color=(0.2,0.2,0.7)不明确。它可以被解释为颜色元组,在这种情况下0.2将是第一列绘制的颜色。但是,0.2毕竟不是有效的颜色,因此也就是错误。

选项是使用

color=matplotlib.colors.to_hex((0.2,0.2,0.9))

或直接使用十六进制等值

color="#3333e5"

顺便说一句,我不认为matplotlib版本之间的这种行为已经发生了变化,所以可能只是你现在使用不同的数据帧(?)。