Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Matplotlib animation.FuncAnimation:自定义帧生成器仅生成一次_Python 3.x_Matplotlib - Fatal编程技术网

Python 3.x Matplotlib animation.FuncAnimation:自定义帧生成器仅生成一次

Python 3.x Matplotlib animation.FuncAnimation:自定义帧生成器仅生成一次,python-3.x,matplotlib,Python 3.x,Matplotlib,我在matplotlib动画中遇到了一个奇怪的问题。我正在尝试使用以下代码创建动画条形图: import os, time from PIL import Image, ImageSequence import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.path as path import matplotlib.animation as a

我在matplotlib动画中遇到了一个奇怪的问题。我正在尝试使用以下代码创建动画条形图:

import os, time
from PIL import Image, ImageSequence
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.animation as animation

import blackxample

FILE_PREFIX = "cell-isotohyper"
FILE_SUFFIX = ".tif"
FILE_PATH   = "./example-video"
XCUT = (91, 91+266)
YCUT = (646, 646+252)

LIMIT = 100
OFFSET = 0

Y_SCALE = 3000
NUM_OF_BINS = 37

BAR_WIDTH = 1.0
BAR_COLOR = 'b'
RANGE = range(0, NUM_OF_BINS//2+1)

def animate(i, fig, ax, bars):
    # = np.random.randn(1000)
    print(len(i))
    for a in RANGE:
        bars[a].set_height(i[a])
    return (fig, ax, bars)

def main():
    fig, ax = plt.subplots()
    ax.set_ylim(0, Y_SCALE)
    ax.set_xlim(0, NUM_OF_BINS//2+1)

    bars = ax.bar(np.arange(NUM_OF_BINS), [i for i in range(NUM_OF_BINS)], BAR_WIDTH, color=BAR_COLOR)

    ani = animation.FuncAnimation(fig, animate, xframes, fargs = (fig, ax, bars), interval=500)
    plt.show()
如果我使用随机生成的数据或常量,则此代码段可以完全正常工作:

def xframes():
    i = 0
    while i < 100:
        yield [2312.7094266223335, 27.238786592368257, 75.252063484372513, 13.678304922077643, 11.879804374653929, 21.900570139020687, 2.930771773796323, 11.945594479736741, 10.88517941461987, 4.4176609254771506, 4.1075871395528338, 1.248363771876285, 1.4798157379442216, 3.5285036346353564, 3.2583080973651732, 3.4640042567344267, 3.130503535456981, 0.67334205875304676, 0.71393606581800562]
        #yield np.histogram(np.random.randn(1000), NUM_OF_BINS//2 + 1)[0]
        i+=1
def xframes():
i=0
当我<100时:
产量[2312.7094266223335, 27.238786592368257, 75.252063484372513, 13.678304922077643, 11.879804374653929, 21.900570139020687, 2.930771773796323, 11.945594479736741, 10.88517941461987, 4.4176609254771506, 4.1075871395528338, 1.248363771876285, 1.4798157379442216, 3.5285036346353564, 3.2583080973651732, 3.4640042567344267, 3.130503535456981, 0.67334205875304676, 0.71393606581800562]
#产量直方图(np.random.randn(1000),单元数//2+1)[0]
i+=1
相反,使用函数aframes只会在与animation.FuncAnimation()一起使用时生成第一个项目。但是,如果手动迭代aframe,生成器工作完全正常

def aframes():
    list_of_files = []
    for dirname, dirnames, filenames in os.walk(FILE_PATH):
        for filename in filenames:
            if filename.startswith(FILE_PREFIX) and filename.endswith(FILE_SUFFIX):
                list_of_files.append(os.path.join(FILE_PATH, filename))

    # Open every picture - in every file
    count = 0
    imagecount = 0
    framecount = 0
    skipped = 0
    for file in list_of_files:
        framecount = 0
        a = Image.open(file)
        for frame in ImageSequence.Iterator(a):
            if count > OFFSET and count <= OFFSET+LIMIT:
                # Cut image beforehand - probably faster
                frame = frame.crop((XCUT[0], YCUT[0], XCUT[1], YCUT[1]))
                # Load image intro Matrix
                imageMatrix = blackxample.Matrix.fromPillow(frame)
                try:
                    imageMatrix.findContour()
                    imageMatrix.calculateCentroid()
                    imageMatrix.transform(NUM_OF_BINS)
                    #yield imageMatrix.getTransform()
                    yield [2312.7094266223335, 27.238786592368257, 75.252063484372513, 13.678304922077643, 11.879804374653929, 21.900570139020687, 2.930771773796323, 11.945594479736741, 10.88517941461987, 4.4176609254771506, 4.1075871395528338, 1.248363771876285, 1.4798157379442216, 3.5285036346353564, 3.2583080973651732, 3.4640042567344267, 3.130503535456981, 0.67334205875304676, 0.71393606581800562]
                except blackxample.NoConvergenceError:
                    skipped+=1
            print("[", count ,"] done")

            framecount+=1
            count+=1
        imagecount+=1

# Test for frame iterator - works fine
#for i in _frames():
#    print(i)
def aframes():
_文件列表=[]
对于os.walk(文件路径)中的dirname、dirname和文件名:
对于文件名中的文件名:
如果filename.startswith(文件前缀)和filename.endswith(文件后缀):
列出所有文件。追加(os.path.join(文件路径,文件名))
#打开每个文件中的每个图片
计数=0
imagecount=0
帧数=0
跳过=0
对于\u文件列表中的文件:
帧数=0
a=图像。打开(文件)
对于ImageSequence.Iterator(a)中的帧:

如果count>OFFSET和count因为我没有找到任何关于这个问题的解决方案,我决定将aframes()的结果保存在一个文件中,然后分别读取和设置动画,这样可以在不调整动画代码的情况下完美地工作。

我想你应该传递
动画(…,aframes(),…)
动画(…,aframes,…)
@tcaswell不,这不起作用-animation.animate()将生成器对象视为一个序列,并尝试使用len()确定其长度-这在生成器序列上是不可能的,并会因类型错误而退出。aframes()和xframes()都是这种情况@tcaswell我收回我的语句-使用较新版本的matplotlib确实允许直接传递生成器。但是,它不会改变结果,函数aframes()在使用animate时只产生一次,而xframes()会产生我想要的结果。如果直接迭代,aframes()仍然有效。