代码之家  ›  专栏  ›  技术社区  ›  Georg

通过多次调用函数来显示多个matplotlib图?

  •  3
  • Georg  · 技术社区  · 7 年前

    下面的代码不做我想要的:它显示一个数字,等待用户关闭窗口,然后显示第二个数字。

    def my_plot(arr_x, arr_y):
        import matplotlib.pyplot as plt
        plt.plot(arr_x, arr_y)
        plt.show()
    
    if __name__ == '__main__':
        my_plot((1, 2, 3), (10, 20, 90))
        my_plot((101, 102, 103), (1, 5, 4))
    

    我想同时显示这两个数字,这样我可以直观地比较它们,然后选择其中一个并关闭另一个。 我的要求是只在一个用户定义函数中存储所有matplotlib调用 例如 my_plot .

    我读过了 show 必须在定义了所有不符合上述要求的绘图之后调用。

    实际上,我几乎失去了Matplotlib能够自己完成这一任务的希望,也许唯一的解决方案是为每个调用使用一个新的线程。 米尔图 ?


    编辑:
    问题解决了。我在这里分享了一个我写的函数,感谢尼兹尼的回答,这正是我想要的。当然,它是可以改进的,但是对于我的大多数用例来说,它是有效的,我可以很高兴地忘记matplotlib。

    #!/usr/bin/env python
    import matplotlib.pyplot as plt
    from os.path import splitext
    
    def plot_XY(arr_x, arr_y, graph_file_name=None, graph_title="", x_label="", y_label=""):
        """Plot a series of (x, y) points.
    
        If graph_file_name is not provided, the graph is
        plotted in a temporary window; else, the file
        format is deduced from the extension contained
        in graph_file_name.
        """
    
        def create_figure():
            plt.figure()
            plt.plot(arr_x, arr_y)
            plt.title(graph_title)
            plt.xlabel(x_label)
            plt.ylabel(y_label)
    
        if graph_file_name is None:
            def my_show():
                create_figure()
                plt.show()
            from multiprocessing import Process
            Process(target=my_show, args=[]).start()
        else:
            extension = splitext(graph_file_name)[1][1:] # Get extension, without dot
            if extension not in ("eps", "pdf", "pgf", "png", "ps", "raw", "rgba", "svg", "svgz"):
                print(f"\"{extension}\" is not a file extension recognized by savefig. Assuming png.")
                extension = "png"
                graph_file_name += "." + extension
            create_figure()
            plt.savefig(graph_file_name, format=extension)
            plt.close()
    
    if __name__ == '__main__':
        # Test: calling the function multiple times:
        x = (1, 2, 3)
        plot_XY(x, (1, 4, 2), graph_title="Example")
        plot_XY(x, (2, 3, 1), graph_file_name="output.abc")
        plot_XY(x, (1, 4, 9), x_label="x", y_label="x^2")
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   Nieznany    7 年前

    可以使用单个进程绘制窗口并显示它,而不阻塞主进程。
    示例解决方案:

    def my_plot(arr_x, arr_y):
        import matplotlib.pyplot as plt
        from multiprocessing import Process
        def _my_plot(arr_x, arr_y):
    
            plt.figure()
            plt.plot(arr_x, arr_y)
            plt.show()
    
        Process(target=_my_plot, args=[arr_x,arr_y]).start()
    
    if __name__ == '__main__':
        my_plot((1, 2, 3), (10, 20, 90))
        my_plot((101, 102, 103), (1, 5, 4))
    

    Resources

        2
  •  0
  •   chryss    7 年前

    呼叫 plt.show() 一旦你创造了所有的数字。

    import matplotlib.pyplot as plt
    
    def my_plot(arr_x, arr_y):
        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1)
        ax.plot(arr_x, arr_y)
        return fig
    
    if __name__ == '__main__':
        f1 = my_plot((1, 2, 3), (10, 20, 90))
        f2 = my_plot((101, 102, 103), (1, 5, 4))
        plt.show()