Search code examples
pythonmatplotlibfigure

Put additional data on existing figure


I'm solving the optimization problem by different methods (for study).

The idea is written bellow:

instance = class_with_diff_methods()

instance.create_some_func()
instance.optimization_method_one()
instance.create_data_for_plot()
instance.display()

instance.optimization_method_two()
instance.create_data_for_plot()
instance.display()

So I wanted to add the new data iteratively, without saving and I implemented my idea this way: `

import matplotlib.pyplot as plt

 def display(self):

    if not self.__plot_exists:

        self.figure = plt.figure()
        plt.scatter( self.data[0], self.data[1])
        plt.plot( self.data[0], self.appdata) 
        self.figure.show()
        self.__plot_exists = True

    else:
        plt.plot( self.data[0], self.appdata)
        plt.show( block=False)`

This works, but the problem is that I don't really understand, why I even need (it necessary) using "self.figure" in the first part of the code and why I should use "plt.show()" instead of just "self.figure.show" at the last line.

I would be grateful for links, that will help me clearly understand how this works.


Solution

  • The plt.show() method is native from matplotlib, it is what launches the GUI. Check out their documentation if you would like to know about these types of things:

    https://matplotlib.org/contents.html