Doubt with .show () from matplotlib

2

Working with matplotlib to generate graphs, I do not quite understand the difference between these two ways of generating and displaying a graph. I have read documentation about it, but it has not been clear to me.

Form 1:

import matplotlib.pyplot as plt

plt.figure()
plt.plot(x, y)
plt.show()

Form 2:

import matplotlib.pyplot as plt

graph = plt.figure()
plt.plot(x, y)
graph.show()

I know they do not do the same, but the difference is not clear to me.

Can someone explain to me step by step what happens in each case?

    
asked by Zhisi 10.01.2018 в 14:11
source

1 answer

1

In the first way you are creating the graph programmatically. Matplotlib saves in memory that you have called plt.figure() then creates a figure. Then all the calls you make to paint in the figure as plt.plot() will act on the last figure you created by calling plt.figure() , including the call plt.show() .

On the other hand, in the second you are obtaining the result of the figure plt.figure() in a variable. This variable is an instance of the figure. Then you can call plt.show() or apply the method show() of the object Figure to show it. If you look at the documentation of the function plt.figure() you will see that under the parameters, where it indicates that returns ("Returns:"), you will see that it says that it returns an instance of the figure, the one that you are saving and then operate on it. This would be appropriate if we want to operate on several figures.

    
answered by 04.02.2018 / 13:37
source