matplotlib动画IOError [Error32]管道损坏(使用anaconda)

时间:2016-04-03 08:26:28

标签: python animation matplotlib ioerror

尝试创建动画并将其保存到文件时,我收到了该错误。

我的代码:

def animate_scatter(x_val, y_val, z_vals,
                        divisions, name, z_max, z_min, units, radius):
    """
    Specific animator.
    """
    try:
        z_val = decompress(z_vals.pop(0), level=settings.medium_comp_level)
    except IndexError:
        return
    plt.cla()
    plt.clf()
    rad = float(radius*1.3)/divisions
    size = (rad/4)**2
    x_min = min(x_val)-rad*1.1
    x_max = max(x_val)+rad*1.1
    y_min = min(y_val)-rad*1.1
    y_max = max(y_val)+rad*1.1
    plt.xlim((x_min, x_max))
    plt.ylim((y_min, y_max))
    #plt.suptitle(name)
    plt.scatter(x_val, y_val, s=size, c=z_val, marker='s',
                vmin=z_min, vmax=z_max)
    plt.gca().invert_yaxis()
    cb = plt.colorbar()
    plt.xlabel("x [pixels]")
    plt.ylabel("y [pixels]")
    cb.set_label(units)

def create_scatter_animation(x_val, y_val, z_vals_avg, divisions, z_max, z_min, units, name, radius=800, fps=15):
    """
    Creates animation from data.
    """
    fig = plt.figure()
    frames = len(z_vals_avg)
    def animate(dummy_frame):
        """
        Wrapper.
        """
        animate_scatter(x_val, y_val, z_vals_avg,
                            divisions, name, z_max, z_min, units, radius)
    anim = animation.FuncAnimation(fig, animate, frames=frames)
    anim.save(name, writer=WRITER, fps=fps)


def import_and_plot(source, radius=None, level=9):
    """
    Imports data from a compressed file and plots it.
    """
    src = open(source, 'r')
    values = decompress(src.read(), level=level)
    #[x_val, y_val, z_vals_avg, divisions, z_max, z_min, units]
    x_val, y_val, z_vals_avg = values[0], values[1], values[2]
    divisions = values[3]
    z_max, z_min = values[4], values[5]
    units = values[6]
    src.close()
    create_scatter_animation(x_val, y_val, z_vals_avg, divisions, z_max, z_min, units, source)

输出:

Traceback (most recent call last):
  File "plot_data.py", line 5, in <module>
    plot()
  File "/home/jaime/Dropbox/TFG/rod-analysis/4090_42Hz/methods.py", line 795, in plot_all_data_files
    import_and_plot(file_)
  File "/home/jaime/Dropbox/TFG/rod-analysis/4090_42Hz/methods.py", line 773, in import_and_plot
    create_scatter_animation(x_val, y_val, z_vals_avg, divisions, z_max, z_min, units, source)
  File "/home/jaime/Dropbox/TFG/rod-analysis/4090_42Hz/methods.py", line 758, in create_scatter_animation
    anim.save(name, writer=WRITER, fps=fps)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/animation.py", line 810, in save
    writer.grab_frame(**savefig_kwargs)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/animation.py", line 230, in grab_frame
    dpi=self.dpi, **savefig_kwargs)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 1565, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 196, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2232, in print_figure
    **kwargs)
  File "/home/jaime/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 519, in print_raw
    fileobj.write(renderer._renderer.buffer_rgba())
IOError: [Errno 32] Broken pipe

我在"IOError: [Errno 32] Broken pipe" when saving animation files in anaconda python尝试过解决方案  但它对我不起作用。我也在ubuntu中使用anaconda。

_writer = animation.writers['ffmpeg']
WRITER = _writer(fps=15, metadata=dict(artist='name'), bitrate=1800)

0 个答案:

没有答案