首页 > 解决方案 > 如何纠正python填充之间的错误,TypeError:'PolyCollection'对象不可迭代

问题描述

下面的代码片段打印出 fill_between 命令的 x、y1 和 y2 输入的类型和形状。我收到以下运行时错误“TypeError:'PolyCollection' 对象不可迭代”。我该如何纠正?

        x_fill = x_fill.reshape((n_steps_fcst, ))
        y1_fill = y1_fill.reshape((n_steps_fcst, ))
        y2_fill = y2_fill.reshape((n_steps_fcst, ))

        print('type(x_fill): ', type(x_fill), ', x_fill.shape: ', x_fill.shape)
        print('type(y1_fill): ', type(y1_fill), ', y1_fill.shape: ', y1_fill.shape)
        print('type(y2_fill): ', type(y2_fill), ', y2_fill.shape: ', y2_fill.shape)

        ax_y.append(fig.add_subplot(ny, n_columns, iy * n_columns + 1))

        lin, = ax_y[iy].fill_between(x_fill, y1_fill, y2_fill)

输出是

type(x_fill):  <class 'numpy.ndarray'> , x_fill.shape:  (99,)
type(y1_fill):  <class 'numpy.ndarray'> , y1_fill.shape:  (99,)
type(y2_fill):  <class 'numpy.ndarray'> , y2_fill.shape:  (99,)

回溯(最近一次通话最后):

  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Documents/git_spork/Project/time_series_plot_2.py", line 371, in <module>
    main()
  File "C:/Users/Documents/git_spork/Project/time_series_plot_2.py", line 276, in main
    lin, = ax_y[iy].fill_between(x_fill, y1_fill, y2_fill)

TypeError: 'PolyCollection' object is not iterable

标签: pythonmatplotlibruntime-error

解决方案


推荐阅读