TypeError:需要一个类似字节的对象,而不是FuncAnimation的“ str”对象。我该如何解决

时间:2019-09-12 16:39:05

标签: python matplotlib

我该如何解决这个问题?

我在哪里将数据作为字符串而不是字节传递?

TypeError: a bytes-like object is required, not 'str'

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import animation
from JSAnimation import IPython_display
def solwave(t, x, c=1):
    """ Solitary wave solution of the K deV equation."""
    return c/(2*np.cosh(np.sqrt(c)*(x-c*t)/2)*2)

# Initialization
fig = plt.figure()
ax = plt.axes(xlim=(-5, 20), ylim=(0, 0.6))
line, = ax.plot([], [], lw=2)
t=np.linspace(-10,25,91)
x = np.linspace(-5, 20.0, 101)
def init():
    line.set_data([], [])
    return line,
def animate(i):
    y = solwave(t[i], x)
    line.set_data(x, y)
    return line,
animation.FuncAnimation(fig, animate, init_func=init,
frames=90, interval=30, blit=True)

import numpy as np
# Fix speed
c = 1.0
def solwave(t, x):
    """ Solitary wave solution of the K deV equation."""
    return c/(2*np.cosh(np.sqrt(c)*(x-c*t)/2)*2)
TypeError                                 Traceback (most recent call last)
D:\Software\Windows\Anaconda\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
343             method = get_real_method(obj, self.print_method)
344             if method is not None:
--> 345                 return method()
346             return None
347         else:

D:\Software\Windows\Anaconda\lib\site-packages\JSAnimation\IPython_display.py in anim_to_html(anim, fps, 
embed_frames, default_mode)
 74             anim.save(f.name,  writer=HTMLWriter(fps=fps,
 75                                                  
embed_frames=embed_frames,
---> 76                                                  
default_mode=default_mode))
 77             html = open(f.name).read()
 78 

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, 
extra_anim, savefig_kwargs, progress_callback) 1154                             progress_callback(frame_number, 
total_frames) 1155                             frame_number += 1
-> 1156                     writer.grab_frame(**savefig_kwargs) 1157  1158         # Reconnect signal for first draw if necessary

D:\Software\Windows\Anaconda\lib\contextlib.py in __exit__(self, type, 
value, traceback)
117         if type is None:
118             try:
--> 119                 next(self.gen)
120             except StopIteration:
121                 return False

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
saving(self, fig, outfile, dpi, *args, **kwargs)
230             yield self
231         finally:
--> 232             self.finish()
233 
234 

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
finish(self)
526         # are available to be assembled.
527         self._run()
--> 528         MovieWriter.finish(self)  # Will call clean-up
529 
530     def cleanup(self):

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
finish(self)
365     def finish(self):
366         '''Finish any processing for writing the movie.'''
--> 367         self.cleanup()
368 
369     def grab_frame(self, **savefig_kwargs):

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
cleanup(self)
529 
530     def cleanup(self):
--> 531         MovieWriter.cleanup(self)
532 
533         # Delete temporary files

D:\Software\Windows\Anaconda\lib\site-packages\matplotlib\animation.py in 
cleanup(self)
397         self._frame_sink().close()
398         # Use the encoding/errors that universal_newlines would use.
--> 399         out = TextIOWrapper(BytesIO(out)).read()
400         err = TextIOWrapper(BytesIO(err)).read()
401         if out:

TypeError: a bytes-like object is required, not 'str'

<matplotlib.animation.FuncAnimation at 0x1842f83d4e0>

2 个答案:

答案 0 :(得分:0)

您必须编辑D:\ Software \ Windows \ Anaconda \ lib \ site-packages \ matplotlib \ animation.py

来自

399         out = TextIOWrapper(BytesIO(out)).read()
400         err = TextIOWrapper(BytesIO(err)).read()

399         out = TextIOWrapper(BytesIO(out.encode('utf-8'))).read()
400         err = TextIOWrapper(BytesIO(err.encode('utf-8'))).read()

答案 1 :(得分:0)

如何如下编辑animation.py:

    if type(out) is str:
        out = TextIOWrapper(BytesIO(out.encode('utf-8'))).read()
        err = TextIOWrapper(BytesIO(err.encode('utf-8'))).read()
    else:
        out = TextIOWrapper(BytesIO(out)).read()
        err = TextIOWrapper(BytesIO(err)).read()