Difference between plt.close() and plt.clf()

前端 未结 4 1651
猫巷女王i
猫巷女王i 2020-12-29 03:49

In Python, what is the difference between plt.clf() and plt.close()?

Will they function the same way?

4条回答
  •  梦毁少年i
    2020-12-29 04:32

    There is a slight difference between the two functions.

    plt.close() - It altogether plots the graph in seperate windows,releasing memory,retaining each window for view.

    plt.clf() - We can say,it displays the graph in the same window one after other

    For illustration, I have plotted two graphs with paramters year and views on X axis and Y axis each. Initially I have used closed function.it displayed the graphs in two seperate windows…

    Afterwords, when I run the program with clf() it clears the graph and displays next one in same window i.e figure 1. Here is the code snippet -

        import matplotlib.pyplot as plt
        year = [2001,2002,2003,2004]
        Views= [12000,14000,16000,18000]
        Views2 = [15000,1800,24000,84000]
        plt.plot(year,Views)
        plt.show()
        plt.clf()
        plt.plot(year,Views2)
        plt.show()
        plt.clf()
    

    image 1 with close function


    image 2 with close function


    image 1 with clf function


    image 2 with clf function


提交回复
热议问题