savefig()导致ValueError:RGBA值应在0-1范围内

时间:2017-08-08 10:19:06

标签: python python-3.x matplotlib

我正在尝试创建一个程序:

  1. 从用户选择的csv文件中检索数据
  2. 绘制数据
  3. 在窗口上显示图形
  4. 允许用户更改绘图参数(color,linestyle ...)
  5. 将绘图保存在用户以PS格式
  6. 选择的目录中

    步骤1到4工作正常,但第5步让我觉得我已经杀了matplotlib ......

    我使用与之前版本相同的语法进行保存,效果很好:

            def savePlot(self):
        self.saveDirectory = QFileDialog.getExistingDirectory(None, 'Chose where you want to save','/home',QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
    
        if self.plotParam==[]:
            plt.savefig(self.saveDirectory + "/" + "Untiltled.ps", dpi = 1200)
    
        else :
            plt.savefig(self.saveDirectory + "/" + "%s.ps" %self.plotParam[0],format = 'ps', dpi = 1200)
    

    Here是完整代码的链接,here是我正在使用的数据文件。

    在此部分中,self.plotParam是一个最初为空的列表,如果用户想要更改默认值,则包含绘图参数。

    以下是错误日志:

        Traceback (most recent call last):
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 141, in to_rgba
            rgba = _colors_full_map.cache[c, alpha]
        TypeError: unhashable type: 'numpy.ndarray'
    
        During handling of the above exception, another exception occurred:
    
        Traceback (most recent call last):
          File "/home/antoine/CERB/courbes/courbesv2.py", line 354, in savePlot
            plt.savefig(self.saveDirectory + "/" + "essai.ps", dpi = 1200)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 697, in savefig
            res = fig.savefig(*args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1573, in savefig
            self.canvas.print_figure(*args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 222, in print_figure
            FigureCanvasAgg.print_figure(self, *args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2252, in print_figure
    **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 957, in print_ps
            return self._print_ps(outfile, 'ps', *args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 988, in _print_ps
    **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 1078, in _print_figure
            self.figure.draw(renderer)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
            draw(artist, renderer, *args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1144, in draw
    renderer, self, dsu, self.suppressComposite)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
            a.draw(renderer)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
            draw(artist, renderer, *args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2426, in draw
            mimage._draw_list_compositing_images(renderer, self, dsu)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
            a.draw(renderer)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
            draw(artist, renderer, *args, **kwargs)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 354, in draw
            self._offset_position)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backends/backend_ps.py", line 607, in draw_path_collection
    offset_position)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 309, in draw_path_collection
    antialiaseds, urls, offset_position):
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 497, in _iter_collection
            gc0.set_foreground(fg)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 1027, in set_foreground
            self._rgb = colors.to_rgba(fg)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 143, in to_rgba
            rgba = _to_rgba_no_colorcycle(c, alpha)
          File "/home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 204, in _to_rgba_no_colorcycle
            raise ValueError("RGBA values should be within 0-1 range")
        ValueError: RGBA values should be within 0-1 range
    

    我尝试过使用与.ps不同的扩展程序,并且所有这些扩展程序都显示相同的错误,除了.png,这为我节省了令人作呕的糟糕图像,即使它应该是1200 dpi(我无法向您发送保存图片的链接,因为我没有足够的声誉,抱歉。

    我也试过做一条干净的路径"使用os.path.join,但它没有改变任何内容,所以我恢复了以前版本的语法。

    我知道我的代码很可能不是最理想的,但我正在学习,所以它无法得到帮助...

    你有解决这个问题的线索吗?

    编辑:添加了最小,完整且可验证的示例:

        import matplotlib.pyplot as plt
    
        data = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2]]
    
        def plotAttempt():
            plt.errorbar(data[0],data[1],data[2],0)
            path = '/home/PlotAttempt.ps'
            plt.savefig(path)
    

    通过控制台调用plotAttempt()会得到相同的错误日志

1 个答案:

答案 0 :(得分:0)

这似乎是一个pyzo bug,当我通过控制台运行它时,代码工作正常,重新启动后它也可用于pyzo。