Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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循环生成但仅在循环结束后绘制的多个图形_Python_Loops_Matplotlib_Plot_Iteration - Fatal编程技术网

由python循环生成但仅在循环结束后绘制的多个图形

由python循环生成但仅在循环结束后绘制的多个图形,python,loops,matplotlib,plot,iteration,Python,Loops,Matplotlib,Plot,Iteration,我有以下代码,它获取对象列表,并从对象中存储的数据为每个对象绘制一个图形: def Plot(self, *figures , wait = True): def plot(fig,n): fig.fig = plt.figure(n) print("Ploting figure {} for file {}\n".format(n,fig.name)) fig.Plot() plt.show() if

我有以下代码,它获取对象列表,并从对象中存储的数据为每个对象绘制一个图形:

def Plot(self, *figures , wait = True):


    def plot(fig,n):
        fig.fig = plt.figure(n)
        print("Ploting figure {} for file {}\n".format(n,fig.name))
        fig.Plot()
        plt.show()
        if wait:
            input("Press Enter to continue to next figure")

    if not figures:
        figures = self.figures            
        for n,fig in figures.items():
            plot(fig,n)
    else:
        for n,fig in enumerate(figures):
            plot(fig,n)
fig.Plot()是处理打印的函数。运行“绘图”时,在循环的每次迭代中都会生成一个图形,但实际绘图只有在循环完成后才会发生


我试图实现的是,在每次迭代中生成图形,并在继续下一次迭代之前绘制绘图

在更多的谷歌搜索之后,我在:physicalmodelingwithpython.blogspot.co.il/2015/07/…上找到了一个解决方案,其中的一行:“if wait:input(“按回车键继续下一个图形”)”应该替换为:“while True:if plt.waitforbuttonpress():break”

plt.waitforbuttonpress()返回True,如果按下鼠标按钮,则返回False,从而停止函数中的循环并返回控制台

我添加了关键字'Startat',以便以后能够从列表中的下一个图继续(这也可以通过合适的装饰器来完成)

一个更优雅的解决方案可能是使用python事件处理例程实现的,我和John Snow都知道这些例程

完整功能(某些关键字与原始问题无关):


在更多的谷歌搜索之后,我在:physicalmodelingwithpython.blogspot.co.il/2015/07/…上找到了一个解决方案,其中的一行:“if wait:input(“按Enter键继续下一个图形”)”应该替换为:“while True:if plt.waitforbuttonpress():break”

plt.waitforbuttonpress()返回True,如果按下鼠标按钮,则返回False,从而停止函数中的循环并返回控制台

我添加了关键字'Startat',以便以后能够从列表中的下一个图继续(这也可以通过合适的装饰器来完成)

一个更优雅的解决方案可能是使用python事件处理例程实现的,我和John Snow都知道这些例程

完整功能(某些关键字与原始问题无关):


在更多的谷歌搜索之后,我在以下位置找到了一个解决方案:“if wait:input(“按Enter键继续下一个数字”)”应该替换为:“while True:if plt.waitforbuttonpress():break”请发布您的评论作为答案。在更多的谷歌搜索之后,我在以下位置找到了一个解决方案:行:“if wait:input(“按Enter键继续下一个图形”)”应替换为:“while True:if plt.waitforbuttonpress():break”请将您的评论作为答案发布。
def Plot(self, figures=None, wait=True, Save=False, Legendtitle=False, Startat=0):

    def plot(fig, n):

        print("Ploting figure {} for file {}\n".format(n, fig.name))
        fig.Plot(Legendtitle)
        if Save:
            fig.SavePlot()   
        if wait:    
            while True:
                if plt.waitforbuttonpress():
                    break
                else:
                    return True


    if not figures:
        figures = self.figures
        for n, fig in figures.items(): 
            if n>= Startat:                   
                if plot(fig, n):
                    break


    else:
        for n, fig in enumerate(figures):
            if n>= Startat:
                if plot(fig, n):
                    break