- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
"-6ren"> "-我试图通过重现 this website 中的代码来保存动画图.然后我尝试从 another website 复制代码这很相似但得到了相同的消息错误“Invalid file object: ” im-6ren">
我试图通过重现 this website 中的代码来保存动画图.然后我尝试从 another website 复制代码这很相似但得到了相同的消息错误“Invalid file object: <_io.BufferedReader name=76>
”
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_line(num, data, line):
line.set_data(data[..., :num])
return line,
# Fixing random state for reproducibility
np.random.seed(19680801)
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fig1 = plt.figure()
data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
line_ani.save('lines.mp4', writer=writer)
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
im_ani.save('im.mp4', writer=writer)
得到如下错误信息:
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:3: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
The backend was *originally* set to 'module://ipykernel.pylab.backend_inline' by the following code:
File "/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
self.io_loop.start()
File "/anaconda3/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 127, in start
self.asyncio_loop.run_forever()
File "/anaconda3/lib/python3.6/asyncio/base_events.py", line 422, in run_forever
self._run_once()
File "/anaconda3/lib/python3.6/asyncio/base_events.py", line 1432, in _run_once
handle._run()
File "/anaconda3/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/anaconda3/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 117, in _handle_events
handler_func(fileobj, events)
File "/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2666, in run_cell
self.events.trigger('post_run_cell', result)
File "/anaconda3/lib/python3.6/site-packages/IPython/core/events.py", line 88, in trigger
func(*args, **kwargs)
File "/anaconda3/lib/python3.6/site-packages/ipykernel/pylab/backend_inline.py", line 160, in configure_once
activate_matplotlib(backend)
File "/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 311, in activate_matplotlib
matplotlib.pyplot.switch_backend(backend)
File "/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 229, in switch_backend
matplotlib.use(newbackend, warn=False, force=True)
File "/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1305, in use
reload(sys.modules['matplotlib.backends'])
File "/anaconda3/lib/python3.6/importlib/__init__.py", line 166, in reload
_bootstrap._exec(spec, module)
File "/anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 14, in <module>
line for line in traceback.format_stack()
This is separate from the ipykernel package so we can avoid doing imports until
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-97bf2a96714e> in <module>()
29 line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
30 interval=50, blit=True)
---> 31 line_ani.save('lines.mp4', writer=writer)
32
33 fig2 = plt.figure()
/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
1258 # TODO: See if turning off blit is really necessary
1259 anim._draw_next_frame(d, blit=False)
-> 1260 writer.grab_frame(**savefig_kwargs)
1261
1262 # Reconnect signal for first draw if necessary
/anaconda3/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
97 value = type()
98 try:
---> 99 self.gen.throw(type, value, traceback)
100 except StopIteration as exc:
101 # Suppress StopIteration *unless* it's the same exception that
/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in saving(self, fig, outfile, dpi, *args, **kwargs)
235 yield self
236 finally:
--> 237 self.finish()
238
239
/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in finish(self)
367 def finish(self):
368 '''Finish any processing for writing the movie.'''
--> 369 self.cleanup()
370
371 def grab_frame(self, **savefig_kwargs):
/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in cleanup(self)
406 def cleanup(self):
407 '''Clean-up and collect the process used to write the movie file.'''
--> 408 out, err = self._proc.communicate()
409 self._frame_sink().close()
410 verbose.report('MovieWriter -- '
/anaconda3/lib/python3.6/subprocess.py in communicate(self, input, timeout)
841
842 try:
--> 843 stdout, stderr = self._communicate(input, endtime, timeout)
844 finally:
845 self._communication_started = True
/anaconda3/lib/python3.6/subprocess.py in _communicate(self, input, endtime, orig_timeout)
1503 selector.register(self.stdin, selectors.EVENT_WRITE)
1504 if self.stdout:
-> 1505 selector.register(self.stdout, selectors.EVENT_READ)
1506 if self.stderr:
1507 selector.register(self.stderr, selectors.EVENT_READ)
/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data)
349
350 def register(self, fileobj, events, data=None):
--> 351 key = super().register(fileobj, events, data)
352 poll_events = 0
353 if events & EVENT_READ:
/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data)
235 raise ValueError("Invalid events: {!r}".format(events))
236
--> 237 key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
238
239 if key.fd in self._fd_to_key:
/anaconda3/lib/python3.6/selectors.py in _fileobj_lookup(self, fileobj)
222 """
223 try:
--> 224 return _fileobj_to_fd(fileobj)
225 except ValueError:
226 # Do an exhaustive search.
/anaconda3/lib/python3.6/selectors.py in _fileobj_to_fd(fileobj)
37 except (AttributeError, TypeError, ValueError):
38 raise ValueError("Invalid file object: "
---> 39 "{!r}".format(fileobj)) from None
40 if fd < 0:
41 raise ValueError("Invalid file descriptor: {}".format(fd))
ValueError: Invalid file object: <_io.BufferedReader name=76>
我尝试运行其他在线可用的动画图形代码,但遇到了同样的错误。我更新了ffmpeg
的版本但它没有帮助。我该怎么办?
最佳答案
在 OSX 中我通过以下方式解决了它:
1) 通过 brew 安装 ffmpeg
brew install ffmpeg
2) 也通过 brew 升级 ffmpeg
brew upgrade ffmpeg
3) 在“matplotlib”之后添加“ffmpeg”的路径:
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 20 22:19:44 2018
@author: Irbin
"""
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg' # Add the path of ffmpeg here!!
def update_line(num, data, line):
line.set_data(data[..., :num])
return line,
# Fixing random state for reproducibility
np.random.seed(19680801)
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fig1 = plt.figure()
data = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
interval=50, blit=True)
line_ani.save('lines.mp4', writer=writer)
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
im_ani.save('im.mp4', writer=writer)
请注意,如果更改 .mp4 文件的输出目录,可能会出现错误。
关于python - 尝试在 Python 中使用 matplotlib 保存图形动画 - "Invalid file object: <_io.BufferedReader name=76>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198609/
我想知道是否可以访问放在 tomcat 的 conf 文件夹中的文件。通常我会在这个文件中放置多个 webapp 的配置,在 war 之外。 我想使用类路径独立于文件系统。 我过去使用过 lib 文件
我有一个 PowerShell 脚本,它获取文件列表并移动满足特定条件的文件。为什么即使对象为空,foreach 循环也会运行? 我假设如果 $i 不存在,它就不会运行。但是如果 $filePath
我已将 BasicAccountRule.drl 放置在我的 Web 应用程序中,位置为:C:/workspace/exim_design/src/main/resources/rules/drl/i
我使用 File.open('file.txt').class 和 File.open('file.txt').readlines.class 以及前者进行了检查一个返回 File,后者返回 Arra
我正在尝试使用 FileOutputStream 删除文件,在其中写入内容后。这是我用来编写的代码: private void writeContent(File file, String fileC
我正在尝试使用 flink 和 python 批处理 api 测试 Wordcount 经典示例。我的问题是,将数据源从 env.from_elements() 修改为 env.read_text()
我正在尝试制作一个可以同时处理多个不同文件的程序。我的想法是制作一个包含 20 个 FILE* 的数组,以便在我达到此限制时能够关闭其中一个并打开请求的新文件。 为此,我想到了一个函数,它选择一个选项
我有两个文件A和B文件A: 976464 792992 文件B TimeStamp,Record1,976464,8383,ABCD 我想搜索文件 A 和文件 B 中的每条记录并打印匹配的记录。打印的
我有一些保存在 map 中的属性文件。示例: Map map = new HashMap<>(); map.put("1", "One"); map.put("2", "Two"); map.put(
我正在尝试找出一个脚本文件,该文件接受一个包含文件列表的文件(每一行都是一个文件路径,即 path/to/file)并将它们合并到一个文件中。 例如: list.text -- path/to/fil
为了使用 File.CreateText() 和 File.AppendText() 你必须: 通过调用这些方法之一打开流 写消息 关闭流 处理流 为了使用 File.AppendAllText()
使用rsync时,如何在使用--files-from参数复制时重命名文件?我有大约190,000个文件,在从源复制到目标时,每个文件都需要重命名。我计划将文件列表放在一个文本文件中传递给--files
我在非服务器应用程序中使用 Spring(只需从 Eclipse 中某个类的 main() 编译并运行它)。 我的问题是作为 new FileSystemXmlApplicationContext 的
QNX (Neutrino 6.5.0) 使用 ksh 的开源实现作为其 shell 。许多提供的脚本,包括系统启动脚本,都使用诸如 if ! test /dev/slog -ef /dev/slog
当我尝试打开从我的应用程序下载的 xls 文件时,出现此错误: excel cannot open the file because the file format or file extension
有一些相关的概念,即文件指针、流和文件描述符。 我知道文件指针是指向数据类型 FILE 的指针(在例如 FILE.h 和 struct_FILE.h 中声明)。 我知道文件描述符是 int ,例如成员
好吧,这应该很容易... 我是groovy的新手,我希望实现以下逻辑: def testFiles = findAllTestFiles(); 到目前为止,我想出了下面的代码,该代码可以成功打印所有文
我理解为什么以下内容会截断文件的内容: Get-Content | Out-File 这是因为 Out-File 首先运行,它会在 Get-Content 有机会读取文件之前清空文件。 但是当我尝
您好,我正在尝试将文件位置表示为变量,因为最终脚本将在另一台机器上运行。这是我尝试过的代码,然后是我得到的错误。在我看来,python 是如何添加“\”的,这就是导致问题的原因。如果是这种情况,我如何
我有一个只包含一行的输入文件: $ cat input foo bar 我想在我的脚本中使用这一行,据我所知有 3 种方法: line=$(cat input) line=$( input"...,
我是一名优秀的程序员,十分优秀!